升级到 Gradle 3.3 后出现翻译错误

MissingTranslation Errors after Upgrading to Gradle 3.3

自升级到 Gradle 3.3 以来,由于缺少翻译错误,我在构建代码时遇到了问题:

Error: xxx is not translated in "af" (Afrikaans), "am" (Amharic), "ar" (Arabic), "az" (Azerbaijani), "az-AZ" (Azerbaijani: Azerbaijan), "be" (Belarusian), "bg" (Bulgarian), "ca" (Catalan), [...], "zh-TW" (Chinese: Taiwan), "zu" (Zulu) [MissingTranslation]

大多数报告的语言都是我的项目中包含的第 3 方模块支持的语言,现在它似乎定义了整个项目支持的语言,给我所有字符串的这种错误未翻译成上述语言。在升级到 Gradle 3.3 之前,这不会导致任何问题。

我考虑了以下解决方案:

  1. 从其他模块中删除多余的翻译。我想避免这种情况,因为这些模块是外部的,不必要地更改它们会真正损害我项目的可维护性。
  2. 禁用 "incomplete translation" Lint 检查 - 对 SO 上类似问题的最常见建议。这是次优的,因为我想知道我的代码中缺少的翻译(到目前为止工作)。除此之外,禁用检查并不能消除错误。
  3. 在 build.gradle 中定义支持的配置,如 this answer 中所述。我喜欢这个选项(指定语言而不是依赖模块中可用的翻译),但它也做了一些奇怪的事情:我遇到了标记为 translatable = false.
  4. 的字符串的缺失翻译错误

现在,我再次降级到以前的 Gradle 版本。但是修复这些构建错误的最佳方法是什么?

在build.gradle中添加以下代码

lintOptions {
    disable 'MissingTranslation'
}

希望自从我几个月前发布这个问题后可能有更正,我检查了情况。

问题似乎是由 Gradle 插件 2.3.0 引入的,而不是我在问题中建议的 Gradle 3.3 本身。降级插件可以避免错误,但很难成为长期解决方案。

我发现问题中的选项 3 是处理它的最佳方式:将其添加到应用程序的 build.gradle:

android {
    defaultConfig {
        ...
        resConfigs "en", "fr"
    }
}

这在 Googles documentation and, as mentioned, also in this answer 中有描述。它会删除所有不必要的资源 - 以及 warnings/errors 以及它们。

引用文档:

The Gradle resource shrinker removes only resources that are not referenced by your app code, which means it will not remove alternative resources for different device configurations. If necessary, you can use the Android Gradle plugin's resConfigs property to remove alternative resource files that your app does not need.

For example, if you are using a library that includes language resources (such as AppCompat or Google Play Services), then your APK includes all translated language strings for the messages in those libraries whether the rest of your app is translated to the same languages or not. If you'd like to keep only the languages that your app officially supports, you can specify those languages using the resConfig property. Any resources for languages not specified are removed.

我得到的“误报”(不可翻译字符串的缺失翻译错误)是针对在多个模块中定义的字符串。重命名字符串或为它们提供翻译解决了这个问题。这似乎也是在 Gradle 插件 2.3.0 中引入的。