https://developer.android.com/studio/projects/add-native-code.htmlhtml
2.选择想要使用的c++标准,如C++14.android
3.最后 生成的项目以下:c++
其中:架构
4.编辑代码,使用本地程序....app
项目名(或者File) ---> 右键 ---> New ---> Folder ---> JNI Folderide
在上一步上建立的jni目录(当前版本目录名是cpp) 右键 ---> New ---> File 输入CMakeLists.txt函数
1 # Sets the minimum version of CMake required to build your native library. 2 # This ensures that a certain set of CMake features is available to 3 # your build. 4 5 cmake_minimum_required(VERSION 3.4.1) 6 7 # Specifies a library name, specifies whether the library is STATIC or 8 # SHARED, and provides relative paths to the source code. You can 9 # define multiple libraries by adding multiple add.library() commands, 10 # and CMake builds them for you. When you build your app, Gradle 11 # automatically packages shared libraries with your APK. 12 13 add_library( # Specifies the name of the library. 14 # 本地库名 15 student-lib 16 17 # Sets the library as a shared library. 18 # 本地库的类型 19 SHARED 20 21 # Provides a relative path to your source file(s). 22 # 要编译的源文件 ,多个之间用空格. 23 student.cpp school.cpp) 24 25 # Specifies a path to native header files. 26 include_directories(inc) 27 28 29 #添加本地代码依赖的其它系统库. 30 find_library( # Defines the name of the path variable that stores the 31 # location of the NDK library. 32 log-lib 33 34 # Specifies the name of the NDK library that 35 # CMake needs to locate. 36 log ) 37 38 # Links your native library against one or more other native libraries. 39 target_link_libraries( # Specifies the target library. 40 student-lib 41 42 # Links the log library to the target library. 43 ${log-lib} )
cmake命令官网 : https://cmake.org/cmake/help/latest/manual/cmake-commands.7.html 工具
add_library()gradle |
向您的 CMake 构建脚本添加源文件或库ui 1 add_library( # Specifies the name of the library. 2 native-lib 3 4 # Sets the library as a shared library. 5 SHARED 6 7 # Provides a relative path to your source file(s). 8 src/main/cpp/native-lib.cpp ) |
include_directories |
指定头文件的路径 1 add_library(...) 2 3 # Specifies a path to native header files. 4 include_directories(src/main/cpp/include/) |
find_library() | 添加引用的NDK 库 1 find_library( # Defines the name of the path variable that stores the 2 # location of the NDK library. 3 log-lib 4 5 # Specifies the name of the NDK library that 6 # CMake needs to locate. 7 log ) |
target_link_libraries() | 连接多个库 1 target_link_libraries( native-lib imported-lib app-glue ${log-lib} ) |
add_library() | 将其它源代码编译到本地库中. 1 add_library( app-glue 2 STATIC 3 ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c ) 4 5 # You need to link static libraries against your shared native library. 6 target_link_libraries( native-lib app-glue ${log-lib} ) |
set_target_properties 和 IMPORTED |
IMPORTED不是个函数,只是add_library的参数,添加其余预构建的本地库 而后,您须要使用 set_target_properties() 命令指定库的路径. |
右键点击您想要关联的模块(例如 app 模块),并从菜单中选择 Link C++ Project with Gradle。关联到以前建立的CMakeLists.txt,点OK.
略...
https://developer.android.com/ndk/guides/cmake.html#variables
1 android { 2 ... 3 defaultConfig { 4 ... 5 // This block is different from the one you use to link Gradle 6 // to your CMake or ndk-build script. 7 externalNativeBuild { 8 9 // For ndk-build, instead use ndkBuild {} 10 cmake { 11 12 // Passes optional arguments to CMake. 13 arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang" 14 15 // Sets optional flags for the C compiler. 16 cFlags "-D_EXAMPLE_C_FLAG1", "-D_EXAMPLE_C_FLAG2" 17 18 // Sets a flag to enable format macro constants for the C++ compiler. 19 cppFlags "-D__STDC_FORMAT_MACROS" 20 } 21 } 22 } 23 24 buildTypes {...} 25 26 ... 27 }
默认状况下,Gradle 会针对 NDK 支持的 ABI 将您的原生库构建到单独的 .so
文件中,并将其所有打包到您的 APK 中。若是您但愿 Gradle 仅构建和打包原生库的特定 ABI 配置,您能够在模块级 build.gradle
文件中使用 ndk.abiFilters
标志指定这些配置.
1 apply plugin: 'com.android.application' 2 3 android { 4 compileSdkVersion 28 5 defaultConfig { 6 applicationId "com.example.tocpp5" 7 minSdkVersion 15 8 targetSdkVersion 28 9 versionCode 1 10 versionName "1.0" 11 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 12 // This block is different from the one you use to link Gradle 13 // to your CMake or ndk-build script. 14 externalNativeBuild { 15 // For ndk-build, instead use ndkBuild {} 16 cmake { 17 // Passes optional arguments to CMake. 18 arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang" 19 20 // Sets optional flags for the C compiler. 21 cFlags "-D_EXAMPLE_C_FLAG1", "-D_EXAMPLE_C_FLAG2" 22 23 // Sets a flag to enable format macro constants for the C++ compiler. 24 cppFlags "-D__STDC_FORMAT_MACROS" 25 26 abiFilters 'armeabi-v7a','arm64-v8a' 27 28 } 29 } 30 ndk { 31 // Specifies the ABI configurations of your native 32 // libraries Gradle should build and package with your APK. 33 abiFilters 'x86', 'x86_64', 'armeabi-v7a','arm64-v8a' 34 } 35 } 36 buildTypes { 37 release { 38 minifyEnabled false 39 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 40 } 41 } 42 externalNativeBuild { 43 cmake { 44 path file('src/main/jni/CMakeLists.txt') 45 } 46 } 47 } 48 49 dependencies { 50 implementation fileTree(dir: 'libs', include: ['*.jar']) 51 implementation 'androidx.appcompat:appcompat:1.0.2' 52 implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 53 testImplementation 'junit:junit:4.12' 54 androidTestImplementation 'androidx.test:runner:1.2.0' 55 androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 56 }
在 defaultConfig.externalNativeBuild.cmake {}
块
或
defaultConfig.externalNativeBuild.ndkBuild {}
块
中配置另外一个 abiFilters
标志。Gradle 会构建defaultConfig.ndk中的 ABI 配置,不过仅会打包在 defaultConfig.
块中指定的配置。cmake或者
{}ndkBuild