Dwarsoft

A blog where we show how technology enables us to build the products and provide top service. We are based in Chennai.

How To Provide Languages Dynamically Using App Bundle

--

Android App Bundle

When Google introduced Android App Bundle, it was remarkable as it lowered the download size of every app by approximately 30–60%. I did get my hands on it as soon as it was launched and wanted to try it out with one of my applications. I did so and it was a good experience. However, I faced one issue. My application supports both English and Hindi language within the application and the user can change the language dynamically anytime they want.

The feature was working quite well when I was testing with APKs and launched it on Playstore. However, when I introduced AAB of my application, the feature wasn’t working as the language did not change when being switched. After looking into the issues, I found that Android App Bundle downloads the APK only with what languages are set in the default section of the device.

I was going through and tried to solve the problem as not every user of the application will have both English and Hindi as their default language but would want to use it within the application. I deployed a quick fix and it worked but was not the optimal way to do it.

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 of overhead. But if your application supports many languages, the solution does not seem to be optimal as it instructs the AAB to download all the language resources.

A better and optimal solution for this use case is by using the 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 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.

--

--

Dwarsoft
Dwarsoft

Published in Dwarsoft

A blog where we show how technology enables us to build the products and provide top service. We are based in Chennai.

Varun Dwarkani
Varun Dwarkani

Written by Varun Dwarkani

I am a tech enthusiast and I look forward to learning new technologies and adapting them. I have rich knowledge in Android development and will be writing on it

Responses (1)