Android studio: UnsatisfiedLinkError: findLibrary returned null - loading native library
Asked Answered
G

2

11

I am making an app in Android Studio which uses two libraries. A native library with an Android wrapper and a jar-library. For some reason, the native library won't load if the other jar-library is compiled into the project. So if I run the app with only the native library, everything works fine. I add the other jar-library to my gradle-file and boom... an UnsatisfiedLinkError:

java.lang.UnsatisfiedLinkError: Couldn't load MobileOcrEngine from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.app-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.app-1, /vendor/lib, /system/lib]]]: findLibrary returned null

My app runs fine when I use this:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'src/main/jniLibs')
    compile 'com.android.support:support-v13:21.0.2'
    compile project(':wheel')
}

The error occurs when I try:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'src/main/jniLibs')
    compile 'com.android.support:support-v13:21.0.2'
    compile project(':wheel')
    compile files('libs/realm-0.78.0.jar')
}

or when I try to use the same library but using the Maven repository:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'src/main/jniLibs')
    compile 'com.android.support:support-v13:21.0.2'
    compile project(':wheel')
    compile 'io.realm:realm-android:0.78.0'
}

or if I try to place the jar in jniLibs folder:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'src/main/jniLibs')
    compile 'com.android.support:support-v13:21.0.2'
    compile project(':wheel')
}

I have no idea where the root of the problem lies. With one of the two libraries, Android Studio or am I doing something wrong?

Note: I know there have been many questions on StackOverflow regarding UnsatisfiedLinkErrors, yet none of these provide solutions for my problem. I have no problem loading the native library if it's the only library I use...

Gershon answered 26/1, 2015 at 9:0 Comment(0)
G
17

I found the problem. The other jar I wanted to add uses internally a C++ library with support for armeabi, armeabi-v7a, x86 and mips. The native library I was using all this time supported only armeabi.

The device I am using for testing is a armeabi-v7a device. All this time when I was using the native library, the device checked for the library in the armeabi-v7a of my libs directory. If it couldn't find it there, it would try the armeabi directory.

When I load the other jar with support for 4 different architectures, the device loads the armeabi-v7a library. As it found an armeabi-v7a library for the jar, it will try to load the native library for the same architecture. If the library wasn't found, it will not check the armeabi directory as a backup, so the findLibrary returns null, hence the UnsatisfiedLinkError.

I solved it by making a directory for the armeabi architecture and copying the .so-library of the armeabi-v7a directory into it.

Gershon answered 26/1, 2015 at 14:40 Comment(2)
Could you please explain how to do your solutionPlastometer
In your jniLibs directory, have a subdirectory for both armeabi and armeabi-v7a. Copy your library so that it appears in both architecture (armeabi and armeabi-v7a) directories.Gershon
L
0
defaultConfig {
    ...

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

}
Loosing answered 4/11, 2016 at 11:6 Comment(1)
Could you provide even the slightest explanation of why someone should copy this code into their application?Benison

© 2022 - 2024 — McMap. All rights reserved.