Skip to content

Load Modules in Runtime

The Google Play app bundle size limit is 150 MB. You may wonder what to do if your app size has increased dramatically after integrating the Document Reader SDK. No need to panic! You can download some resources on demand while using your app, for example, Core, using Google's Play Feature Delivery.

Quote

Play Feature Delivery uses advanced capabilities of app bundles, allowing certain features of your app to be delivered conditionally or downloaded on demand.

Create a Feature Module

First, you need to separate features like the Core framework from your base app into distinct feature modules. For instance, you can name it regula_core_sdk.

When you create a new feature module using Android Studio, the IDE applies the following Gradle plugin to the module’s build.gradle file:

build.gradle
1
2
3
plugins {
    id 'com.android.dynamic-feature'
}

Include the Core library as a dependency:

build.gradle
1
2
3
4
5
6
7
8
plugins {
    id 'com.android.dynamic-feature'
}

dependencies {
    implementation project(":app")
    implementation 'com.regula.documentreader.core:fullrfid:+@aar'
}

Then establish a relationship with the app module by adding the following line to the app's build.gradle file:

build.gradle
1
2
3
android {
    dynamicFeatures = [':regula_core_sdk']
}

And add the Feature Delivery library:

build.gradle
1
2
3
4
5
6
7
android {
    dynamicFeatures = [':regula_core_sdk']
}

dependencies {
    implementation 'com.google.android.play:feature-delivery:+'
}

How to add the API library to your project

In the AndroidManifest.xml file of the feature module, indicate the option of how to load the feature module:

AndroidManifest.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:dist="http://schemas.android.com/apk/distribution"
    package="com.regula.feature.core">

    <dist:module
        dist:instant="false"
        dist:title="@string/title_dynamic_core">
        <dist:delivery>
            <dist:on-demand/>
        </dist:delivery>
        <dist:fusing dist:include="false" />
    </dist:module>
</manifest>

Download a Feature Module

Once the project is configured, you can download the Core library using the following code:

class MainActivity : AppCompatActivity() {
    private const val coreModule = "regula_core_sdk"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        loadModule(coreModule)
    }

    private fun loadModule(name: String) {
        val request = SplitInstallRequest.newBuilder()
            .addModule(name)
            .build()

        splitInstallManager.startInstall(request)
            .addOnFailureListener {
                ...
            }
    }
}
public class MainActivity extends AppCompatActivity {

    private static final String coreModule = "regula_core_sdk";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadModule(coreModule);
    }

    private void loadModule(String name) {
        SplitInstallRequest splitInstallRequest = SplitInstallRequest.newBuilder()
                .addModule(name)
                .build();

        splitInstallManager.startInstall(splitInstallRequest)
                .addOnFailureListener(e -> { ... });

    }
}

Check If a Feature Module is Installed

To verify if the feature module is installed, you can use this code:

private fun checkModule() {
    if (splitInstallManager.installedModules.contains(coreModule)) {
        SplitCompat.install(this)
        ...
    } else {
        ...
    }
}
private fun checkModule() {
    if (splitInstallManager.getInstalledModules().contains(coreModule)) {
        SplitCompat.install(this)
        ...
    } else {
        ...
    }
}

Start Using Document Reader SDK

Once the feature module is downlaoded and installed, you need to download the database (if needed) and initialize the Document Reader SDK.

Example

A complete project demonstrating these steps can be found on GitHub: Feature Delivery.

Next Steps