NDK--CMakeLists配置第三方so库

2021-01-10 17:20:52

参考地址 NDK--CMakeLists配置第三方so库

当我们创建一个NDK工程时,会自动创建一个CMakeLists.txt的文件,在AS中c++的编译器是使用LLVM,规则为cmake,今天来学习下cmake的基本套路
首先,我创建了两个NDK工程,第一个工程为lib,为第二个工程提供so库

我们修改cpp文件,新增一个求和方法
#include <jni.h>#include <string>int sum(int a, int b) {
    return a + b;}
由于我们这个工程要提供so库,所以在CMakeLists中改下编译出来的so库名称:test-lib

编译下,找到生成的so库文件夹,复制到第二个工程

如果想要指定平台可以在gradle中配置:

在defaultConfig目录里面 ndk { abiFilters "armeabi","x86" }

复制到libs下

AS中默认存放so库的目录需要在src/main中创建一个jniLibs的文件夹,也可以通过gradle配置,指定目录

在app.gralde中的android目录下 sourceSets.main { jniLibs.srcDirs = ['libs'] jni.srcDirs = [] }

这边使用的是libs目录作为so库的存放目录,接下来我们来配置第二个工程的CMakeLists
1.首先,为了以后方便使用,我们为so库的路径设置一个别名
#设置so库路径set(my_lib_path ${CMAKE_SOURCE_DIR}/../../../libs)
${CMAKE_SOURCE_DIR}为CMakeLists文件的当前路径,以后我们就可以直接使用my_lib_path了
2.第二步,我们配置导入的so库
#将第三方库作为动态库引用add_library(test-lib
        SHARED
        IMPORTED)
这边我们只需要修改库的名称(test-lib)就可以了,其他的复制粘贴
3.第三步,配置第三方库的路径,这边就要用到我们开始定义的my_lib_path别名了
#指定第三方库的绝对路径set_target_properties(test-lib
        PROPERTIES IMPORTED_LOCATION
        ${my_lib_path}/${ANDROID_ABI}/libtest-lib.so)
同样的,我们只需要关注上一步定义好的名称和连接的so库的绝对路径,另外,关于${ANDROID_ABI}的说明:系统会自动根据apk安装时的平台,自动将相应平台下的so库导入
target_link_libraries( # Specifies the target library.
        native-lib
        test-lib
        
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})
完整CMakeLists:
cmake_minimum_required(VERSION 3.4.1)#设置so库路径set(my_lib_path ${CMAKE_SOURCE_DIR}/libs)#将第三方库作为动态库引用add_library(test-lib
        SHARED
        IMPORTED)#指定第三方库的绝对路径set_target_properties(test-lib
        PROPERTIES IMPORTED_LOCATION
        ${my_lib_path}/${ANDROID_ABI}/libtest-lib.so)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).
        native-lib.cpp)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)target_link_libraries( # Specifies the target library.
        native-lib
        test-lib
        
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

接下来,我们开始使用导入的库中的方法:求和方法

1.首先,创建一个.h文件,声明导入方库的方法:

//// Created by aruba on 2020/4/13.//#ifndef NDKAPPLICATION_TEST_LIB_H
#define NDKAPPLICATION_TEST_LIB_H//申明外部 函数 外部属性extern int sum(int a, int b);#endif //NDKAPPLICATION_TEST_LIB_H
2.在需要使用的cpp中引入头文件,并调用
#include <jni.h>#include <string>#include <android/log.h>#include <assert.h>#include "test-lib.h"#define TAG "C++"#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))extern "C"JNIEXPORT jstring JNICALL native_stringFromJNI(JNIEnv *env, jclass type) {

    return env->NewStringUTF("C++");}JNIEXPORT jint JNICALL native_sum(JNIEnv *env, jclass type, jint a, jint b) {

    return sum(a, b);}static const JNINativeMethod gMethods[] = {
        {
                "stringFromJNI", "()Ljava/lang/String;", (void *) native_stringFromJNI        },
        {
                "sum", "(II)I", (void *) native_sum        }};static int registerNatives(JNIEnv *env) {
    LOGI("registerNatives begin");
    jclass clazz;
    //找到java的类
    clazz = env->FindClass("com/aruba/ndkapplication/JniUtils");

    if (clazz == NULL) {
        LOGI("clazz is null");
        return JNI_FALSE;
    }

    if (env->RegisterNatives(clazz, gMethods, NELEM(gMethods)) < 0) {
        LOGI("RegisterNatives error");
        return JNI_FALSE;
    }

    return JNI_TRUE;}JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
    LOGI("jni_OnLoad begin");

    JNIEnv *env = NULL;

    if (vm->GetEnv((void **) &env, JNI_VERSION_1_4) != JNI_OK) {
        LOGI("ERROR: GetEnv failed\n");
        return -1;
    }

    assert(env != NULL);

    registerNatives(env);

    return JNI_VERSION_1_4;}
在java中调用
package com.aruba.ndkapplication;public class JniUtils {
    static {
        System.loadLibrary("native-lib");
    }
    
    public static native String stringFromJNI();

    public static native int sum(int a, int b);}
public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(JniUtils.sum(10, 20) + "");
    }}


  • 2019-12-05 17:13:40

    JS模板工具lodash.template的简单用法

    lodash是从underscore分支的一个项目,之前我写了一篇JS模板工具underscore.template的简单用法,lodash跟underscore很相似,这也简单介绍一下lodash的template方法。 先把underscore的文章中用过的代码贴过来,把underscore的js文件换成lodash的js,其他一字不改,然后我们试试:

  • 2019-12-06 10:47:29

    date-fns日期工具的使用方法详解

    isToday() 判断传入日期是否为今天 isYesterday() 判断传入日期是否为昨天 isTomorrow() 判断传入日期是否为 format() 日期格式化 addDays() 获得当前日期之后的日期 addHours() 获得当前时间n小时之后的时间点 addMinutes() 获得当前时间n分钟之后的时间 addMonths() 获得当前月之后n个月的月份 subDays() 获得当前时间之前n天的时间 subHours() 获得当前时间之前n小时的时间 subMinutes() 获得当前时间之前n分钟的时间 subMonths() 获得当前时间之前n个月的时间 differenceInYears() 获得两个时间相差的年份 differenceInWeeks() 获得两个时间相差的周数 differenceInDays() 获得两个时间相差的天数 differenceInHours() 获得两个时间相差的小时数 differenceInMinutes() 获得两个时间相差的分钟数

  • 2019-12-06 10:49:39

    npm 查看源 换源

    npm,cnpm,查看源,切换源,npm config set registry https://registry.npmjs.org

  • 2019-12-06 11:01:31

    npm发布包流程详解 有demo

    npm发布包步骤,以及踩过的坑(见红颜色标准): 1.注册npm账号,并完成Email认证(否则最后一步提交会报Email错误) 2.npm添加用户或登陆:npm adduser 或 npm login

  • 2019-12-06 13:16:18

    vue mixins组件复用的几种方式

    最近在做项目的时候,研究了mixins,此功能有妙处。用的时候有这样一个场景,页面的风格不同,但是执行的方法,和需要的数据非常的相似。我们是否要写两种组件呢?还是保留一个并且然后另个一并兼容另一个呢? 不管以上那种方式都不是很合理,因为组件写成2个,不仅麻烦而且维护麻烦;第二种虽然做了兼容但是页面逻辑造成混乱,必然不清晰;有没有好的方法,有那就是用vue的混合插件mixins。混合在Vue是为了提出相似的数据和功能,使代码易懂,简单、清晰。

  • 2019-12-06 13:26:30

    vue的mixins混入合并规则

    混入minxins:分发vue组件中可复用功能的灵活方式。混入对象可以包含任意组件选项。组件使用混入对象时,所有混入对象的选项将混入该组件本身的选项。

  • 2019-12-06 16:50:34

    Intellij idea 如何关闭无用的提示

    Linux:Settings —> Editor —> Inspections —> General —> Duplicated Code Mac:Preferences --> Editor —> Inspections —> General —> Duplicated Code fragment 将对应的勾去掉。

  • 2019-12-09 15:36:56

    神秘的 shadow-dom 浅析,shadow-root

    顾名思义, shadow-dom,直译的话就是 影子dom ?我觉得可以理解为潜藏在黑暗中的 DOM 结构,也就是我们无法直接控制操纵的 DOM 结构。前端同学经常用开发者工具的话,查看 DOM 结构的时候,肯定看到过下面这样的结构: