Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native libraries not found in ApplicationInfo.nativeLibraryDir when building app bundle for arm64 Android phone

I am trying to migrate my app from a monolithic APK to the app bundle format. I need to set LD_LIBRARY_PATH environment variable for an exec() call, therefore I need the location of my native libraries. With the original APK I would call getApplicationInfo().nativeLibDir and the libraries were there.

With the app bundle they are not. I can see the correct abi split APK installed, but for some reason the libraries are not extracted.

I have tried installing with bundletool and through Google Play,

Tried to run 'ls -alR' and I can clearly see the directory exists as well as the split apk, but the libraries are simply not extracted. I guess I could extract them manually as a workaround but that would seem unnecessary..?

Here is the output of ls on the parent folder of nativeLibPath

genLibraryPath: Dir Contents: /data/app/com.unseenonline-raAFLhJMQpjqWkVdG1Vocg==:
        total 16704
        drwxr-xr-x   4 system system      4096 2019-06-11 12:41 .
        drwxrwx--x 114 system system     12288 2019-06-11 12:41 ..
        -rw-r--r--   1 system system   5688352 2019-06-11 12:41 base.apk
        drwxr-xr-x   3 system system      4096 2019-06-11 12:41 lib
        drwxrwx--x   3 system install     4096 2019-06-11 12:41 oat
        -rw-r--r--   1 system system  11226112 2019-06-11 12:41 split_config.arm64_v8a.apk
        -rw-r--r--   1 system system     35636 2019-06-11 12:41 split_config.en.apk
        -rw-r--r--   1 system system     90443 2019-06-11 12:41 split_config.xxhdpi.apk

        /data/app/com.unseenonline-raAFLhJMQpjqWkVdG1Vocg==/lib:
        total 24
        drwxr-xr-x 3 system system 4096 2019-06-11 12:41 .
        drwxr-xr-x 4 system system 4096 2019-06-11 12:41 ..
        drwxr-xr-x 2 system system 4096 2019-06-11 12:41 arm64

        /data/app/com.unseenonline-raAFLhJMQpjqWkVdG1Vocg==/lib/arm64:
        total 16
        drwxr-xr-x 2 system system 4096 2019-06-11 12:41 .
        drwxr-xr-x 3 system system 4096 2019-06-11 12:41 ..

As you can see the split apks are there but the libraries are not extracted.

Libraries should be extracted to the same location as they were with the original apk

like image 901
Elad Levin Avatar asked Jun 11 '19 20:06

Elad Levin


1 Answers

By default, APKs generated from the Android App Bundle have the native libraries uncompressed on devices with Android M / API level 23 or higher (source). Not only does that often reduce the download size but that also considerably reduce the size of the app on devices since the Android platform can directly read the native libraries from the APK instead of having to extract them to a separate location. There was a talk at last I/O on how to reduce the size of your app and how that impacts install numbers, and they detailed how this works if you're interested in understanding this better.

So, now that you know why Google Play is doing this, you have the following options:

  • You can choose to revert to the original APK behaviour, and this can be done by adding the flag android.bundle.enableUncompressedNativeLibs=false in your gradle.properties file. This will effectively disable this optimization, leading to a bigger size of your app for all your users on M+.

  • You can ensure that the native library is loaded by the Android platform (e.g. using System.loadLibrary) or you if you're reading the library directly yourself for some reason, read it from the APK directly as well.

If the native libraries are loaded by a third party library you're depending on, consider filing a bug for them to address this issue so they follow the same logic as the platform.

Hope that helps,

like image 194
Pierre Avatar answered Sep 19 '22 07:09

Pierre