添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Android JNA UnsatisfiedLinkError: Native library (com/sun/jna/xxx/libjnidispatch.so) not found

Ask Question

Hey I am trying to set up a android studio project with JNA and libvirt java bindings.

This is what the project structure looks like so far.

enter image description here however the solution did not work for me

This is being tested not he emulator so far and the arm binaries have not been imported yet.

When trying to use Connect method in the libvirt java bindings I get this error.

There is a very similar post here

11-18 23:54:37.584 4182-4182/com.local.test E/AndroidRuntime: FATAL EXCEPTION: main
                                                           Process: com.local.test, PID: 4182
                                                           java.lang.UnsatisfiedLinkError: Native library (com/sun/jna/android-x86/libjnidispatch.so) not found in resource path (.)
                                                               at com.sun.jna.Native.loadNativeDispatchLibraryFromClasspath(Native.java:1039)
                                                               at com.sun.jna.Native.loadNativeDispatchLibrary(Native.java:999)
                                                               at com.sun.jna.Native.<clinit>(Native.java:191)
                                                               at com.sun.jna.Native.loadLibrary(Native.java:625)
                                                               at org.libvirt.jna.Libvirt.<clinit>(Unknown Source)
                                                               at org.libvirt.Library.<clinit>(Unknown Source)
                                                               at org.libvirt.Connect.<init>(Unknown Source)
                                                               at com.local.haris.MainActivity.onCreate(MainActivity.java:34)
                                                               at android.app.Activity.performCreate(Activity.java:6662)
                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                               at android.os.Looper.loop(Looper.java:154)
                                                               at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Notes: Android studio 3.0 Do not have the c++ SDK installed (didn't think I needed to since I am not compiling binaries) If am unsure if the libvirt java jar works on android. I tried to find information about this but failed to do. If someone could comment on this it would be appreciated.

although many days have passed since the question arose I found that adding the following will resolve the issue (of course use version of your choice):

implementation 'net.java.dev.jna:jna:5.8.0@aar'
                There are duplicate class errors at build-time. Another response to this question provides more detail.
– ReubenBeeler
                Aug 2, 2022 at 17:20

Include the libjnidispatch.so shared library for all the Android ABIs that your project supports.

  • Navigate to JNA libraries.
  • Under Version 4.5.0, download the zip archive
  • Unzip the package, navigate to jna-4.5.0/dist/ directory. libjnidispatch.so for different ABIs can be extracted from respective jar file. The mapping is as below illustrated in below table.

    | JNA ABI             | Android ABI   |
    | ------------------- | ------------- |
    | android-aarch64.jar | arm64-v8a     |
    | android-armv7.jar   | armeabi-v7a   |
    | android-x86-64.jar  | x86_64        |
    | android-x86.jar     | x86           |
    
  • Put the libjnidispatch.so into the mapped Android ABI folder, for example, arm64-v8a, armeabi-v7a, x86 and x86_64.
  • koby hershkovitz's answer of "just add @aar" results in duplicate classes:

    Duplicate class com.sun.jna.AltCallingConvention found in modules jetified-jna-5.8.0 (net.java.dev.jna:jna:5.8.0) and jetified-jna-5.8.0-runtime (net.java.dev.jna:jna:5.8.0)
    Duplicate class com.sun.jna.Callback found in modules jetified-jna-5.8.0 (net.java.dev.jna:jna:5.8.0) and jetified-jna-5.8.0-runtime (net.java.dev.jna:jna:5.8.0)
    Duplicate class com.sun.jna.Callback$UncaughtExceptionHandler found in modules jetified-jna-5.8.0 (net.java.dev.jna:jna:5.8.0) and jetified-jna-5.8.0-runtime (net.java.dev.jna:jna:5.8.0)
    Duplicate class com.sun.jna.CallbackParameterContext found in modules jetified-jna-5.8.0 (net.java.dev.jna:jna:5.8.0) and jetified-jna-5.8.0-runtime (net.java.dev.jna:jna:5.8.0)
    Duplicate class com.sun.jna.CallbackProxy found in modules jetified-jna-5.8.0 (net.java.dev.jna:jna:5.8.0) and jetified-jna-5.8.0-runtime (net.java.dev.jna:jna:5.8.0)
    

    To fix this, use @aar on whatever library needs jna as well, otherwise gradle will pull the JNA library in twice.

    implementation "net.java.dev.jna:jna:5.9.0@aar"
    

    Then add the necessary dependencies to your resource path. If you need to add*.so files, download the relevant jar and extract it. For example, if you are missing android-x86,

  • download the jar from here. https://github.com/java-native-access/jna/blob/master/dist/android-x86-64.jar

  • Extract the jar file

  • Copy and paste extracted directory to the resource path Example Resource path :

    sourceSets {
        main {
            jniLibs.srcDirs += ["$projectDir/src/debug/jniLibs"]
        debug {
            jniLibs.srcDirs += ["$projectDir/src/debug/jniLibs"]
        release {
            jniLibs.srcDirs += ["$projectDir/src/release/jniLibs"]
    
  • Clean and rebuild the project

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •