通过CmakeLists.txt或Android.mk向Android工程添加c++代码

一、Cmake Build /main/.Java->main/.cpp->app/CmakeLists.txt->app/build.gradle

1.创建java侧接口文件

目的是以后将这些native方法当java方法使用,java侧只关心这几个方法。

publicclassTestJNI{ 

//加载.so库: 

//1.加载之后,sayHello就与so库里面的cpp中的函数对应起来,不需要头文件 

    static{ 
   		 System.loadLibrary("TestJNI"); 
    } 
    
	Public static native String sayHello(); 

} 

2.根据.h文件的函数命名规则编写cpp文件

3.编译java接口文件对应的.h文件,根据.h文件的函数名称实现对应的cpp文件,cpp文件一般放在main文件夹下的cpp文件夹下

根据Java方法编译出JNI的.h头文件。命令行进入到java代码目录下,即app/src/main/java,输入以下命令:

javah -jni xxxx.com.jnitech.JniInterface

JniInterface为class的类名,然后在对应java文件的目录下生成对应的.h文件

4.在app文件夹下新建CmakeLists.txt文件添加源文件依赖关系

4.1 添加源代码依赖:

add_library( # Specifies the name of the library. 

             native-lib 

             # Sets the library as a shared library. 

             SHARED 

             # Provides a relative path to your source file(s). 

             src/main/cpp/native-lib.cpp ) 

4.2 添加原生API和库依赖,例如下面将原生log库作为依赖保存到log-lib变量

原生库依赖:

find_library( # Defines the name of the path variable that stores the 
    
                  # location of the NDK library. 
    
                  log-lib 
    
                  # Specifies the name of the NDK library that 
    
                  # CMake needs to locate. 
    
                  log ) 


# Links your native library against one or more other native libraries. 

target_link_libraries( # Specifies the target library. 

                       native-lib 

                       # Links the log library to the target library. 

                       ${log-lib} ) 

原生API依赖:

add_library( app-glue 

             STATIC 

             ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c ) 

# You need to link static libraries against your shared native library. 

target_link_libraries( native-lib app-glue ) 

4.3 添加已经编译好的.so库依赖

add_library( imported-lib 

             SHARED 

             IMPORTED ) 

set_target_properties( # Specifies the target library. 

                       imported-lib 

                       # Specifies the parameter you want to define. 

                       PROPERTIES IMPORTED_LOCATION 

                       # Provides the path to the library you want to import. 

                       imported-lib/src/${ANDROID_ABI}/libimported-lib.so ) 

include_directories( imported-lib/include/ ) #已经编译好的库可能有头文件也要引入 

4.5 如果自身的.so库用到了上述的依赖API或库还需要关联到自身库中

target_link_libraries(  #自身的cpp将要生成的.so库文件名 

				 native-lib  

       				  #依赖的其他的库名 

    				imported-lib #外部库 

    				app-glue     #原生API 

 					   ${log-lib} ) #原生库

在app的build.gradle里面关联CmakeLists.txt

  // Encapsulates your external native build configurations. 

  externalNativeBuild { 

    // Encapsulates your CMake build configurations. 

    cmake { 

      // Provides a relative path to your CMake build script. 

      path "CMakeLists.txt" 

    } 

  } 

默认是NDK支持的ABI全部打包到APK中,通过下面的配置可以打包指定ABI类型的.so库到APK中

defaultConfig { 

    ... 

    externalNativeBuild { 

      cmake {...} 

      // or ndkBuild {...} 

    } 

  

    ndk { 

      // Specifies the ABI configurations of your native 

      // libraries Gradle should build and package with your APK. 

      abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 

                   'arm64-v8a' 

    } 

  } 

二、Ndk Build main/.Java->main/jni/.cpp->PROJECT_PATH/jni/Android.mk->PROJECT_PATH/Application/mk

1.创建java接口文件,如上述第一点

2.在main目录下右键–>New–>Folder–>JNI Folder,再创建cpp文件,如上述第二点

Java_com_huawei_hicode_ndk_JniUtils_getStringFromC( 

        JNIEnv *env, 

        jclass type) { 

    std::string hello = "Hello from C++"; 

    return env->NewStringUTF(hello.c_str()); 

} 

3.在工程目录下先创建jni文件夹,再创建Application.mk如下:

APP_STL := c++_static #默认提供最小运行时库,这里是添加c++静态库,使string\vector\iostream能够包含进来 

4.在app目录或者上述jni目录创建Android.mk目录。如下:

    LOCAL_PATH := $(call my-dir) 
    include $(CLEAR_VARS) 
    LOCAL_MODULE    := TestJNI
    LOCAL_SRC_FILES := $(LOCAL_PATH)/../app/src/main/jni/com_huawei_hicode_ndk_JniUtils.cpp 
    include $(BUILD_SHARED_LIBRARY) 

5.在app/build.gradle中添加:

externalNativeBuild { 

        ndkBuild { 

            path '../jni/Android.mk' 

        } 

} 

你可能感兴趣的:(Android)