Android studio: UnsatisfiedLinkError: findLibrary

2019-02-16 15:50发布

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...

2条回答
Rolldiameter
2楼-- · 2019-02-16 16:18

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.

查看更多
【Aperson】
3楼-- · 2019-02-16 16:22
defaultConfig {
    ...

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

}
查看更多
登录 后发表回答