As part of this article, I will explain the recent experience I had with the resource configurations that broke localization
support in our application.
Here is a sample of what was broken in the French translation
Our client applications are exposed to this UI using a library
.
Following are the files that library developers have put the string resources in 👇
- strings.xml
- 🇨🇦 strings.xml (fr-rCA)
You can see 👆 that they’ve put the French strings in a file that targets the Canadian region.
We have strings for the French language in our application that are not region-specific and stored in a common file👇
Before adding the following configuration to our app-level build.gradle
, it was working as expected
Kotlin
android {
defaultConfig {
...
resourceConfigurations.addAll(listOf("en", "fr"))
}
}
Groovy
android {
defaultConfig {
...
resConfigs "en", "fr"
// Or
resourceConfigurations += ["en", "fr"]
}
}
So what does this config do:
Definition from Android developer’s documentation
Remove unused alternative resources 👇
The Gradle resource shrinker removes only resources that are not referenced by your app code, which means it will not remove alternative resources for different device configurations. If necessary, you can use the Android Gradle plugin’s
resConfigs
property to remove alternative resource files that your app does not need.For example, if you are using a library that includes language resources (such as AppCompat or Google Play Services), then your app includes all translated language strings for the messages in those libraries whether the rest of your app is translated to the same languages or not. If you’d like to keep only the languages that your app officially supports, you can specify those languages using the
resConfig
property. Any resources for languages not specified are removed.
The main advantage of this is that it helps us remove all of the
resources that we do not need for our application
At this point, I hope you understand why localization support for French is broken in our case.
For our application, we added the configuration to specify that we only need the Two following resources:👇
resConfigs "en", "fr"
// Or
resourceConfigurations += ["en", "fr"]
Therefore, Resource Shrinker removes
thefr-rCA 🇨🇦
resources
and we only have [“en”, “fr”] resources available for our application.
It’s time to fix it
- To resolve this issue quickly, there are a few options:
1st
- Update the
build.gradle
to not removefr-rCA 🇨🇦 resources
android {
defaultConfig {
...
resConfigs "en", "fr", "fr-rCA"
// Or
resourceConfigurations += ["en", "fr", "fr-rCA"]
}
}
Job Offers
It’s time to fix it
- To resolve this issue quickly, there are a few options:
1st
- Update the
build.gradle
to not removefr-rCA 🇨🇦 resources
android {
defaultConfig {
...
resConfigs "en", "fr", "fr-rCA"
// Or
resourceConfigurations += ["en", "fr", "fr-rCA"]
}
}
2nd
- Alternately, if you are in control of the library and do not wish to use region-specific resources, you can move them into a file that can be accessed by all regions.
Let’s Connect
https://twitter.com/navczydev?source=post_page—–27a0d20734c6——————————–
https://www.linkedin.com/in/navczydev/
https://github.com/navczydev?source=post_page—–27a0d20734c6——————————–
References
This article is previously published on proandroiddev.com