Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

android - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...