Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easily remove unnecessary localization resources from added libraries in release APK

My app is quite simple and does not need a lot of localization.

I supply default language (in English) and German - this is all I ever want and will ever supply, as the app is completely focused in Germany.

As I recently added Google Play Services library, I face the problem that 56 (!!!) additional languages have been added to my app, as the Google Play Store tells me. Reason is: the library comes with many more language resources that I do NOT want in my app. It simply does not make any sense if a Google Play dialog pops up in French when the rest was only English/German.

I do not want to manually delete resources from the library project, this is tedious and error prone. Plus, maybe I will have another app relying on the same library and there I want more languages?

So - how can I accomplish this??

Thanks!

like image 846
Zordid Avatar asked Mar 26 '14 08:03

Zordid


1 Answers

I understand your problem, the easy solution is to remove all extra languages from the library but, you need to do it with every new version of Google Play Services, and as you says, if you need other languages in other apps, that wouldn't be the best option.

Instead try to force to your app to use German or English as default:

You need to add this code in your Application class

@Override
public void onCreate() {
    super.onCreate();
    avoidOtherLanguages();
    // your code here
}

@Override
public void onConfigurationChanged() {
    super.onConfigurationChanged();
    avoidOtherLanguages();
    // your code here
}

public void avoidOtherLanguages() {
    if (!Locale.getDefault().getLanguage().equals(Locale.GERMAN.getLanguage()))
    {
        // when other than german, use english
        final Configuration configuration = getResources().getConfiguration();
        configuration.locale = Locale.ENGLISH;
        getResources().updateConfiguration( configuration, getResources().getDisplayMetrics() );
    }   
}

I hope it works for you!

** UPDATED: SOLUTION **

Hi come up with a solution after a lot of googling! If you are using gradle as build system you can do this in your build.gradle file:

   .....
   defaultConfig {
    minSdkVersion 15
    targetSdkVersion 22
    versionCode 75
    versionName "1.0.0"

    resConfigs "en", "de"
}
...

use resConfig to tell gradle that you are only using these locales configuration, all other languages in your libraries will be stripped out of the APK bundle!

Let me know if that worked for you!

like image 93
Tano Avatar answered Nov 02 '22 16:11

Tano