android studio cmake 编译 ndk 代码 项目配置
来源:互联网 发布:淘宝刹那数码2店怎么样 编辑:程序博客网 时间:2024/05/16 10:34
最近研究了下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 = logfind_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} )
阅读全文
0 0
- android studio cmake 编译 ndk 代码 项目配置
- android studio 2.2 使用cmake编译NDK
- Android Studio配置CMake开发NDK
- Android Studio NDK编译配置
- Android studio ndk配置cmake开发native C
- Android Studio NDK项目编译备忘录
- Android-Ndk Cmake编译JNI
- Android 使用CMake 编译NDK
- Android Studio NDK 代码 Source Insight调试 (NDK 目前开发方案 | NDK 编译 | 导入 so 库 | 项目编码转换)
- Android Studio编译NDK app gradle配置
- Android Studio配置NDK编译环境
- Android Studio NDK JNI编译环境 配置
- Android Studio快捷键、配置 Android Studio、cmake配置Android原生项目、cmake命令
- Android Studio NDK 编译
- Android Studio NDK编译.
- Android studio编译NDK
- Android studio ndk 编译
- Android Studio NDK 编译
- Sequoiadb分布式数据库调整内核参数
- python基础
- mongodb概念--文档、集合、数据库、shell
- zipkin c/c++库编译及环境搭建
- 计蒜客 Our Journey of Dalian Ends 拆点+最小费用最大流
- android studio cmake 编译 ndk 代码 项目配置
- Android原生项目集成React Native踩坑记
- JAXB validation failed because the necessary plug-ins were unavailable
- Java中Map接口的遍历
- Spring的事务管理器用法
- 初始化sentry报错/CDH中添加sentry服务失败----解决方法
- spring boot入门学习
- JS二级联动选择框动态创建和编辑实现
- unity 判断物品是否出现在角色面前