如何在发布APK中添加库中轻松删除不必要的本地化资源

Zor*_*did 15 resources android localization strip apk

我的应用程序很简单,不需要很多本地化.

我提供默认语言(英语)和德语 - 这是我想要和将来提供的所有内容,因为该应用程序完全专注于德国.

正如我最近添加的Google Play服务库,我面临的问题是我的应用程序中添加了56(!!!)其他语言,正如Google Play商店告诉我的那样.原因是:图书馆附带了更多我不希望在我的应用程序中使用的语言资源.如果Google Play对话框以法语弹出,其余部分只有英语/德语,那就没有任何意义.

我不想手动删除库项目中的资源,这很繁琐且容易出错.另外,也许我会有另一个应用程序依赖于同一个库,我想要更多语言?

那么 - 我怎么能做到这一点?

谢谢!

Tan*_*ano 13

我理解您的问题,简单的解决方案是从库中删除所有额外的语言,但是,您需要在每个新版本的Google Play服务中执行此操作,正如您所说,如果您在其他应用中需要其他语言,则不会是最好的选择.

而是尝试强制您的应用程序使用德语或英语作为默认值:

您需要在Application类中添加此代码

@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() );
    }   
}
Run Code Online (Sandbox Code Playgroud)

我希望这个对你有用!

**更新:解决方案**

嗨,经过大量的谷歌搜索后,你想出了一个解决方案!如果您使用gradle作为构建系统,则可以在build.gradle文件中执行此操作:

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

    resConfigs "en", "de"
}
...
Run Code Online (Sandbox Code Playgroud)

使用resConfig告诉gradle你只使用这些语言环境配置,你的库中的所有其他语言都将从APK包中删除!

如果这对您有用,请告诉我!