Android 工作室:UnsatisfiedLinkError:findLibrary 返回 null - 加载本机库 [英] Android studio: UnsatisfiedLinkError: findLibrary returned null - loading native library

查看:37
本文介绍了Android 工作室:UnsatisfiedLinkError:findLibrary 返回 null - 加载本机库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Android Studio 中制作一个使用两个库的应用程序.带有 Android 包装器和 jar 库的本机库.出于某种原因,如果另一个 jar-library 被编译到项目中,则不会加载本机库.因此,如果我仅使用本机库运行该应用程序,则一切正常.我将另一个 jar-library 添加到我的 gradle-file 和 boom...一个 UnsatisfiedLinkError:

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')
}

我尝试时出现错误:

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')
}

或者当我尝试使用相同的库但使用 Maven 存储库时:

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'
}

或者如果我尝试将 jar 放在 jniLibs 文件夹中:

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')
}

我不知道问题的根源在哪里.使用两个库之一,Android Studio 还是我做错了什么?

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?

注意:我知道在 StackOverflow 上有很多关于 UnsatisfiedLinkErrors 的问题,但这些都没有为我的问题提供解决方案.如果它是我唯一使用的库,则加载本机库没有问题...

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

推荐答案

我发现了问题.我想添加的另一个 jar 在内部使用 C++ 库,支持 armeabiarmeabi-v7ax86mips.我一直使用的本机库仅支持 armeabi.

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.

我用于测试的设备是 armeabi-v7a 设备.一直以来,当我使用本机库时,设备会在我的 libs 目录的 armeabi-v7a 中检查该库.如果在那里找不到它,它会尝试 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.

当我加载另一个支持 4 种不同架构的 jar 时,设备会加载 armeabi-v7a 库.当它为 jar 找到 armeabi-v7a 库时,它将尝试加载相同架构的本机库.如果没有找到库,它不会检查 armeabi 目录作为备份,所以 findLibrary 返回 null,因此 UnsatisfiedLinkError.

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.

我通过为 armeabi 架构创建一个目录并将 armeabi-v7a 目录的 .so-library 复制到其中来解决它.

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

这篇关于Android 工作室:UnsatisfiedLinkError:findLibrary 返回 null - 加载本机库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆