问题
I want to add another lib into android-ndk hello-libs example.
In CMakeLists.txt, I add:
# this is from the hello-libs sample code
add_library(lib_gperf SHARED IMPORTED)
set_target_properties(lib_gperf PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/gperf/lib/${ANDROID_ABI}/libgperf.so)
########## I add this after the sample code: ###########
add_library(lib_py SHARED IMPORTED)
set_target_properties(lib_py PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/gperf/lib/${ANDROID_ABI}/libpython.so)
And this:
target_link_libraries(
hello-libs
android
lib_gperf
#### this line ######
lib_py
log)
And copy libpython.so in the directory where libgperf.so located:
Also copy the python headers into the include directory:
When I click the run button:
java.lang.UnsatisfiedLinkError: dlopen failed: library "/Users/finn/Downloads/hello-libs/app/src/main/cpp/../../../../distribution/gperf/lib/arm64-v8a/libpython.so" not found
at java.lang.Runtime.loadLibrary0(Runtime.java:1016)
at java.lang.System.loadLibrary(System.java:1657)
at com.example.hellolibs.MainActivity.<clinit>(MainActivity.java:36)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2931)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1620)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:173)
at android.app.ActivityThread.main(ActivityThread.java:6698)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
The path exists in my computer, but why the apk use my computer path, but not the android device path?
And I use the Android device file explorer, the lib is in the directory:
Then how can I make the apk use the right path?
Or I miss something to add?
回答1:
I encounter the exact same issue and I discovered that when the .so file is built, the library link for the python library is wrong while the link for gperf library is ok.
Link error for python library
Both libraries are imported with the exact same method in the cmake so it doesn't make sense.
I friend of mine told me it is a bug from ninja and provide a "turn-around" solution. You have to import the python library as if it was an Android-NDK provided one (like Android or Log)
Remove the code you added for lib_py and change target_link_library to
target_link_libraries(
hello-libs
android
lib_gperf
python //The name of the library libpython.so
log
)
You still need to provide the headers
target_include_directories( pyjni PRIVATE ${python_DIR}/include )
Refresh the C files and attempt to compile. On first try, it will tell you that the compiler cannot find the python library on a specific location. Go to that location and add your library
In my case
Linking C shared library [ ... ] D:/Android/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin\ld: cannot find -lpython
Add the libpython.so file to the specified location, in my case D:/Android/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/aarch64-linux-android/4.9.x/ and recompile.
It should works, in my case, you can see that the link is now only libpython.so ( removed libgperf.so in this case)
Resulting library link in .so file
Please note, in my case, I compile for arm64-v8a only. I do not now if it will works with multiple ABI.
Obviously, this is not a good solution. I hope my answer will draw more attention on this issue and somebody will provide a real solution to avoid such tweaks
回答2:
Assume path /Users/finn/Downloads/hello-libs/app/src/main/cpp/../../../../distribution/gperf/lib is correct, then you can configure your JNI libs like below:
sourceSets {
release {
jniLibs.srcDirs += ["/Users/finn/Downloads/hello-libs/app/src/main/cpp/../../../../distribution/gperf/lib"]
}
debug {
jniLibs.srcDirs += ["/Users/finn/Downloads/hello-libs/app/src/main/cpp/../../../../distribution/gperf/lib"]
}
}
Try to change /Users/finn/Downloads/hello-libs/app/src/main/cpp/../../../../distribution/gperf/lib if it is not your correct path to the jni libs.
来源:https://stackoverflow.com/questions/55083734/java-lang-unsatisfiedlinkerror-dlopen-failed-library-users-not-found