Xamarin Firebase:不同的构建配置需要不同的google-services.json文件

6
在xamarin.android项目中,我们有两个构建配置(Basic和Pro),同时也有不同的软件包名称。
在firebase上,我们在同一个项目中注册了基本版和专业版两个应用程序。因此,现在我们有两个google-services.json文件。
现在的问题是 - 如何在不同的构建配置下处理不同的google-services.json文件。
5个回答

11

已解决: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): error : Failed to Read or Deserialize GoogleServicesJson file: google-services.json。

解决方案:使用预构建事件命令行

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

例如:

[ProjectDir]/GoogleServices >

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

[ProjectDir]为您的项目目录路径。

2. 在您的[ProjectDir]级别上创建一个虚假的google-services.json

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

enter image description here

4. 进入属性 > 构建操作 > GoogleServicesJson。

enter image description here

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

进入生成> 配置管理器...> 活动解决方案配置 > 新建...

enter image description here

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

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

进入项目> [您的项目名称]属性... > 构建 > 预生成事件命令行 > 编辑预生成

enter image description here

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

使用生产配置示例:

enter image description here

重复步骤5到7,为每个google-service.json创建一个构建配置。

Visual Studio for Windows 命令:

用于开发配置的示例命令:

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"

Visual Studio for MAC 命令:

用于开发配置的示例命令:

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

用于生产配置的示例命令:

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

用于任何配置的示例


Andrespengineer,你不能只把开发版本留在[ProjectDir]级别,然后将生产版本复制到生产构建中吗? - j.t.h.
@j.t.h.我现在不知道,但当时不是这样的。发生的情况是文件被替换了。如果你有一个文件A(dev)和另一个B(prod),并用B(prod build)替换A,当你切换到dev build时,如果A(dev)被prod build字面上删除了,你将如何用A(dev)替换B(prod)?你需要A => 替换C和B => 替换C,A和B永远不应该被构建操作改变。 - Andrespengineer
@Andrespengineer 啊,我现在明白了。它替换的是目录中的文件,而不仅仅是编译时。你觉得这个解决方案比 @SushiHangover 提到的那个更好吗?我尝试了他的解决方案,并且我读到了关于切换 FILE 和 INCLUDE 的文章,但它在项目目录中显示了两个 google-services-json 文件。 - j.t.h.

4
你可以根据构建配置条件性地包含/排除.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属性的地方。下表解释了这些条件。

(有关条件表达式,请参见链接)


所以,如果我将不同的JSON文件放在不同的目录中,它仍然会在根目录中查找googleservices JSON文件。如果我从根目录中删除JSON文件,则构建将失败。 - uncle_scrooge
你是否正在使用 Link 属性? - SushiHangover
@SushiHangover- 你能否用你的方法来实现这个功能?能否请你详细指导我一下步骤? - uncle_scrooge
1
@uncle_scrooge,“Include”属性和“Link”元素的文本应该颠倒过来,这样“Include”就是实际文件的路径,而“Link”只是“google-services.json”。 - Paul
1
正如@Paul所建议的那样,需要更改链接和包含之间的文本。 如果您收到“无法读取或反序列化GoogleServicesJson文件:google-services.json”的错误消息,则也可能是JSON文件中特殊字符的出现导致的。 - rahulinaction
显示剩余2条评论

2

0

对于Visual Studio for Mac(版本8.9.4),我使用的语法是:

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

0

Andrespengineer的解决方案是一个好主意,但我找不到他如何切换构建配置,因为你不能在构建事件页面更改它。

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

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

如何在 Mac 上使其工作?使用 Visual Studio 在 Mac 上构建时,COPY 命令不存在。 - Arun Gupta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接