Xamarin firebase 针对不同的构建配置使用不同的 google-services.json [英] Xamarin firebase different google-services.json for different build configurations

查看:31
本文介绍了Xamarin firebase 针对不同的构建配置使用不同的 google-services.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案

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

尝试

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

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

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

设置您的配置名称,然后单击确定".

6. 选择您刚刚创建的配置(转到构建>配置管理器...>活动解决方案配置并选择它,然后关闭)并配置命令以复制文件从您的文件夹 GoogleServices 到预编译事件中的 [ProjectDir].

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

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

生产配置示例:

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

Visual Studio for Windows 命令:

开发配置示例命令:

COPY/Y "$(ProjectDir)GoogleServicesgoogle-services-development.json";"$(ProjectDir)google-services.json"

生产配置的示例命令:

COPY/Y "$(ProjectDir)GoogleServicesgoogle-services-production.json";"$(ProjectDir)google-services.json"

任何配置的示例命令:

COPY/Y "$(ProjectDir)GoogleServicesgoogle-services-any.json";"$(ProjectDir)google-services.json"

Visual Studio for MAC 命令:

开发配置示例命令:

cp "$(ProjectDir)GoogleServicesgoogle-services-development.json";"$(ProjectDir)google-services.json"

生产配置的示例命令:

cp "$(ProjectDir)GoogleServicesgoogle-services-production.json";"$(ProjectDir)google-services.json"

任何配置的示例命令:

cp "$(ProjectDir)GoogleServicesgoogle-services-any.json";"$(ProjectDir)google-services.json"

现在您可以动态地在构建配置之间切换以执行您想要的 json.逻辑是创建一个假文件,使用复制和替换命令作为预构建命令替换为选定的配置.

In a xamarin.android project, we have 2 build configuration (Basic and Pro) also different Package Name.

On firebase we have registered two apps basic and pro in same project. So now we have two google-services.json file.

Now Problem is that - How can we handle different google-services.json file on different Build Configuration.

解决方案

Solved: Xamarin firebase different google-services.json for different compilation settings.

After trying @SushiHangover solution, the following exception occurred when doing build, something like this:

Failed to Read or Deserialize GoogleServicesJson file: google-services.json System.IO.FileNotFoundException: Could not find file [your root directory]google-services.json'.

at

[your packages directory]Xamarin.GooglePlayServices.Basement.60.1142.1uildMonoAndroid80Xamarin.GooglePlayServices.Basement.targets(66,5): error : Failed to Read or Deserialize GoogleServicesJson file: google-services.json.

Solution: Using Pre-Build event command line

1. Create the folder that contains the different google-services.json files at your [ProjectDir].

Example:

[ProjectDir]/GoogleServices >

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

where [ProjectDir] is your project directory path.

2. Create a fake google-services.json at your [ProjectDir] level.

3. Include that at your project.

4. Go to properties > Build Action> GoogleServicesJson.

5. Create your build configuration. This is what you will use to change the configuration before compiling and executing the project and choosing which GoogleServiceJson will take action with your configuration.

Go to Build> Configuration manager...> Active solution configuration > New...

Set the name of your configuration and click Ok.

6. Select the configuration you just created (Go to Build> Configuration manager...> Active solution configuration and select it, then close) and configure the command to copy the file from your folder GoogleServices to the [ProjectDir] in the pre-compiled event.

Go to Project> [Your project name] properties... > Build > Pre-Build event command line> Edit pre-build

7. Add your command to copy the file specified to your configuration.

Example with production configuration:

Repeat steps from 5 to 7 for each google-service.json you have.

Visual Studio for Windows Commands:

Example command for development configuration:

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

Example command for production configuration:

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

Example command for any configuration:

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

Visual Studio for MAC Commands:

Example command for development configuration:

cp "$(ProjectDir)GoogleServicesgoogle-services-development.json" "$(ProjectDir)google-services.json"

Example command for production configuration:

cp "$(ProjectDir)GoogleServicesgoogle-services-production.json" "$(ProjectDir)google-services.json"

Example command for any configuration:

cp "$(ProjectDir)GoogleServicesgoogle-services-any.json" "$(ProjectDir)google-services.json"

Now you can switch between Build configurations dinamically to execute the json you want. The Logic is create a Fake file to replace with the selected configuration using Copy and Replace commands as Pre-Build commands.

这篇关于Xamarin firebase 针对不同的构建配置使用不同的 google-services.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆