How to use 32-bit native libraries on 64-bit Android device
Asked Answered
D

2

61

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

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)

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

Deathbed answered 11/6, 2015 at 13:54 Comment(4)
Have you looked at https://mcmap.net/q/323977/-how-to-use-32bit-native-libraries-on-64-bit-android-l-platform/603270 and https://mcmap.net/q/323978/-java-lang-unsatisfiedlinkerror-when-loading-native-library-in-android-5-0-duplicate ?Winni
I did. One applies to AOSP, the other one for dynamic classloading. My case is actually much more simple. But I just discovered that another native library that I include does indeed supply 64-bit .so files, therefore there is a arm64-86 folder in my /lib folder in the final apk. Probably this is why the system assumes, that I provide 64-bit .so files in all cases. I'll try to recompile this librariy with 32-bit support only and see if it works.Deathbed
Good thinking. Let us know (answer you own question if needed).Winni
tools.android.com/tech-docs/new-build-system/user-guide/…Compliment
A
149

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.

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.

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"
        }
    }
}
Aerobiology answered 12/6, 2015 at 9:34 Comment(10)
Looks good, the 64-bit folders are gone with this snippet. I will mark the answer as accepted once I can find a 64 bit phone to reproduceDeathbed
tools.android.com/tech-docs/new-build-system/user-guide/…Compliment
Getting error NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.Blayze
add android.useDeprecatedNdk=true to a file named gradle.properties at the root of your project. btw don't feel bad about using a deprecated integration, as using abiFilters is still the cleanest way atm to filter out 64-bit libs from an APK.Aerobiology
Thanks @ph0b, this solves a totally different issue I've been struggling with for days :)Gayden
@Aerobiology You should make a separate answer. It is bearly visible and this solved my problem too :)Morrissey
@Aerobiology i have issue with two different so file that i have used if i set ndk api filter to abiFilters "arm64-v8a","armeabi", "armeabi-v7a", "x86","x86_64", "mips" then first so file working fine but second so file not loaded and if i set abiFilters "armeabi", "armeabi-v7a", "x86", "mips" then first so file store loading but second file working fine can you please provide any solutionResentful
As of today (August 1st, 2019), you cannot submit APKs to the Google Play Store without 64-bit architecture.Cob
Adding this to my app's build.gradle file did it for me :)Carpospore
Unfortunately, this solution did not work for me. I have also updated the gradle.properties file with deperecatedNDK property but that also do not behave as per expectation.Boatload
F
8

The above answer will help to generate a build with 32- bit only not a 64-bit. If you are using [ abiFilters "armeabi", "armeabi-v7a", "x86", "mips" ]. generate signed apk means, that is not for 64 bit. It will raise an error when we upload into Google Play store. It is not an 64 bit build.

Frieda answered 17/9, 2019 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.