构建包含静态链接库的Android Native Library项目
创建Android Native Library项目
添加.a文件和头文件
假设这里有一个tboxtest的C/C++项目,
- 在cpp文件夹下新建include文件夹(为方便管理而已,文件夹名随意),在include文件夹下创建指向tboxtest项目中存放头文件的文件夹的链接(也可以整个复制过来,但如此同步比较麻烦);
- 在cpp文件夹下新建jniLibs文件夹,在jniLibs文件夹下新建arm64-v8a文件夹(如果需要其他架构的.a文件,则添加对应架构的文件夹,如需要armeabi-v7a架构,则添加armeabi-v7a文件夹),在arm64-v8a文件夹下创建指向tboxtest项目编译后的arm64-v8a架构的.a文件的链接(同理也可以复制.a文件过来)。
效果大概如图:
编写CMakeLists.txt文件
- 配置头文件路径
include_directories(include)
- 配置库文件
add_library(tboxtest STATIC IMPORTED)
set_target_properties(
tboxtest
PROPERTIES
IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libtboxtest.a)
- 将库添加的链接目标中
target_link_libraries( # Specifies the target library.
nativelib2
tboxtest
# Links the target library to the log library
# included in the NDK.
${log-lib})
整体上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.18.1)
# Declares and names the project.
project("nativelib2")
include_directories(include)
# 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.
nativelib2
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
nativelib2.cpp)
add_library(tboxtest STATIC IMPORTED)
set_target_properties(
tboxtest
PROPERTIES
IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libtboxtest.a)
# 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.
nativelib2
tboxtest
# Links the target library to the log library
# included in the NDK.
${log-lib})
如果有多个.a文件,重复上述2和3步骤即可;
启用JNI调试
在引用该模块的项目的build.gradle文件中添加如下代码:
buildTypes {
release {
......
}
debug {
jniDebuggable true
packagingOptions {
doNotStrip '**/*.so'
}
}
}
- 原文作者:岁寒
- 原文链接:http://www.suihanime.com/post/%E5%8E%9F%E7%94%9F%E5%BC%80%E5%8F%91%E6%8A%80%E6%9C%AF/%E6%9E%84%E5%BB%BA%E5%8C%85%E5%90%AB%E9%9D%99%E6%80%81%E9%93%BE%E6%8E%A5%E5%BA%93%E7%9A%84Android-Native-Library%E9%A1%B9%E7%9B%AE/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。