如何使用 Xamarin.Android 从最终 APK 文件中删除未使用的语言?

How do I remove unused languages from the final APK file with Xamarin.Android?

我已将一些 Android 支持库添加到我的项目中,现在我基本上面临与此问题中所述相同的问题: Android Studio exports strings from support library to APK

由于我无法在 Xamarin 中使用 Gradle 设置,因此我无法使用 Whosebug 答案中描述的解决方案。

有谁知道如何在我的最终 APK 文件中只保留特定的本地化?

现在无法真正检查,但您是否看过解决方案 .csproj 文件中的 AndroidLinkSkipAndroidLinkMode (reference) 标签?

所以,它会是这样的

<AndroidLinkMode>Full</AndroidLinkMode>
<AndroidLinkSkip>Mono.Android.Export;I18N;I18N.West</AndroidLinkSkip>

另外,看看MandroidI18n。来自上面的same reference

Specifies the internationalization support included with the Application, such as collation and sorting tables. The value is a comma- or semicolon-separated list of one or more of the case-insensitive values

<MandroidExtraArgs>-i18n=west</MandroidExtraArgs> 

<MandroidI18n>West</MandroidI18n>

通常,在 Xamarin 中,AndroidManifest 处理库使用的特殊说明 Android.App.UsesLibraryAttribute(string name, bool required) 设置将在生成的 Manifest.XML.

中的特定包含和排除

据我所知,只有三种方法可以设置 link 排除项,@sunseeker 提到了第一种和第二种方法,但是 Xamarin 文档和开发说明强烈建议不要使用上面指出的 Full 和一般提倡使用以下内容:

SdkOnly(默认)

上面提到的第二个也是针对特定的排除项,也建议不要使用它,除非您确定特定的包不会被 "behind the scenes" 进一步调用 class层次结构。

最后在第三种方法中设置LinkMode为None,具体linkings使用AndroidManifest接口规定。

其他一些提高效率的方法是:

  1. 至少在调试时将 AndroidUseSharedRuntime 属性 设置为 true 以减小包大小。

  2. 当你有一个稳定的构建来预编译包含的库时,将 AotAssemblies 属性 设置为 true。

  3. 除非是发布版本,否则将 EmbedAssembliesIntoApk 设置为 false。

关于 Xamarin 的构建知识就到此为止,希望对您有所帮助。

所以我终于设法以一种理智的方式做到了这一点

  1. https://ibotpeaches.github.io/Apktool/
  2. 下载 Apktool
  3. 使用 Xamarin 创建最终的 .apk 并使用 apktool d MyApp.apk
  4. 反编译
  5. 进入Apktool创建的MyApp目录,寻找res目录
  6. 删除所有以您不需要的语言标识符结尾的 values 目录,例如,如果您的应用仅支持德语,请删除 values-frvalues-es 等...,但不是 values-de不要 删除 non-language 个目录,例如 values-v11!
  7. 使用 apktool b MyApp
  8. 重新编译您的应用
  9. 重新编译的应用包现在在MyApp/dist/MyApp.apk中。获取此文件并使用 signtool 对其进行签名,然后对其进行 zipalign。
  10. 将 apk 上传到 Google 播放

我确信这个过程可以自动化,一旦我有一个脚本,我就会更新这个答案。

您是否尝试过 Borris Spinner 的解决方案:

您可以在项目文件中提供AndroidResgenExtraArgs并添加-c en,de

查看 aapt 文档:

-c  specify which configurations to include.  The default is all
       configurations.  The value of the parameter should be a comma
       separated list of configuration values.  Locales should be specified
       as either a language or language-region pair.  Some examples:
            en
            port,en
            port,land,en_US