美文网首页Android-NDK/JNIJNI&NDK
JNI&NDK开发最佳实践(二):CMake实现调用已有

JNI&NDK开发最佳实践(二):CMake实现调用已有

作者: taoyyyy | 来源:发表于2019-06-28 22:41 被阅读41次

    目标

    用CMake方法实现在java中调用本地C/C++文件中的方法,并生成相应so库导出。

    实现步骤梳理

    1. 在需要调用本地方法的java文件中加载so库,并声明本地函数。
    2. 新建与java同级的cpp文件夹,在其中新建c/c++文件,实现本地方法。
    3. 新建CMakeList.txt文件,在其中作相应so库名称、C文件路径等相关配置。
    4. 在build.gradle中配置CMakeList.txt文件路径、ABI类别等。
    5. 编译运行,在app/build/intermediates/cmake/debug/obj路径下即可找到生成的so库。

    具体实现

    1.在需要调用本地方法的java文件中加载so库,并声明本地函数。

    package com.example.taoying.testndkapp;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        // Used to load the 'native-lib' library on application startup.
        static {
            System.loadLibrary("native-lib");//加载so库,待加载的so库的名称为libnative-lib.so。
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Example of a call to a native method
            TextView tv = findViewById(R.id.sample_text);
            tv.setText(stringFromJNIwithp("11"));
        }
    
        /**
         * A native method that is implemented by the 'native-lib' native library,
         * which is packaged with this application.
         */
        public native String stringFromJNI();//本地方法用native修饰
    
    
        public native String stringFromJNIwithp(String a);
    }
    

    注意两点:

    • System.loadLibrary("native-lib");加载so库,so库的名称为libnative-lib.so。
    • 本地函数用native修饰。

    2.新建与java同级的cpp文件夹,在其中新建c/c++文件,实现本地方法。

    本地新建cpp文件夹和相应文件.png

    native-lib.cpp

    #include <jni.h>
    #include <string>
    
    extern "C" JNIEXPORT jstring JNICALL
    Java_com_example_taoying_testndkapp_MainActivity_stringFromJNI(
            JNIEnv* env,
            jobject /* this */) {
        std::string hello = "Hello from C++";
        return env->NewStringUTF(hello.c_str());
    }
    
    
    extern "C" JNIEXPORT jstring JNICALL Java_com_example_taoying_testndkapp_MainActivity_stringFromJNIwithp
            (JNIEnv * env, jobject /* this */, jstring jstring1){
        std::string hello = "stringFromJNIwithp";
        return env->NewStringUTF(hello.c_str());
    }
    
    • 本地函数名的格式需要遵循如下规则:Java_包名类名方法名,否则该方法将无法被找到。
    • JNIEnv* 表示一个指向JNI环境的指针,可以通过他来访问JNI提供的接口方法。
    • jobject表示Java对象中的this(如果该方法是非静态方法)或者Class对象(如果该方法是static方法)。
    • JNIEXPORT和JNICALL是JNI中定义的宏,可以在jni.h这个头文件中找到。JNIEXPORT确保函数在符号表中可见, JNICALL确保函数使用正确的调用约定。

    3.新建CMakeList.txt文件,在其中作相应so库名称、C文件路径等相关配置。

    CMakeLists.txt

    # For more information about using CMake with Android Studio, read the
    # documentation: https://d.android.com/studio/projects/add-native-code.html
    
    # Sets the minimum version of CMake required to build the native library.
    
    cmake_minimum_required(VERSION 3.4.1)
    
    # Creates and names a library, sets it as either STATIC
    # or SHARED, and provides the relative paths to its source code.
    # You can define multiple libraries, and CMake builds them for you.
    # Gradle automatically packages shared libraries with your APK.
    //add_library表示生成静态链接库libnative-lib.so
    //native-lib 即为生成库的名称
    //SHARED 表示生成动态库libnative-lib.so, STATIC表示生成静态库libnative-lib.a。
    //native-lib.cpp 表示c/c++源文件的路径。
    //${CMAKE_SOURCE_DIR}:表示CMakeLists.txt的当前文件夹路径。
    add_library( # Sets the name of the library.
                 native-lib
    
                 # Sets the library as a shared library.
                 SHARED
    
                 # Provides a relative path to your source file(s).
                 native-lib.cpp )
    
    # Searches for a specified prebuilt library and stores the path as a
    # variable. Because CMake includes system libraries in the search path by
    # default, you only need to specify the name of the public NDK library
    # you want to add. CMake verifies that the library exists before
    # completing its build.
    
    find_library( # Sets the name of the path variable.
                  log-lib
    
                  # Specifies the name of the NDK library that
                  # you want CMake to locate.
                  log )
    
    # Specifies libraries CMake should link to your target library. You
    # can link multiple libraries, such as libraries you define in this
    # build script, prebuilt third-party libraries, or system libraries.
    //需要加载的so库,需要加载多个时用空格" "连接
    target_link_libraries( # Specifies the target library.
                           native-lib
    
                           # Links the target library to the log library
                           # included in the NDK.
                           ${log-lib} )
    

    这里有更多关于CMakeList.txt的编写规则的介绍。

    4.在build.gradle中配置CMakeList.txt文件路径、ABI类别等。

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 28
        defaultConfig {
            applicationId "com.example.taoying.testndkapp"
            minSdkVersion 15
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            externalNativeBuild {
                cmake {
                    //代表编译C++代码时用的编译选项
                    //如cppFlags "-frtti -fexceptions" 配置支持RTTI和C++异常处理(这个非必须 也可以不配)
                    cppFlags ""
                }
            }
            ndk {
                //配置ABI类别,注意这里配置的Cpu类型在编译后会生成相应的so动态库,不配置默认生成所有
                //生成的so库在C:\Users\taoying\Desktop\TestNdkApp\app\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so路径下
                abiFilters "arm64-v8a"
            }
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
        externalNativeBuild {
            // 指定CMake编译配置文件路径
            cmake {
                path "src/main/cpp/CMakeLists.txt"
                version "3.10.2"
            }
        }
    
        //可选配置
        /*sourceSets.main {
            //默认so库放在src/main/jniLibs目录下,此时可以制定新的存放so库的目录
            jniLibs.srcDirs = ['libs']
        }*/
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    
    

    5. 编译运行,在app/build/intermediates/cmake/debug/obj路径下即可找到生成的so库。

    so库路径.png

    更多JNI&NDK系列文章,参见:
    JNI&NDK开发最佳实践(一):开篇
    JNI&NDK开发最佳实践(二):CMake实现调用已有C/C++文件中的本地方法
    JNI&NDK开发最佳实践(三):CMake实现调用已有so库中的本地方法
    JNI&NDK开发最佳实践(四):JNI数据类型及与Java数据类型的映射关系
    JNI&NDK开发最佳实践(五):本地方法的静态注册与动态注册
    JNI&NDK开发最佳实践(六):JNI实现本地方法时的数据类型转换
    JNI&NDK开发最佳实践(七):JNI之本地方法与java互调
    JNI&NDK开发最佳实践(八):JNI局部引用、全局引用和弱全局引用
    JNI&NDK开发最佳实践(九):调试篇

    相关文章

      网友评论

        本文标题:JNI&NDK开发最佳实践(二):CMake实现调用已有

        本文链接:https://www.haomeiwen.com/subject/cihycctx.html