Best practices for remote loading of .so files in Android SDKs

Updated at:

If your app supports remote loading of .so files and has strict size limits for SDK packages, move the .so files from the SDK to your app’s remote repository. Then use your app’s existing remote loading mechanism to deliver and load them dynamically.

Applicability

  • Apps must support remote access.

  • This solution does not support NFC features.

Procedure

  1. Extract and upload .so files
    Manually unpack the AAR file or use your custom framework to automatically extract the .so files from the AAR. Upload them to your app’s remote repository.

  2. Download and load .so files
    Download and load the .so files before calling ZIMFacade.install(this). Use logic similar to the following example.

        /**
         * Load an .so file from outside the APK.
         * @param soFullPath Full path to the external .so file. For example: /data/user/0/ctrip.android.view.test/files/libtoyger.so
         */
        public void loadsoFile(String soFullPath) {
            try {
                if (!TextUtils.isEmpty(soFullPath)) {
                    File file = new File(soFullPath);
                    if (file.exists()) {
                        // Validate the file.
                        String fileMd5 = Utils.getFileMd5(file);
                        if (fileMd5.equals("xxxxx")) {// Replace "xxxxx" with the actual MD5 hash of the .so file.
                            System.load(soFullPath);
                            // Add logic to run after successful loading.
                        }
                    }
                }
            } catch (Throwable th) {
                // Loading failed.
            }
        }
  3. Configure packaging to exclude .so files
    Exclude the .so files you plan to load remotely from your build configuration. This prevents them from being bundled into the APK. Example:

    android {
        packagingOptions {
            exclude 'lib/arm64-v8a/libtoyger.so'
            exclude 'lib/armeabi-v7a/libtoyger.so'
            exclude 'lib/armeabi/libtoyger.so'
        }
    }

Impact on APK size after remote loading .so files

Feature module combination

Dual architecture

arm64

armv7

Code modules

Face recognition + Quality + Dual security modules

16.04 MB

10.52 MB

9.71 MB

  • aliyun-face

  • aliyun-base

  • aliyun-facade

  • aliyun-facequality

  • APSecuritySDK

  • Android-AliyunFaceGuard

Remote load .so files from aliyun-face (libtoyger.so, libzkfv_tj.so)

–1.07 MB

–0.58 MB

–0.48 MB

Remote load .so files from aliyun-facade (libaliyunaf.so)

–0.19 MB

–0.11 MB

–0.08 MB

Remote load .so files from aliyun-facequality (libxnn.so, libxnnloader.so)

–3.68 MB

–1.96 MB

–1.73 MB

Remote load .so files from APSecuritySDK (libAPSE_8.0.1.so, libAPSE_J.so)

–4.05 MB

–2.16 MB

–1.89 MB

The libfacedevice.so library in Remote Android-AliyunFaceGuard

–2.15 MB

–1.14 MB

–1.01 MB

Final APK size after full remote loading

4.90 MB

4.57 MB

4.52 MB

  • aliyun-face (without libtoyger.so and libzkfv_tj.so)

  • aliyun-base

  • aliyun-facade (without libaliyunaf.so)

  • aliyun-facequality (without libxnn.so and libxnnloader.so)

  • APSecuritySDK (without libAPSE_8.0.1.so and libAPSE_J.so)

  • Android-AliyunFaceGuard (without libfacedevice.so)