简体   繁体   English

如何将本机库加载到本机android代码中? (AndroidStudio)

[英]How to load native library into native android code? (AndroidStudio)

I have a library in C called mylib in the folder jniLibs/your_architecture/mylib.so 我在C中有一个名为mylib的库,位于文件夹jniLibs / your_architecture / mylib.so中

In Java, to load the library, you just have to type that code in your source: 在Java中,要加载库,只需在源代码中键入该代码即可:

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

But how can you load the library in a native C code (in Android Studio) ? 但是如何在本机C代码中加载库(在Android Studio中)? I add that the library don't use the JNI conventions, it's a normal shared library. 我补充说该库不使用JNI约定,它是一个普通的共享库。

If this can help someone, here how to load a library in native code c/c++ : 如果这可以帮助某人,那么这里是如何使用本地代码c / c ++加载库:

1 - to avoid java.lang.UnsatisfiedLinkError: dlopen failed: add this to the build.gradle into android block : 1-避免java.lang.UnsatisfiedLinkError: dlopen failed:将其添加到build.gradle到android块中:

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

Assuming that your lib files are in 假设您的lib文件在

src/main/jniLibs/{Architecture}/ 的src /主/ jniLibs / {架构} /

( depending where is your jniLibs folder, in my case it is located at app/src/main/ but the most time it is in app/ ) (根据您的jniLibs文件夹在哪里,在我的情况下,它位于app/src/main/但大多数时候在app/


2 - In your CMakeList, add you library as SHARED and IMPORTED by adding the following block : 2-在您的CMakeList中,通过添加以下代码块将您的库添加为SHAREDIMPORTED

add_library(mylib SHARED
            IMPORTED
            )

3 - Add target properties to locate you lib mylib.so by adding the following block : 3-通过添加以下块,添加目标属性以找到您的lib mylib.so

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

We back forward (/../) here because we concidere that CMakeLists.txt is in 我们在这里后退(/../),因为我们认为CMakeLists.txt位于

src/main/cpp/ 的src /主/ CPP /

Otherwise, the most time it is in app/ folder, in this case, we don't need to back forward. 否则,大多数情况下,它位于app/文件夹中,在这种情况下,我们不需要后退。

So we have to back to main/ before going into jniLibs/ 因此,在进入jniLibs /之前,我们必须先回到main /


4 - Add you lib mylib to your target link libraries : 4-将lib mylib添加到目标链接库:

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


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

You can now call your methods [namespace::]method(args...) 您现在可以调用您的方法[namespace::]method(args...)


Extra links : 额外链接:

PROJECT_SOURCE_DIR PROJECT_SOURCE_DIR

CMAKE_SOURCE_DIR CMAKE_SOURCE_DIR

Q : Are CMAKE_SOURCE_DIR and PROJECT_SOURCE_DIR the same in CMake? 问:CMake中的CMAKE_SOURCE_DIR和PROJECT_SOURCE_DIR是否相同?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM