最近研究了下android studio 使用cmake编译CPP代码,
之前走了不少弯路,现在保存一个配置模板,方便以后复用。
1,首先需要在app build.gradle中添加cmake的支持
在defaultConfig{}中,增加如下代码: 指定CMAKE的参数选项和 支持的CPU架构
defaultConfig {
applicationId "net.johnhany.opencv_ndk"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11", "-frtti", "-fexceptions"
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
}
}
}
2,在android{}中,指定需要依赖的第三方库所在的位置:有两种方式引用
一,库在项目之外 这样指定:
sourceSets {
main {
// jniLibs.srcDirs = ['E:/dev-lib/OpenCV-android-sdk/sdk/native/libs']
jniLibs.srcDirs = ['D:\\AsWorkspace\\OpenCV-android-sdk\\sdk\\native\\libs']
}
}
二,库在项目内,需要把库复制到指定目录:
sourceSets.main { jniLibs.srcDirs = ['src/main/jniLibs'] }
3,在android{}中自定依赖的makeList文件名:
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
4,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的版本号
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.
#非必须
set(CMAKE_VERBOSE_MAKEFILE on)
#指定库所在的目录 和build.gradle一致
#set(ocvlibs D:/AsWorkspace/OpenCV-android-sdk/sdk/native/libs)
set(ocvlibs "${CMAKE_SOURCE_DIR}/src/main/jniLibs")
#指定头文件所在的目录 可以指定项目外路径和项目内路径
#include_directories(D:/AsWorkspace/OpenCV-android-sdk/sdk/native/jni/include)
include_directories(${CMAKE_SOURCE_DIR}/src/main/jni/include)
#指定需要引用的第三方库
add_library(libopencv_java3 STATIC IMPORTED )
set_target_properties(libopencv_java3 PROPERTIES
IMPORTED_LOCATION "${ocvlibs}/${ANDROID_ABI}/libopencv_java3.so")
add_library(libopencv_core SHARED IMPORTED )
set_target_properties(libopencv_core PROPERTIES
IMPORTED_LOCATION "${ocvlibs}/${ANDROID_ABI}/libopencv_core.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/jni/native_processing.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.
#添加NDK库,变量名log-lib = log
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 android log libopencv_java3 libopencv_core
# Links the target library to the log library
# included in the NDK.
${log-lib} )
本文详细介绍如何在Android Studio中使用CMake编译CPP代码,包括配置appbuild.gradle支持CMake、设置第三方库路径、定义CMakeLists.txt等内容。

1万+

被折叠的 条评论
为什么被折叠?



