I have a library in C called mylib in the folder jniLibs/your_architecture/mylib.so
In Java, to load the library, you just have to type that code in your source:
static {
System.loadLibrary("mylib");
}
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.
If this can help someone, here how to load a library in native code c/c++ :
1 - to avoid
java.lang.UnsatisfiedLinkError: dlopen failed:
add this to the build.gradle intoandroid
block :Assuming that your lib files are in
( depending where is your
jniLibs
folder, in my case it is located atapp/src/main/
but the most time it is inapp/
)2 - In your CMakeList, add you library as SHARED and IMPORTED by adding the following block :
3 - Add target properties to locate you lib mylib.so by adding the following block :
We back forward (/../) here because we concidere that CMakeLists.txt is in
Otherwise, the most time it is in
app/
folder, in this case, we don't need to back forward.So we have to back to main/ before going into jniLibs/
4 - Add you lib mylib to your target link libraries :
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"
You can now call your methods
[namespace::]method(args...)
Extra links :
PROJECT_SOURCE_DIR
CMAKE_SOURCE_DIR
Q : Are CMAKE_SOURCE_DIR and PROJECT_SOURCE_DIR the same in CMake?