如何轻松地从发布 APK 中添加的库中删除不必要的本地化资源 [英] How to easily remove unnecessary localization resources from added libraries in release APK

查看:15
本文介绍了如何轻松地从发布 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.

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

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??

谢谢!

推荐答案

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

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() );
    }   
}

希望对你有用!

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

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"
}
...

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

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!

这篇关于如何轻松地从发布 APK 中添加的库中删除不必要的本地化资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆