我有一个开源 Xamarin 项目,它在 AndroidManifest.xml
中有一个 API key 。文件。目前,我已通过 .gitignore
处理了该问题。 -ing AndroidManifest.xml
,但这是一个困惑的解决方案,它阻止我使用像 AppCenter 这样的自动构建环境。
可以使用 Gradle 文件完成:https://developer.android.com/studio/build/manifest-build-variables
但是,在 Xamarin 中,我们无法访问 Gradle。我在 Xamarin 项目中找不到这样做的方法。我错过了一些明显的东西吗?
最佳答案
不幸的是,在读取 AndroidManifest.xml(或 iOS 版本的 Info.plist)时,环境变量不可用。
我发现更改这些文件中的配置值(包括 com 标识符)的一种方法是通过 Project Options -> Build -> Custom Commands 运行预构建脚本。
实际的脚本可能非常简单。就像是:
#!/bin/sh
cat ./manifest/prod/AndroidManifest.xml > Properties/AndroidManifest.xml
每个构建配置都需要不同版本的脚本。在我的情况下,我需要根据环境改变 com 标识符,以便将生产与开发推送通知与 Pushy.me 隔离开来。
这会显示在您的 .csproj 文件中,如下所示:
<CustomCommands>
<CustomCommands>
<Command>
<type>BeforeBuild</type>
<command>set_manifest_dev.sh</command>
<workingdir>${ProjectDir}</workingdir>
</Command>
</CustomCommands>
</CustomCommands>
关于Xamarin:有没有办法将环境变量注入(inject)到 AndroidManifest.xml 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54006515/