When Google introduced Android App Bundle, it was remarkable as it lowered the download size of every application by approximately 30–60%. As soon as it was launched, I tried it on one of my applications and it was a good experience. I did not face any major issues and it helped me reduce my app size. However, I faced an issue while trying to support supporting multiple languages in the application. The user could switch between different languages and it was working quite well when I deployed APKs. However, when I introduced the AAB, the feature wasn’t working as the language did not change dynamically. After looking into it, I found that AAB downloads only the APK with the languages set as default in the device. But we can tweak and provide languages dynamically with App Bundle too!

Implementation Steps-

In the App level Gradle, I included the following code within the android division,

bundle {
        language {
            // Specifies that the app bundle should not support
            // configuration APKs for language resources. These
            // resources are instead packaged with each base and
            // dynamic feature APK.
            enableSplit = false
        }
    }

However, the solution was not optimal. In my case, I had only two languages so it did not cause much overhead. But if your application supports many languages, the solution does not seem to be optimal. Because this setting instructs the AAB to download all the language resources.

A better and optimal solution for this use case is by using the SplitInstallRequest from Play core library along with for SplitInstallRequest downloading the images when required.

To do so, follow the 2 easy steps mentioned below,

  1. Add the following dependency in your App Gradle file,
implementation 'com.google.android.play:core:1.6.3'

2. Use SplitInstallRequest in the appropriate location (possibly when the user selects a new language mode) to download the language’s strings.xml on the fly,

// Creates a request to download and install additional language resources.
SplitInstallRequest request = SplitInstallRequest.newBuilder()
// Uses the addLanguage() method to include French language resources in the request.
// Note that country codes are ignored. That is, if your app
// includes resources for “fr-FR” and “fr-CA”, resources for both
// country codes are downloaded when requesting resources for "fr".
 .addLanguage(Locale.forLanguageTag("fr"))
        .build();
// Submits the request to install the additional language resources.
splitInstallManager.startInstall(request);

Refer to the following Android Developer’s article to know about the other listeners and how to use them — https://developer.android.com/guide/app-bundle/playcore#lang_resources

The SplitInstallRequest feature can be used to download other dynamic modules and it is the recommended way to do with the new publishing format — AAB.

So here’s how you can use either the App Bundle to provide languages dynamically with App Bundle or use SplitInstallManager to do the same.

Useful Links –

To know how we can add localization support in our Android application, read the guide here.

Leave a Reply

Your email address will not be published. Required fields are marked *