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

Ela*_*vin 3 android android-ndk android-studio android-app-bundle

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 ..
Run Code Online (Sandbox Code Playgroud)

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

Pie*_*rre 8

默认情况下,从Android App Bundle生成的APK具有在Android P +的设备上未压缩的本地库。由于Android平台可以直接从APK读取本机库,而不必将它们提取到单独的位置,因此这不仅通常会减少下载大小,而且还大大减少了设备上应用程序的大小。在最后的I / O上,有一个关于如何减小应用程序大小以及如何影响安装数量的演讲,他们详细介绍了如何在您有兴趣更好地理解应用程序的情况下工作。

因此,既然您知道为什么Google Play会这样做了,那么您可以选择以下选项:

  • 您可以选择还原为原始的APK行为,这可以通过android.bundle.enableUncompressedNativeLibs=false在gradle.properties文件中添加标志来完成。这将有效地禁用此优化,从而为M +上的所有用户提供更大的应用程序大小。

  • 您可以确保本机库是由Android平台加载的(例如使用System.loadLibrary),或者如果您出于某种原因直接读取该库,也可以直接从APK读取该库。

如果本机库是由您依赖的第三方库加载的,请考虑为它们提交错误以解决此问题,以便它们遵循与平台相同的逻辑。

希望能有所帮助,

  • 要在您的APK中找到共享库,可以使用[AssetManager.openNonAssetFd()](https://developer.android.com/reference/android/content/res/AssetManager.html#openNonAssetFd(java.lang.String ))。而不是** dlopen()**,您应该使用特定于平台的[android_dlopen_ext()](https://developer.android.com/ndk/reference/group/libdl),并传递** AssetFileDescriptor **的句柄和偏移量。请注意,** AssetManager **也具有[native API](https://developer.android.com/ndk/reference/group/asset)。 (3认同)
  • 我相信您也可以执行dlopen(“ / path / to / MyApp.apk!libfoo.so”,...)。如果那不起作用,您可以提交错误吗?我们应该确保有一个简单的方法可以做到这一点(我认为应该有一个API可以“从我的APK打开此库”,所以您无需弄清楚APK ...文件的路径。询问API是否是您想要的错误) (3认同)
  • @DanAlbert根据[此答案](/sf/ask/1100340461/#15721061),您可以调用`系统Java中的.loadLibrary()`,然后`dlsym(0,..)`将与加载的任何库一起使用。不确定这是否适用于通过exec()生成的进程,或者仅适用于JNI调用。您也可以尝试在您的本机代码中调用`loadLibrary()`java命令。 (2认同)