在我们创建项目的时候,如果勾选 Include C++ Support ,就会在 main 的同级目录下生成一个CMakeLists.text
下面来一一介绍
# 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( # 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).
src/main/cpp/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.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
我们可以直接翻译来看:
# 有关使用Android工作室使用CMAGE的更多信息,
# 阅读文档: https://d.android.com/studio/projects/add-native-code.html
# 设置创建本地库所需的最小CMake版本
cmake_minimum_required(VERSION 3.4.1)
# 创建和命名库, 将其设置为STATIC 或者SHARED,
# 并提供其源代码的相对路径。
# 您可以定义多个库,CMake为您构建它们。
# 用Apk自动打包共享库.
add_library( # 设置库的名称.
native-lib
# 将库设置为共享库.
SHARED
# 为源文件提供相对路径.
src/main/cpp/native-lib.cpp
)
# 搜索指定的预构建库并将路径存储为变量。
# 因为CMake默认包含搜索路径中的系统库
# 您只需指定要添加的公共NDK库的名称即可.
# CMake 在完成构建之前验证该库是否存在
find_library( # 设置路径变量的名称.
log-lib
# 指定希望CMake定位的NDK库的名称.
log )
# 指定库CMake 应该链接到目标库.
# 你可以链接多个库 ,作为在这个构建脚本中定义的库,
# 预构建的第三方库或系统库.
target_link_libraries( # 指定目标库.
native-lib
# 将目标库链接到日志库
# 包含在NDK中.
${log-lib} )
解析:
1、制定CMake 所需的最小版本,有系统自动生成,无需更改
cmake_minimum_required(VERSION 3.4.1)
2、add_library():用来设置编译生成本地库的相关属性
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).
src/main/cpp/native-lib.cpp
)
native-lib :
生成动态库的名称
SHARE:
设置链接库的类型, 分为 :SHARE 动态链接库,SATTIC 静态链接库
src/main/cpp/native-lib.cpp:
你写的c/c++ 代码的相对路径。目前我的cpp 目录中有个natice-lib.cpp文件。如果,我又添加了一个 hello.cpp,则直接在下面添加、
add_library( # Sets the name of the library.
native-lib
SHARED
src/main/cpp/native-lib.cpp
src/main/cpp/hello.cpp
)
3、find_library():用来让我们加一些编译本地NDK库的时候所用的到一些依赖库.
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 )
log-lib:依赖库的别名
log: log 日志库
4、target_link_libraries(): 用来关联我们本地的库跟第三方的库
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
native-lib:
我们自己的库,和add_library 中的名称一致
${log-lib}:
需要关联的库的名称。
二、使用cmake 生成so库
CMakeLists.text 中添加 如下代码:
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})
编译运行后,会在目录中直接生成 .so
还可以 在app 下的 build.gradle中指定abi abiFilters
defaultConfig {
......
......
......
externalNativeBuild {
cmake {
cppFlags ""
abiFilters 'x86','armeabi-v7a';
}
}
}
如果不指定,会生成所有当前支持的 abi库
指定之后,只会生成响应版本的abi
注: 参考借鉴:https://blog.csdn.net/hktkfly6/article/details/79593127