JNI 相关CmakeList.txt 配置

首先需要进入当前Module的Gradle.Build  设置编译CmakeList  及 cpp的编译环境
defaultConfig {

//        externalNativeBuild {
////            cmake {
////                cppFlags "-frtti -fexceptions"
////            }
//            cmake {
//                cppFlags "-std=c++11 -frtti -fexceptions" // 根据情况设置
//                abiFilters 'arm64-v8a' // 根据情况设置 'x86', 'armeabi-v7a',  目前orc 只支持识别
//            }
//        }
}
android{
//方法 2
//    externalNativeBuild {
//        cmake {
//            path "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.
#设置OpenCv sdk的路径变量 eg:   Users/apple/Downloads/OpenCV-android-sdk/
set(pathToOpenCv E:/AS/xxx/OpenCV-android-sdk)
#so库的输出路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
   ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})

cmake_minimum_required(VERSION 3.4.1)
#支持-std=gnu++11
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

#配置加载native依赖
include_directories(${pathToOpenCv}/sdk/native/jni/include)

#动态方式加载
add_library(lib_opencv STATIC IMPORTED )

#引入libopencv_java3.so文件
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so )

# 定义添加第三方so的路径 path_to_so
set(path_to_so E:/AS/xxx/app/libs)
#动态方式加载
add_library(libspen STATIC IMPORTED )
#设置 libseetanet2.so 库
set_target_properties(libspen PROPERTIES IMPORTED_LOCATION
        ${path_to_so}/${ANDROID_ABI}/xxx.so)


#官方配置 隐藏
#find_package(OpenCV REQUIRED)



# 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.
             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
             #src/main/cpp/xxx.h
            )

# 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.
                       native-lib
                       OpenSLES
                       libspen
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} android -ljnigraphics lib_opencv)
### 关于 Android JNICMakeLists 配置 对于希望了解如何配置 Android NDK 使用 Java Native Interface (JNI) 并通过 CMake 构建原生代码的开发者来说,理解两者之间的交互至关重要。当涉及到设置 `CMakeLists.txt` 文件来支持 Android Studio 中的 JNI 编程时,有几个关键点需要注意。 #### 设置环境变量并创建项目结构 为了使 Android Studio 能够识别本地模块,在项目的根目录下应当有一个合适的文件夹用于存放 C/C++ 源文件以及相应的构建脚本 `CMakeLists.txt`[^3]。 #### 创建 CMakeLists.txt 文件 以下是典型的 `CMakeLists.txt` 的基本框架: ```cmake # 定义最低版本号 cmake_minimum_required(VERSION 3.4.1) # 添加源文件路径 add_library( native-lib SHARED src/main/cpp/native-lib.cpp ) # 寻找预编译库(如果有的话) find_library( log-lib log ) # 将log-lib链接至native-lib target_link_libraries( native-lib ${log-lib} ) ``` 这段简单的例子展示了怎样定义一个共享库 (`SHARED`) 和指定其对应的 .cpp 文件位置;同时也说明了如何找到像 logging 这样的标准安卓库,并将其连接到自定义的本地库上。 #### 处理头文件和外部依赖项 如果有额外的头文件或者第三方静态/动态库需要加入工程,则可以在 `CMakeLists.txt` 中进一步扩展: ```cmake include_directories(src/main/cpp/include/) link_directories(/path/to/thirdparty/libs) ``` 这允许将特定目录下的所有头文件纳入索引范围,并告知链接器去哪里寻找所需的二进制资源。 #### 支持多种 ABI 版本 为了让应用程序兼容不同的 CPU 架构,可以利用 Gradle 插件自动处理多ABI的支持问题。只需确保在 app/build.gradle 文件里适当配置即可: ```gradle android { ... defaultConfig { externalNativeBuild { cmake { cppFlags "" abiFilters 'armeabi-v7a', 'arm64-v8a' } } } externalNativeBuild { cmake { path "src/main/cpp/CMakeLists.txt" } } } ``` 上述片段指定了目标 ABIs 列表,并告诉 build system 去哪里读取 `CMakeLists.txt` 来执行实际的编译工作流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值