java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader
Asked Answered
L

21

75

Is there someone who had experience with this error?

java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/org.swig.simple-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]] couldn't find "liborg.swig.simple.example.so"

Error occurs when I load library by this way.

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

I'm sure 'example' class is exist in the current folder.

Lauritz answered 28/11, 2014 at 10:30 Comment(1)
I test all 15 answers but my problem not solved. what should do? java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.allInOne.gifemaker-1/base.apk"],nativeLibraryDirectories=[/data/app/com.allInOne.gifemaker-1/lib/arm, /data/app/com.allInOne.gifemaker-1/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib, system/vendor/lib, system/vendor/lib/egl, system/lib/hw]]] couldn't find "libavutil.so" at java.lang.Runtime.loadLibrary(Runtime.java:367)Jorgejorgensen
P
55

Please note that there's a naming convention. Your lib needs to be called libexample.so .

LoadLibrary("example") will look for libexample.so.

The .so library needs to be inside the apk under the lib folder (since you are developing for Android, it needs to be under the lib/armeabi and lib/armeabi-v7a folders - why both folders ? some versions of Android look under lib/armeabi and some look under lib/armeabi-v7a ... se what works for you ).

Other things to look for :

  • make sure you compile for the correct architecture (if you compile for armeabi v5 it won't work on armeabiv7 or armeabiv7s ).

  • make sure your exported prototypes are used in the correct class (check the hello jni example. Your exposed functions need to look something like Java_mypackagename_myjavabridgeclass_myfunction).

For example the function Java_com_example_sample_hello will translate in the java class com.example.sample , function hello.

Prurigo answered 28/11, 2014 at 11:4 Comment(6)
I downloaded this example swig.org/Doc2.0/Android.html#Android_examples_intro My CBU/ABI is armeabi-v7a is there any solution ?Lauritz
If you are new to jni, use the google example : code.google.com/p/awesomeguy/wiki/JNITutorial . I personally don't like swig, I avoid using it. Swig will add an extra level of compexity to your code, and since you don't fully grasp the jni concept it's better to avoid it at first (swig generates things for you ... you need to know what these things are and how they are used before you can fully benefit from what swig is offering ;) )Prurigo
I have not any choice. Because my client provides me a C++ function which I have to call from my java code.Lauritz
I was simply giving a hint on where to start to understand jni. I cannot provide blindly a solution for your context :).Prurigo
saved me, struggled for a day for that things, i only had for armeabiv7 and not for armeabi-v7aRumania
I have the same issue like a @Lauritz but only on Sony phones with Android 7 and 8. I have the following abifilters: "armeabi-v7a", "arm64-v8a". On another devices works fine. Is there any ideas?Halm
W
63

I am currently working on an Android application which streams radio. I use native decoder library which is called aacdecoder. Everything was fine till app gets crash error on some Android devices. It was really annoying. Because app was perfectly plays radio streams almost all devices but Samsung S6 and S6 Edge.

Crash report says that

Fatal Exception: java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file “/data/app/com.radyoland.android-1/base.apk”],nativeLibraryDirectories=[/data/app/com.radyoland.android-1/lib/arm64, /vendor/lib64, /system/lib64]]] couldn’t find “libaacdecoder.so”
 at java.lang.Runtime.loadLibrary(Runtime.java:366)
 at java.lang.System.loadLibrary(System.java:988)
 at com.spoledge.aacdecoder.Decoder.loadLibrary(Decoder.java:187)

As you see that crash is saying that it could not load native library. But why? First of all I checked my structure, If native library .so files located correctly.

Seems everything was okay except this crazy error. Then after some research, I find out that some of android devices has 64-bit processors. This devices generates and check arm64 folder to load native library. That was the problem. Because my project does not have arm64 folder. Here is the solution;

defaultConfig {
    ...

    ndk {
        abiFilters "armeabi-v7a", "x86", "armeabi", "mips"
    }

}

You need to add this filters(abiFilters) to your app module’s build.gradle files. So when your device try to run your app, it will check gradle file and understands that it should not generate any folder and use existing native library resources. Boom, almost solved. But still there is one more thing.

android.useDeprecatedNdk=true

Add this line to your gradle.properties to use deprecated Ndk.

Finally my app works on S6 and S6 Edge. I mean it works on every devices which has new 64-bit processors.

Update :

As of Dec/2019 armabi and mips are deprecated. Supported ABIs are [arm64-v8a, armeabi-v7a, x86, x86_64]

So, your code should be like this

defaultConfig {
        ...

        ndk {
            abiFilters "arm64-v8a", "armeabi-v7a", "x86", "x86_64"
        }

    }
Wolsey answered 5/9, 2017 at 9:14 Comment(10)
thanks! this was the only solution that worked for meMaul
android.useDeprecatedNdk=true this flag is depreciatedNutrition
as of Dec/2019 armabi and mips are deprecated. Supported ABIs are [arm64-v8a, armeabi-v7a, x86, x86_64].Ladle
can someone help me? it doesn't work, my app keeps crashing on Pixel 2...Cyanamide
@RuberandindaPatienceCyanamide
@Cyanamide In my build.gradle file file I remains with abiFilters "x86", "armeabi" only. for me to work on my devices.Warmth
@RuberandindaPatience Thanks for reply. But doesn't this means if I build appgradle it won't generate for 64 bits? google playstore requires allCyanamide
original answer is hereOliviaolivie
Only this answer work for me. I added my files into the jniLibs into my app/src/main folder but it didn't work. Adding the abiFilters save my day.Schneider
even using the relevant abiFilters, if you run apk on a device that does not have the relevant native libraries compiled for it, then they system cannot find them. For example, abiFilters "x86" but running on ARM emulator will crash.Griswold
P
55

Please note that there's a naming convention. Your lib needs to be called libexample.so .

LoadLibrary("example") will look for libexample.so.

The .so library needs to be inside the apk under the lib folder (since you are developing for Android, it needs to be under the lib/armeabi and lib/armeabi-v7a folders - why both folders ? some versions of Android look under lib/armeabi and some look under lib/armeabi-v7a ... se what works for you ).

Other things to look for :

  • make sure you compile for the correct architecture (if you compile for armeabi v5 it won't work on armeabiv7 or armeabiv7s ).

  • make sure your exported prototypes are used in the correct class (check the hello jni example. Your exposed functions need to look something like Java_mypackagename_myjavabridgeclass_myfunction).

For example the function Java_com_example_sample_hello will translate in the java class com.example.sample , function hello.

Prurigo answered 28/11, 2014 at 11:4 Comment(6)
I downloaded this example swig.org/Doc2.0/Android.html#Android_examples_intro My CBU/ABI is armeabi-v7a is there any solution ?Lauritz
If you are new to jni, use the google example : code.google.com/p/awesomeguy/wiki/JNITutorial . I personally don't like swig, I avoid using it. Swig will add an extra level of compexity to your code, and since you don't fully grasp the jni concept it's better to avoid it at first (swig generates things for you ... you need to know what these things are and how they are used before you can fully benefit from what swig is offering ;) )Prurigo
I have not any choice. Because my client provides me a C++ function which I have to call from my java code.Lauritz
I was simply giving a hint on where to start to understand jni. I cannot provide blindly a solution for your context :).Prurigo
saved me, struggled for a day for that things, i only had for armeabiv7 and not for armeabi-v7aRumania
I have the same issue like a @Lauritz but only on Sony phones with Android 7 and 8. I have the following abifilters: "armeabi-v7a", "arm64-v8a". On another devices works fine. Is there any ideas?Halm
E
40

This helped me. Sharing it for someone who might come up with same issue.

android {
    ....
    defaultConfig {
        ....
        ndk {
            abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
        }
    }
}
Exceedingly answered 5/7, 2016 at 20:33 Comment(4)
This doesn't explain why it helped you. I am facing the said issue, and already have abiFilters setup.Listed
Thanks, I only had 'armeabi' and was wondering why libc++_shared.so in my armeabi-v7a library cannot be found!! Wow, without your pointer Idk how I could see it myself.Schizo
This helped me, I still don't know why it works. but thank you!Crypt
Thanks Man Save My Day.Phenolic
M
13

What worked for me was to place the jniLibs folder under the "main" folder, just besides the "java" and "res" folders, for example project -> app -> src -> main -> jniLibs

I had all the libraries with the correct names and each one placed on their respective architecture subfolder, but I still had the same exception; even tried a lot of other SO answers like the accepted answer here, compiling a JAR with the .so libs, other placing of the jniLibs folder, etc.

For this project, I had to use Gradle 2.2 and Android Plugin 1.1.0 on Android Studio 1.5.1

Milo answered 4/5, 2016 at 21:39 Comment(0)
M
9

-if gradle.properties not available then first add that file and add android.useDeprecatedNdk=true

-use this code in build.gradle

defaultConfig {
    applicationId 'com.example.application'
    minSdkVersion 16
    targetSdkVersion 21
    versionCode 11
    versionName "1.1"
    ndk {
        abiFilters "armeabi"
    }
}

`

Magnuson answered 14/2, 2018 at 12:19 Comment(0)
M
8

I use Android Studio 3.0 and encounter this problem. And I'm sure app's build.gradle is OK.

Go to Run -> Edit Configurations -> Profiling, and disable "Enable advanced profiling".

This works for me. Reference answer

Megargee answered 26/6, 2017 at 7:7 Comment(2)
but why though?.Trover
Thanks a ton for this. I had enabled advanced profilling for debugging network traffic but the next day I got this error and was left puzzled about why.Opposition
G
7

This is worked for me

If your having .so file in armeabi then mention inside ndk that folder alone.

defaultConfig {
        applicationId "com.xxx.yyy"
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        renderscriptTargetApi 26
        renderscriptSupportModeEnabled true
        ndk {
            abiFilters "armeabi"
        }
    }

and then use this

android.useDeprecatedNdk=true;

in gradle.properties file

Gam answered 2/11, 2017 at 8:30 Comment(2)
Could not find method abiFilters() for arguments [armeabi] on projectStatecraft
Dont include comma after This is worked for me If your having .so file in armeabi then mention inside ndk that folder alone. defaultConfig { applicationId "com.xxx.yyy" minSdkVersion 17 targetSdkVersion 26 versionCode 1 versionName "1.0" renderscriptTargetApi 26 renderscriptSupportModeEnabled true ndk { abiFilters "armeabi" } } and then use this android.useDeprecatedNdk=true; just use android.useDeprecatedNdk=true without commaSuboceanic
T
7

What helped me was to register the source directory for jni files in the build.gradle file. Add this to your gradle file:

android {
    sourceSets {
        main {
            jniLibs.srcDir '[YOUR_JNI_DIR]' // i.e. 'libs'
        }
    }
}
Thicket answered 5/4, 2018 at 7:38 Comment(1)
this is work for me ! In my case I point to libs dir which contains *.so fileAchromatous
H
5

Some old gradle tools cannot copy .so files into build folder by somehow, manually copying these files into build folder as below can solve the problem:

build/intermediates/rs/{build config}/{support architecture}/

build config: beta/production/sit/uat

support architecture: armeabi/armeabi-v7a/mips/x86

Hawkeyed answered 25/3, 2016 at 3:20 Comment(1)
Strange, maybe it's an Android Studio bug? Same thing with jniLibs folder.Listed
W
5

If you are using Android studio, just edit the gradle.properties in the root folder and add android.useDeprecatedNdk=true. Then edit the build.gradle file in your app's folder, set abiFilters as below:

android {
....
defaultConfig {
    ....
    ndk {
        abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
    }
}
}
Wardrobe answered 8/2, 2018 at 18:17 Comment(0)
C
2

If you use module with c++ code and have the same issue you could try

Build -> Refresh Linked C++ Projects

Also, you should open some file from this module and do

Build -> Make module "YourNativeLibModuleName"

Communist answered 23/1, 2018 at 12:28 Comment(0)
R
2

Ensure you have included the different abiFilters, this enables Gradle know what ABI libraries to package into your apk.

defaultConfig {
 ndk { 
       abiFilters "armeabi-v7a", "x86", "armeabi", "mips" 
     } 
  }

If you storing your jni libs in a different directory, or also using externally linked jni libs, Include them on the different source sets of the app.

sourceSets { 
  main { 
    jni.srcDirs = ['src/main/jniLibs'] 
    jniLibs.srcDir 'src/main/jniLibs' 
    } 
}
Rainier answered 22/2, 2020 at 16:46 Comment(0)
H
1

Yet another crash cause and possible solution is described in this article: https://medium.com/keepsafe-engineering/the-perils-of-loading-native-libraries-on-android-befa49dce2db

Briefly:
in build.gradle

dependencies {
    implementation 'com.getkeepsafe.relinker:relinker:1.2.3'
}

in code

static {
    try {
        System.loadLibrary("<your_libs_name>");
    } catch (UnsatisfiedLinkError e) {
        ReLinker.loadLibrary(context, "<your_libs_name>");
    }
}
Halm answered 8/5, 2018 at 7:54 Comment(3)
How are you planning to pass the context in a static block?Nihhi
@Dr.aNdRO I refactored static block to static method. And now I'm calling it in the CustomApplication class.Halm
or you can create a helper with context alive :DNihhi
S
1

I have resolved my issue by adding these lines.

defaultConfig {
 ndk { 
       abiFilters "armeabi-v7a", "x86", "armeabi", "mips" 
     } 
  }
sourceSets { 
  main { 
    jni.srcDirs = ['src/main/jniLibs'] 
    jniLibs.srcDir 'src/main/jniLibs' 
    } 
}
Scant answered 8/11, 2021 at 12:14 Comment(0)
B
0

For me the problem was in NDK_ROOT not being set.

Check your console if:

NDK_ROOT = None [!] NDK_ROOT not defined. Please define NDK_ROOT in your environment or in local.properties

Check if you have set:

  • NDK_ROOT and SDK_ROOT in C/C++->Build->Environment
  • Android->NDK
  • Android->SDK
Banka answered 24/3, 2016 at 14:20 Comment(0)
A
0

This could be device related issue.
I was getting this error in MI devices only, code was working with all other devices.

This might help:

 defaultConfig{
      ...    
      externalNativeBuild {
                    cmake {
                        cppFlags "-frtti -fexceptions"
                    }
                }
    }
Archaeo answered 31/7, 2017 at 9:24 Comment(0)
L
0
Simple Solution with Pics

Step1: Add following code in build.gradle file under defaultConfig

     ndk {
            abiFilters "armeabi-v7a", "x86", "armeabi", "mips"
        }
Example:[![enter image description here][1]][1]


Steo 2: Add following code in gradle.properties file

android.useDeprecatedNdk=true

Example: [![enter image description here][2]][2]

Step 3: Sync Gradle and Run the Project.

@Ambilpur


  [1]: https://i.sstatic.net/IPw4y.png
  [2]: https://i.sstatic.net/ByMoh.png
Leaven answered 31/10, 2019 at 11:20 Comment(0)
G
0

In my case After running the ndk-build in the jni folder the shared library was created under the libs folder but the path specified in build.gradle

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

so I need to move the created shared library to jnilibs folder and it worked!

Gibert answered 11/11, 2020 at 8:24 Comment(0)
S
0

Copy your "example.so" file inside /libs/(armeabi|armeabi-v7a|x86|...) . When using Android Studio, it's /app/src/main/jniLibs/(armeabi|armeabi-v7a|x86|...)

Scorn answered 28/6, 2022 at 18:0 Comment(0)
P
0

This is how I fixed this particular issue

include below code in app level build.gradle insideandroid { }

externalNativeBuild {
        cmake {
            path "/CMakeLists.txt"
           
        }
    }

Ensure CMakeLists.txt exists in correct location

Ensure CMake and NDK are installed. Follow steps mentioned here to install the same

https://developer.android.com/studio/projects/install-ndk

The native files were inside jni folder, which was under Main folder as shown enter image description here

I neither used

ndk {
            abiFilters "arm64-v8a", "armeabi-v7a", "x86", "x86_64"
        } 

nor included

android.useDeprecatedNdk=true

in gradle.properties file

If everything done correctly in 'build' tab we can see

Task :NightSkyApps:buildCMakeDebug[x86]

Task :NightSkyApps:configureCMakeDebug[x86_64]

Task :NightSkyApps:buildCMakeDebug[x86_64]

If anything goes wrong then above tasks fails which can be seen in 'build' tab.

I faced issue on Android Studio emulator Pixel 4: API 31. Android Studio version

Android Studio Dolphin | 2021.3.1
Build #AI-213.7172.25.2113.9014738, built on September 1, 2022

Build gradle

classpath 'com.android.tools.build:gradle:7.2.2'
Pie answered 24/10, 2022 at 15:8 Comment(0)
R
0

If you have old device try to rename "armeabi" folder to "armeabi-v7a". It fixed this issue for me because i dont realized, that i had device with 32-bit ARM-based CPU.

https://developer.android.com/ndk/guides/abis#v7a

Rozella answered 2/6, 2023 at 13:12 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Cephalization

© 2022 - 2024 — McMap. All rights reserved.