基于Xamarin的构建配置,在iOS/Android项目中使用不同的`google-services.json/GoogleService-Info.plist`。

3
我有一个要求,我的项目在使用不同的配置时应该使用不同的GoogleServices文件,例如在使用调试配置时应该使用文件的调试版本,在发布时应该使用发布版本。类似于Xamarin firebase different google-services,json for different build configurations
当我按照接受的答案操作时,我会收到一个编译时错误,指出命令COPY /Y "$(ProjectDir)GoogleServices\google-services-development.json" "$(ProjectDir)google-services.json"已退出,代码为1。
我尝试了清理构建和清理bin/obj,但没有任何变化。
所以我尝试了这里提到的另一种解决方案,结果是GoogleServices文件(所有文件)都被从项目中排除,如果我构建和运行,什么也不会发生。我不确定这是否有效。
我已经在我的csproj中分别添加了以下行,用于发布和调试。
  <ItemGroup Condition="'$(Configuration)'=='Debug'">
  <GoogleServicesJson Include="Dev\google-services.json">
   <Link>google-services.json</Link>
  </GoogleServicesJson>
  </ItemGroup>

 <ItemGroup Condition="'$(Configuration)'=='Release'">
 <GoogleServicesJson Include="Prod\google-services.json">
  <Link>google-services.json</Link>
 </GoogleServicesJson>
 </ItemGroup>

这里的dev和prod是我本地Android项目中的根文件夹。

欢迎提出任何建议。


如果您更改GoogleJson文件,那么您也需要打包/捆绑标识符。 - R15
实际上,您不需要更改捆绑标识符,只需在两个项目中定义相同的标识符即可。 - FreakyAli
据我所知,当我们从Firebase控制台创建GoogleJson文件时,包名和捆绑标识符与其紧密耦合。您不能将其替换为其他GoogleJson文件。 - R15
我意识到包名和捆绑标识符紧密耦合,但这不是我的问题,因为我知道Firebase如何处理推送通知。我的问题完全不同。 - FreakyAli
1个回答

6

您需要编辑 *.csproj 文件。

使用使用多个 Info.plist 的解决方案(LogicalName 标签)和Condition 标签,您可以随心所欲地操作任何其他文件。

对于 Android,我在 Resources 文件夹中添加了两个 *.json 文件,并将此代码片段添加到我的 *.csproj 文件中:

<ItemGroup Condition=" '$(Configuration)' != 'Release' ">
    <GoogleServicesJson Include="Resources\dev-google-services.json">
        <LogicalName>Resources\google-services.json</LogicalName>
    </GoogleServicesJson>
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' == 'Release' ">
    <GoogleServicesJson Include="Resources\release-google-services.json">
        <LogicalName>Resources\google-services.json</LogicalName>
    </GoogleServicesJson>
</ItemGroup>

在此示例中,我使用 release-google-services.json 作为“Release”构建配置文件,使用 dev-google-services.json 作为其他任何配置文件的配置。
对于 iOS,我在根目录中添加了两个 *.plist 文件,并将以下代码段添加到我的 *.csproj 文件中:
<ItemGroup Condition=" '$(Configuration)' != 'AppStore' ">
    <BundleResource Include="Dev-GoogleService-Info.plist">
        <LogicalName>GoogleService-Info.plist</LogicalName>
    </BundleResource>
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' == 'AppStore' ">
    <BundleResource Include="Release-GoogleService-Info.plist">
        <LogicalName>GoogleService-Info.plist</LogicalName>
    </BundleResource>
</ItemGroup>

这种方法适合我。我想你把这些文件放在哪里以及如何命名它们都无所谓。只需使用所需的LogicalName即可。
此外,您可以将其与其他变量组合以组成更复杂的条件。例如,要在Release配置中使用不同的*.json文件构建两个*.apk,可以:
<ItemGroup Condition=" '$(Configuration)|$(DynamicConstants)' != 'Release|' ">
    <GoogleServicesJson Include="Resources\dev-google-services.json">
        <LogicalName>Resources\google-services.json</LogicalName>
    </GoogleServicesJson>
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)|$(DynamicConstants)' == 'Release|' ">
    <GoogleServicesJson Include="Resources\release-google-services.json">
        <LogicalName>Resources\google-services.json</LogicalName>
    </GoogleServicesJson>
</ItemGroup>

按照以下方式构建您的项目:

msbuild MobileApp.sln /p:Configuration=Release /p:DynamicConstants=DEBUG

当你使用 DEBUG 参数时,你会使用 dev-google-services.json 构建 Release apk。

当你省略 DEBUG 参数时,你会使用 release-google-services.json 构建 Release apk。


伟大的解决方案 - 救命稻草!只是想说,不要手动将JSON文件添加到项目中(通过添加现有文件),因为这会导致它停止工作。它们只需要在文件夹中,VS就会加载它们。 - GuybrushThreepwood

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