Search code examples
javaandroidjava-native-interfaceapknative

APK size exceed 50MB due to large amount of native libraries


Our app supports 7 different architectures. We have a native library which we are using in our java code. When we build a single APK its size reach the 50MB limit because we have 7 instances of the native library packed into the APK. Each instance of the library after compilation is about 10MB.

We don't want to use the multiple APKs method to solve it because it's hard to manage more than 1 APK.

We thought to use the expansion method but we don't know if it possible. We want to pack all the native libraries in a zip and upload it as an expansion to google and then in runtime to use the loadLibrary method in order to load the appropriate library. Does it possible?

Feel free to suggest any other solution that you think it might be suitable.

Thanks.


Solution

  • I don't think it is possible to call loadLibrary on a .so that is not in the app's apk or on the device (expansions included).

    What you can do, though, is include your .so libraries inside an expansion file, create a Starter activity that extracts these SO to a system path on first run, then start your application, invoking:

     System.loadLibrary("/path/to/your/lib/yourlib.so");
    

    You can use the assets folder, treat the .so as an asset, and extract it to the given path.

    You can create SOLoaderActivity, which becomes the Activity launched by your app. On first run, the activity extracts the .so, and writes a preference to SharedPreferences. After that, it just checks the preference and launches your other Activity if the pref is set.

    I'm not sure how loadLibrary handles full paths, but System.load can do it.