JNI/NDK开发 配置CMakelist文件加载第三方 so

我们知道我们正常加载so文件都是通过 

 static {
        System.loadLibrary("native-lib");
    }

这是我们纯加载so来做项目,项目没有配置CMakelist文件,如果配置了CMakelist文件我们加载so就不是这样的了,加载方式如下:

1. 首先我们使用的CMakelist开发的所以项目中会有这两个文件:

2.如果我们要加载第三方so库文件如图:

其中include下面是opencv的h头文件,是so库的引用,也就是c代码的实现

3. so库引入之后就是配置CMakelist文件了

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html


#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
#判断编译器类型,如果是gcc编译器,则在编译选项中加入c++11支持
if(CMAKE_COMPILER_IS_GNUCXX)

    set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
    message(STATUS "optional:-std=c++11")
endif(CMAKE_COMPILER_IS_GNUCXX)


# 配置路径 so 的头文件(h)的路径引用
include_directories(src/main/jniLibs/include)
# Sets the minimum version of CMake required to build the native library.

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.

# 代表最终会在build中生成一个so文件名为native-lib//所以如果我们自己添加的第三方so包,要重新 add_library
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)


# 加载第三方的so库文件
add_library(
        opencvc7 #最终在build中生成的so名字
        SHARED
        IMPORTED)
set_target_properties(
        opencvc7 #最终在build中生成的so名字
        PROPERTIES IMPORTED_LOCATION
        ../../../../src/main/jniLibs/armeabi-v7a/libopencv_java4.so) #so的路径


# 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 opencvc7 # link到库

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

4.最后要在build.glide中过滤生成的so

   externalNativeBuild {
            cmake {
                cppFlags "-fexceptions"
                abiFilters "armeabi-v7a"  //控制生成的so架构,有armxx7,armxx64(必须要指定)
            }
        }

5. 第三方头文件引用的路径配置

在CMakelist中我们可以看到 

# 配置路径 so 的头文件(h)的路径引用
include_directories(src/main/jniLibs/include)

配置我们在jni代码中引用的h头文件路径:

#include <jni.h>
#include <string>
// 路径配置
#include <opencv2/opencv.hpp>
using namespace cv;

extern "C" JNIEXPORT jstring JNICALL
Java_com_safe_silent_opencvc_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";

    Mat test(2,2,CV_8UC3,Scalar(0,0,255));

    return env->NewStringUTF(hello.c_str());
}

https://github.com/WangRain1/JavatoC

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WangRain1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值