此篇文章的目的:
将C/C++的源码直接编译成静态库,只能提供给底层调用。
注:比较适用于将第三方开源库编译成静态库,也可以将自己写的源码编译成静态库,给自己或其他人的底层调用。
本文目录:
1.开发环境配置
2.编译静态库方法
3.调用静态库方法
4.示例
1.开发环境配置
环境配置:
开发工具:Android Studio 3.0.1
Android SDK Tools:额外勾选CMake、LLDB、NDK三个选项
JDK版本:JDK 1.8
NDK版本:18.1.5063045
编译方式:CMake
第三方C/C++开源库:cJSON
新建Android项目配置:
Include C++ Support:进行配置NDK环境,勾选。主要是自动创建cpp目录和CMakeLists.txt,并自动在gradle中进行了ndk配置
C++ Standard:选择使用哪种C++标准,选择Toolchain Default。会使用默认的CMake设置。
Exceptions Support:启用对C++异常处理的支持,勾选。
Runtime Type Information Support:启用对运行时类型信息的支持,勾选。
2.编译静态库方法
详见Demo1
(1)先按照上述Android项目配置新建工程。
(2)以cJSON开源库为例,github上下载最新版本的cJSON源码,放入到项目的cpp目录下:
(3)配置CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.4.1)
#打印LOG
message(STATUS "Cmake build type is: "${CMAKE_BUILD_TYPE})
message(STATUS "Cmake build android abi is: "${ANDROID_ABI})
#设置静态库和动态库的输出路径
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/output/libs/${CMAKE_BUILD_TYPE}/${ANDROID_ABI})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/output/libs/${CMAKE_BUILD_TYPE}/${ANDROID_ABI})
#设置变量
set(LOCAL_PATH ${CMAKE_SOURCE_DIR}/src/main/cpp)
#引入头文件
include_directories(
${LOCAL_PATH}/libs/source/cjson/
)
#增加静态库
add_library( # Sets the name of the library.
cjson
# Sets the library as a shared library.
STATIC
# Provides a relative path to your source file(s).
${LOCAL_PATH}/libs/source/cjson/cJSON.c
${LOCAL_PATH}/libs/source/cjson/cJSON_Utils.c
)
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 )
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 )
target_link_libraries( # Specifies the target library.
native-lib
cjson #链接该静态库进行测试, 如果不测试, 则不需要链接
# Links the target library to the log library
# included in the NDK.
${log-lib} )
(4)配置build.gradle文件,主要是配置编译哪种CPU类型的库,使用abiFilters,有些复杂的C/C++源码在编译不同CPU类型的库时,还需要一些其他的配置,使用arguments,具体配置根据需求进行更改,配置如下:
android {
...
defaultConfig {
...
externalNativeBuild {
cmake {
//arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang"
//不写此行则默认使用CMake配置的变量列表
cppFlags "-frtti -fexceptions"
//abiFilters /*"armeabi",*/ "armeabi-v7a", "arm64-v8a", "x86", "x86_64" //不写此行则默认编译后面4个CPU类型的库
}
}
}
...
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
(5)在命令行使用gradlew assemble命令进行编译,静态库会输出在设置的文件夹中。
3.调用静态库方法
详见Demo2
(1)先按照上述Android项目配置新建工程。
(2)以cJSON开源库为例,将编译好的静态库放入到项目的cpp目录下:
(3)引入头文件,并使用C++类重新进行了封装:
#ifndef __Cjson_Utils_H__
#define __Cjson_Utils_H__
//此处导入C++的头文件
//#include "CObjectUtils.h"
#ifdef __cplusplus
extern "C" {
#endif
//此处导入C的头文件
#include "cJSON.h"
#include "cJSON_Utils.h"
#ifdef __cplusplus
};
#endif
class CCjsonUtils{
public:
CCjsonUtils();
virtual ~CCjsonUtils();
static const char* getCjsonVersion();
};
#endif
(4)配置CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.4.1)
#打印LOG
message(STATUS "Cmake build type is: "${CMAKE_BUILD_TYPE})
message(STATUS "Cmake build android abi is: "${ANDROID_ABI})
#设置变量
set(LOCAL_PATH ${CMAKE_SOURCE_DIR}/src/main/cpp)
#引入头文件
include_directories(
${LOCAL_PATH}/libs/include/cjson/
${LOCAL_PATH}/cjsonutils/
)
#导入静态库
add_library(cjson STATIC IMPORTED)
set_target_properties(cjson PROPERTIES IMPORTED_LOCATION ${LOCAL_PATH}/libs/${ANDROID_ABI}/libcjson.a )
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
${LOCAL_PATH}/cjsonutils/CCjsonUtils.cpp #创建的C++工具类, 用于对C源码进行封装, 便于简化接口
)
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 )
target_link_libraries( # Specifies the target library.
native-lib
cjson #链接静态库
# Links the target library to the log library
# included in the NDK.
${log-lib} )
(5)配置build.gradle,使用默认的即可,然后就可以正常调用了。
4.示例
Demo1地址:AndroidNdkCompileStaticLibrary
Demo2地址:AndroidNdkInvokeStaticLibrary