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

后端 未结 1 665
眼角桃花
眼角桃花 2020-12-15 22:32

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

相关标签:
1条回答
  • 2020-12-15 22:47

    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!

    0 讨论(0)
提交回复
热议问题