如何将本机库加载到本机 android 代码中?(安卓工作室) [英] How to load native library into native android code? (AndroidStudio)

查看:35
本文介绍了如何将本机库加载到本机 android 代码中?(安卓工作室)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件夹中有一个名为 mylib 的 C 库jniLibs/your_architecture/mylib.so

I have a library in C called mylib in the folder jniLibs/your_architecture/mylib.so

在 Java 中,要加载库,您只需在源代码中键入该代码:

In Java, to load the library, you just have to type that code in your source:

static {
  System.loadLibrary("mylib");
}

但是您如何以原生 C 代码(在 Android Studio 中)加载库?我补充说该库不使用 JNI 约定,它是一个普通的共享库.

But how can you load the library in a native C code (in Android Studio) ? I add that the library don't use the JNI conventions, it's a normal shared library.

推荐答案

如果这对某人有帮助,这里是如何在本机代码 c/c++ 中加载库:

If this can help someone, here how to load a library in native code c/c++ :

1 - 避免 java.lang.UnsatisfiedLinkError: dlopen failed: 将此添加到 build.gradle 到 android 块中:

1 - to avoid java.lang.UnsatisfiedLinkError: dlopen failed: add this to the build.gradle into android block :

sourceSets {
    main {
        jniLibs.srcDirs = ['src/main/jniLibs']
    }
}

假设你的lib文件在

src/main/jniLibs/{架构}/

src/main/jniLibs/{Architecture}/

(取决于你的 jniLibs 文件夹在哪里,在我的例子中它位于 app/src/main/ 但大多数时候它在 app/)

( depending where is your jniLibs folder, in my case it is located at app/src/main/ but the most time it is in app/)

2 - 在您的 CMakeList 中,通过添加以下块将您的库添加为 SHAREDIMPORTED :

2 - In your CMakeList, add you library as SHARED and IMPORTED by adding the following block :

add_library(mylib SHARED
            IMPORTED
            )

<小时>

3 - 添加目标属性以通过添加以下块来定位您的 lib mylib.so:


3 - Add target properties to locate you lib mylib.so by adding the following block :

set_target_properties( mylib PROPERTIES 
IMPORTED_LOCATION 
${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/mylib.so
)

我们在此处返回 (/../) 因为我们认为 CMakeLists.txt 在

We back forward (/../) here because we concidere that CMakeLists.txt is in

src/main/cpp/

src/main/cpp/

否则,大部分时间都在app/文件夹中,在这种情况下,我们不需要后退.

Otherwise, the most time it is in app/ folder, in this case, we don't need to back forward.

所以在进入jniLibs/之前我们必须回到ma​​in/

So we have to back to main/ before going into jniLibs/

4 - 将您的 lib mylib 添加到您的目标链接库:

4 - Add you lib mylib to your target link libraries :

target_link_libraries(native-library-jni
                      ..
                      ..
                      mylib
                      ..
                      ..
                      )

<小时>5 - 最后,要调用您的 lib mylib.so 的方法,您必须创建/复制包含这些方法签名的标头并将其包含在您的源文件中:#include "mylib.h"


5 - Finally, to call methods of your lib mylib.so, you have to create/copy-past the header containing these methods signatures and include it in your source file : #include "mylib.h"

您现在可以调用您的方法 [namespace::]method(args...)

You can now call your methods [namespace::]method(args...)

<小时>额外链接:

PROJECT_SOURCE_DIR

CMAKE_SOURCE_DIR

问:CMake 中的 CMAKE_SOURCE_DIR 和 PROJECT_SOURCE_DIR 是否相同?

这篇关于如何将本机库加载到本机 android 代码中?(安卓工作室)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆