如何在 64 位 Android 设备上使用 32 位本机库 [英] How to use 32-bit native libraries on 64-bit Android device

查看:76
本文介绍了如何在 64 位 Android 设备上使用 32 位本机库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了一个仅针对 armeabi、armeabi-v7a 和 x86 编译的本机库.

I use a native library in my application that is only compiled for armeabi, armeabi-v7a and x86.

当这个库加载到三星 S6 等 64 位设备上时,应用程序崩溃并出现 UnsatisfiedLinkError

When this library is loaded on a 64-bit device like the Samsung S6, the application crashes with an UnsatisfiedLinkError

java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.myapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.myapp-2/lib/arm64, /vendor/lib64, /system/lib64]]] couldn't find "libfoo.so"
    at java.lang.Runtime.loadLibrary(Runtime.java:366)
    at java.lang.System.loadLibrary(System.java:989)

不幸的是,该库是闭源的.有没有办法在不重新编译具有 64 位目标的库的情况下解决这个问题?

The library is closed source unfortunately. Is there any way to fix this without recompiling the library with 64-bit targets?

推荐答案

当你在 Android 上安装 APK 时,系统会寻找原生库目录(armeabi、armeabi-v7a、arm64-v8a、x86、x86_64、mips64、mips) 位于 APK 的 lib 文件夹中,顺序由 Build.SUPPORTED_ABIS 确定.

When you install an APK on Android, the system will look for native libraries directories (armeabi, armeabi-v7a, arm64-v8a, x86, x86_64, mips64, mips) inside the lib folder of the APK, in the order determined by Build.SUPPORTED_ABIS.

如果您的应用程序碰巧有一个缺少库的 arm64-v8a 目录,则不会从另一个目录安装缺少的库,这些库不会混合.这意味着您必须为每个架构提供完整的库集.

If your app happen to have an arm64-v8a directory with missing libs, the missing libs will not be installed from another directory, the libs aren't mixed. That means you have to provide the full set of your libraries for each architecture.

因此,为了解决您的问题,您可以从构建中删除 64 位库,或将 abiFilters 设置为仅打包 32 位架构:

So, to solve your issue, you can remove your 64-bit libs from your build, or set abiFilters to package only 32-bit architectures:

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

这篇关于如何在 64 位 Android 设备上使用 32 位本机库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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