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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Philipp E.
  • 3,296
  • 3
  • 34
  • 52
  • Have you looked at https://stackoverflow.com/q/27712921/603270 and https://stackoverflow.com/a/29329413/603270 ? – shkschneider Jun 11 '15 at 14:04
  • 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. – Philipp E. Jun 11 '15 at 16:22
  • Good thinking. Let us know (answer you own question if needed). – shkschneider Jun 12 '15 at 08:05
  • http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits#TOC-ABIs-Splits – Junior Buckeridge Oct 09 '15 at 19:23

2 Answers2

148

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"
        }
    }
}
ph0b
  • 14,353
  • 4
  • 43
  • 41
  • 1
    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 reproduce – Philipp E. Jun 12 '15 at 15:32
  • http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits#TOC-ABIs-Splits – Junior Buckeridge Oct 09 '15 at 19:23
  • Getting error NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin. – Umesh Aawte Nov 25 '15 at 12:49
  • 12
    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. – ph0b Nov 30 '15 at 18:05
  • Thanks @ph0b, this solves a totally different issue I've been struggling with for days :) – ssantos Oct 11 '16 at 10:58
  • @ph0b You should make a separate answer. It is bearly visible and this solved my problem too :) – Silviu St Sep 04 '17 at 08:20
  • @ph0b 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 solution – Om Infowave Developers Sep 06 '17 at 07:11
  • 4
    As of today (August 1st, 2019), you cannot submit APKs to the Google Play Store without 64-bit architecture. – Joshua Pinter Aug 01 '19 at 16:47
  • Adding this to my app's `build.gradle` file did it for me :) – Augusto Carmo Jan 15 '20 at 17:45
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.

Philipp E.
  • 3,296
  • 3
  • 34
  • 52
Arun Kumar
  • 89
  • 1
  • 4