繁体   English   中英

Xamarin firebase 不同的 google-services,json 用于不同的构建配置

[英]Xamarin firebase different google-services,json for different build configurations

在 xamarin.android 项目中,我们有 2 个构建配置(Basic 和 Pro),也有不同的包名称。

在 firebase 上,我们在同一个项目中注册了两个应用程序 basic 和 pro。 所以现在我们有两个 google-services.json 文件。

现在的问题是 - 我们如何在不同的构建配置上处理不同的 google-services.json 文件。

已解决: Xamarin firebase 针对不同的编译设置使用不同的 google-services.json。

尝试@SushiHangover解决方案后,在进行构建时出现以下异常,如下所示:

无法读取或反序列化 GoogleServicesJson 文件:google-services.json System.IO.FileNotFoundException:找不到文件 [您的根目录]\\google-services.json'。

[你的包目录]\\Xamarin.GooglePlayServices.Basement.60.1142.1\\build\\MonoAndroid80\\Xamarin.GooglePlayServices.Basement.targets(66,5):错误:无法读取或反序列化GoogleServicesJson文件:google-services.json。

解决方案:使用 Pre-Build 事件命令行

1.在您的 [ProjectDir] 创建包含不同 google-services.json 文件的文件夹。

例子:

[项目目录]/GoogleServices >

 google-services-development.json google-services-production.json google-services-any.json

其中[ProjectDir]是您的项目目录路径。

2.在您的 [ProjectDir] 级别创建一个伪造的google-services.json

3. 将其包含在您的项目中。

在此处输入图片说明

4.转到属性> 构建操作> GoogleServicesJson

在此处输入图片说明

5.创建您的构建配置。 这是您在编译和执行项目以及选择哪个 GoogleServiceJson 将对您的配置执行操作之前用于更改配置的内容。

转到构建> 配置管理器...> 活动解决方案配置> 新建...

在此处输入图片说明

设置您的配置名称,然后单击“确定”。

6.选择刚刚创建的配置(进入Build> Configuration manager...> Active solution configuration并选择它,然后关闭)并配置命令将文件从您的文件夹GoogleServices复制到预配置文件中的[ProjectDir]编译事件。

转到项目> [您的项目名称] 属性...> 构建> 预构建事件命令行> 编辑预构建

在此处输入图片说明

7.添加您的命令以将指定的文件复制到您的配置中。

生产配置示例:

在此处输入图片说明

为您拥有的每个 google-service.json 重复步骤 5 到 7。

开发配置命令示例:

COPY /Y "$(ProjectDir)GoogleServices\\google-services-development.json" "$(ProjectDir)google-services.json"

生产配置的示例命令:

COPY /Y "$(ProjectDir)GoogleServices\\google-services-production.json" "$(ProjectDir)google-services.json"

任何配置的示例命令:

COPY /Y "$(ProjectDir)GoogleServices\\google-services-any.json" "$(ProjectDir)google-services.json"

现在您可以动态地在构建配置之间切换以执行您想要的 json。

有关预构建事件的更多信息,请点击此处

您可以根据构建配置有条件地包含/排除.csproj的项目

注意:您需要手动编辑.csproj ,因此请做好备份😜

因此,假设您有两个调试配置,称为DebugPro | DebugBasic ,您可以包含不同的google-services.json (例如来自不同目录),如下所示:

<ItemGroup Condition="'$(Configuration)'=='DebugPro'">
    <GoogleServicesJson Include="google-services.json">
        <Link>Basic\google-services.json</Link>
    </GoogleServicesJson>
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='DebugBasic'">
    <GoogleServicesJson Include="google-services.json">
        <Link>Pro\google-services.json</Link>
    </GoogleServicesJson>
</ItemGroup>

MSBuild 支持一组特定的条件,这些条件可以应用于任何允许使用 Condition 属性的地方。 下表解释了这些条件。

(条件表达式见链接)

我有同样的问题,在 MainActivity 中,您可以根据动态包名称的包名称从 ApplicationKey 和 APIKey 设置动态值我创建了不同的清单文件并添加到 .csproj

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'qaDebug|AnyCPU'">
    <AndroidManifest>Properties\AndroidManifest-qa.xml</AndroidManifest>

https://github.com/jamontes79/xamarin-forms-firebase-sample/blob/master/Droid/MainActivity.cs

Andrespengineer解决方案是个好主意,但我无法了解他是如何进行构建配置切换的,因为您无法在构建事件页面更改它。

所以我只输入了一次并将它从 .csproj 文件的末尾移到了我的属性组的末尾:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release-Prod|AnyCPU'">
    ...
    <PreBuildEvent>COPY /Y "$(ProjectDir)GoogleServices\prod\google-services.json" "$(ProjectDir)google-services.json"</PreBuildEvent>
</PropertyGroup>

对于 Visual Studio for Mac(ver.8.9.4),对我有用的语法是:

cp "${ProjectDir}/GoogleServices/google-services-development.json" "${ProjectDir}/google-services.json"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM