CMakeLists.txt
就是编译so库的脚本文件,类似于原来的Android.mk,Application.mk。
# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.
cmake_minimum_required(VERSION 3.4.1)
# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.
#语法,参考:https://cmake.org/cmake/help/latest/command/add_library.html
add_library( [STATIC | SHARED | MODULE]
[EXCLUDE_FROM_ALL]
source1 [source2 ...])
# 语法#########################################
# 示例
add_library(
#编译生成的库的名称,注意最终生成时会在前面加`lib`.
# Specifies the name of the library.
native-lib
# Sets the library as a shared library.
# 生成的库的类型,有SHARED,STATIC,MODULE
SHARED
# Provides a relative path to your source file(s).
# 要编译的源代码文件
src/main/cpp/native-lib.cpp )
如果有源码文件有头文件,自然需要包含:
# 语法, 参考:https://cmake.org/cmake/help/latest/command/include_directories.html
include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])
# 语法#########################################
# Specifies a path to native header files.
# 注意这里指定的是头文件的目录
include_directories(src/main/cpp/include/)
有时候我们发现需要使用到NDK中定义的Api,因为NDK中的API都已经是编译好的,而且CMake
会自动去NDK目录中查找这些我们需要使用的库,所以这里只需要提供库的名称就可以了,一下举例加载NDK中的log库,因为我们在NDK开发中,总是少不了要打印日志的。
- 首先查找要使用的库,使用变量保存它的路径。
# 语法,参考:https://cmake.org/cmake/help/latest/command/find_library.html
# A short-hand signature is:
# name1 保存库路径的变量
find_library ( name1 [path1 path2 ...])
# The general signature is:
find_library (
name | NAMES name1 [name2 ...] [NAMES_PER_DIR]
[HINTS path1 [path2 ... ENV var]]
[PATHS path1 [path2 ... ENV var]]
[PATH_SUFFIXES suffix1 [suffix2 ...]]
[DOC "cache documentation string"]
[NO_DEFAULT_PATH]
[NO_CMAKE_ENVIRONMENT_PATH]
[NO_CMAKE_PATH]
[NO_SYSTEM_ENVIRONMENT_PATH]
[NO_CMAKE_SYSTEM_PATH]
[CMAKE_FIND_ROOT_PATH_BOTH |
ONLY_CMAKE_FIND_ROOT_PATH |
NO_CMAKE_FIND_ROOT_PATH]
)
# 语法#########################################
# 示例
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included 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 )
# 语法,参考: https://cmake.org/cmake/help/latest/command/target_link_libraries.html
target_link_libraries( ... - ... ...)
# 语法 end #######################
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.
# 注意这里可以同时链接多个库
target_link_libraries( # Specifies the target library.
native-lib
# Links the log library to the target library.
# 此处使用上面保存的变量
${log-lib} )
# 这样就会在\app\.externalNativeBuild\cmake\debug\${ANDROID_ABI}\ 目录下生成libapp-glue.a/静态库
add_library( app-glue
# 以静态的方式添加
STATIC
# 该源码用于管理`NativeActivity`生命周期事件和触摸事件
${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 ${log-lib} )
add_library
添加第三库# 语法,因为已经编译好了,所以使用`IMPORTED`关键字
add_library( <SHARED|STATIC|MODULE|UNKNOWN> IMPORTED [GLOBAL])
# 示例
add_library( imported-lib
SHARED
IMPORTED )
ANDROID_ABI
变量,它表示默认的ABI架构和NDK支持的架构,如果我们在build.gradle
中设置了过滤值,则表示过滤后的架构集合。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是在项目的app目录下的
imported-lib/src/${ANDROID_ABI}/libimported-lib.so )
include_directories( imported-lib/include/ )
imported-lib
所依赖的),则不需要执行以下步骤。target_link_libraries( native-lib imported-lib app-glue ${log-lib} )
build.gradle
中配置编译库的脚本android {
......
defaultConfig {
......
//在默认配置中配置 cmake 的一些参数
externalNativeBuild {
cmake {
arguments "-DANDROID_TOOLCHAIN=clang"
cFlags ""
cppFlags ""
}
}
//ndk 配置
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'
}
}
//配置库编译的脚本
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
//定义不同的产品类型(多渠道打包),通过复写externalNativeBuild,使用target字段实现打包不同的so库。
productFlavors {
...
demo {
...
externalNativeBuild {
cmake {
...
// Specifies which native libraries to build and package for this
// product flavor. If you don't configure this property, Gradle
// builds and packages all shared object libraries that you define
// in your CMake or ndk-build project.
targets "native-lib-demo"
}
}
}
paid {
...
externalNativeBuild {
cmake {
...
targets "native-lib-paid"
}
}
}
}
}
argments
参数可以参考https://developer.android.com/ndk/guides/cmake.html#variables。cFlags
和cppFlags
,c/c++编译标记。External Native Build Issues
Build command failed.
Error while executing 'C:\Users\Administrator\AppData\Local\Android\Sdk\cmake\3.6.3155560\bin\cmake.exe' with arguments {-HE:\Source\GitHub\JniTestDemo\app -BE:\Source\GitHub\JniTestDemo\app\.externalNativeBuild\cmake\release\armeabi-v7a -GAndroid Gradle - Ninja -DANDROID_ABI=armeabi-v7a -DANDROID_NDK=C:\Users\Administrator\AppData\Local\Android\Sdk\ndk-bundle -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=E:\Source\GitHub\JniTestDemo\app\.externalNativeBuild\cmake\release\obj\armeabi-v7a -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=C:\Users\Administrator\AppData\Local\Android\Sdk\cmake\3.6.3155560\bin\ninja.exe -DCMAKE_TOOLCHAIN_FILE=C:\Users\Administrator\AppData\Local\Android\Sdk\ndk-bundle\build\cmake\android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=15 -DCMAKE_CXX_FLAGS=-std=c++11 -D__cplusplus=201103L -fvisibility=hidden}
-- Configuring incomplete, errors occurred!
See also "E:/Source/GitHub/JniTestDemo/app/.externalNativeBuild/cmake/release/armeabi-v7a/CMakeFiles/CMakeOutput.log".
CMake Error at C:/Users/Administrator/AppData/Local/Android/sdk/cmake/3.6.3155560/android.toolchain.cmake:356 (message):
Invalid Android NDK revision (should be 12): 13.1.3345770.
Call Stack (most recent call first):
.externalNativeBuild/cmake/release/armeabi-v7a/CMakeFiles/3.6.0-rc2/CMakeSystem.cmake:6 (include)
CMakeLists.txt
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
Error:executing external native build for cmake E:\Source\GitHub\JniTestDemo\app\CMakeLists.txt
Build command failed.
Error while executing 'C:\Users\Administrator\AppData\Local\Android\Sdk\cmake\3.6.3155560\bin\cmake.exe' with arguments {-HE:\Source\GitHub\JniTestDemo\app -BE:\Source\GitHub\JniTestDemo\app\.externalNativeBuild\cmake\debug\armeabi-v7a -GAndroid Gradle - Ninja -DANDROID_ABI=armeabi-v7a -DANDROID_NDK=C:\Users\Administrator\AppData\Local\Android\Sdk\ndk-bundle -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=E:\Source\GitHub\JniTestDemo\app\.externalNativeBuild\cmake\debug\obj\armeabi-v7a -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=C:\Users\Administrator\AppData\Local\Android\Sdk\cmake\3.6.3155560\bin\ninja.exe -DCMAKE_TOOLCHAIN_FILE=C:\Users\Administrator\AppData\Local\Android\Sdk\ndk-bundle\build\cmake\android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=15 -DCMAKE_CXX_FLAGS=-std=c++11 -D__cplusplus=201103L -fvisibility=hidden}
-- Configuring incomplete, errors occurred!
See also "E:/Source/GitHub/JniTestDemo/app/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeOutput.log".
CMake Error at C:/Users/Administrator/AppData/Local/Android/sdk/cmake/3.6.3155560/android.toolchain.cmake:356 (message):
Invalid Android NDK revision (should be 12): 13.1.3345770.
Call Stack (most recent call first):
.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/3.6.0-rc2/CMakeSystem.cmake:6 (include)
CMakeLists.txt
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
Error:executing external native build for cmake E:\Source\GitHub\JniTestDemo\app\CMakeLists.txt
出现该问题的修复方案:
1,将NDK的版本修改为需要的版本,这里修改为12
2,修改SDK目录/cmake/cmakeversion/android.toolchain.cmake文件,将NDK版本修改为所使用的版本:
cmake_minimum_required(VERSION 3.6.0)
set(ANDROID_NDK_REVISION 13)
官方介绍CMake: https://developer.android.com/studio/projects/add-native-code.html
Cmake Api 官方帮助文档:https://cmake.org/cmake/help/latest/manual/cmake-commands.7.html
Android Jni Tips: https://developer.android.com/training/articles/perf-jni.html
CMake google: https://developer.android.com/ndk/guides/cmake.html