一、目录结构
根据我的个人管理习惯我喜欢在app目录下的CMakeLists引入到CPP目录下如果有多个项目每个项目构建一个CMakeLists分开管理。
二、app目录下的CMakeLists语法
cmake_minimum_required(VERSION 3.4.1)
#设置编译完成后so库生成的路径为 app/libs(可根据自己的习惯设置)
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})
#引入src/main/cpp/testjni目录下的CMakeLists
ADD_SUBDIRECTORY(src/main/cpp/testjni)
#引入src/main/cpp/testjni2目录下的CMakeLists
ADD_SUBDIRECTORY(src/main/cpp/testjni2)
三、项目下的CMakeLists语法,以tesijni为例
#引入头文件目录
include_directories(include)
# 获取当前目录下的所有的文件(目录)的路径并保存到 all_source 变量中
file(GLOB all_source *.cpp *.c )
#引入all_source变量中的源文件编译为native-lib的库
add_library( native-lib
SHARED
${all_source} )
#引入libs目录中第三方的libopencv_java.so库
add_library(lib_opencv
SHARED
IMPORTED)
set_target_properties(lib_opencv
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libopencv_java.so)
find_library( log-lib
log )
target_link_libraries( native-lib
${log-lib}
lib_opencv)
四、build.gradle配置
defaultConfig {
applicationId "com.taiyao.tongzi"
minSdkVersion 17
targetSdkVersion 25
versionCode 3
versionName "2.0.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions -ffast-math -O3"
arguments '-DANDROID_STL=gnustl_static' //支持C++异常处理标准模板快
arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang"
}
}
ndk {
abiFilters 'armeabi-v7a'
}
}
sourceSets{
main{
jniLibs.srcDirs=['libs']
}
}
externalNativeBuild {
cmake {
path 'CMakeLists.txt'
}
}