Xamarin:有没有办法将环境变量注入(inject)到 AndroidManifest.xml 文件中?

标签 xamarin gradle xamarin.android

我有一个开源 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 运行预构建脚本。

Project options view

实际的脚本可能非常简单。就像是:

#!/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/

相关文章:

android - PullToRefresharp 在设备上不起作用

c# - 如何从现有列表中获取与关键字匹配的项目列表并忽略大小写(不区分大小写)?

c# - Microsoft Graph - 获取成员(member)资格

c# - 自动将 F# 转换为 C#

Xamarin Android 绑定(bind)库自定义命名空间无法识别

xaml - 实现类似 OnPlatform/OnIdiom 的系统

gradle - 预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $ 处为 BEGIN_OBJECT 生成 APK

android - 如何在使用 Gradle 时使用 opencv?

java - API 'variant.getJavaCompile()' 已过时

Visual Studio 中的 Android 文档