【问题标题】:Different `google-services.json/GoogleService-Info.plist` for iOS/Android project based on build configuration in Xamarin基于 Xamarin 中的构建配置的 iOS/Android 项目的不同 `google-services.json/GoogleService-Info.plist`
【发布时间】:2019-12-07 01:30:58
【问题描述】:

所以我有一个要求,我的项目应该为 Android/iOS 使用不同的 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 文件意味着您也需要打包/捆绑标识符。
  • 实际上您不需要更改捆绑标识符,只需在两个项目中定义相同的标识符..
  • 据我所知,当我们从 Firebase 控制台创建它时,包名称和包标识符与 GoogleJson 文件紧密耦合。您只是无法将其替换为其他 GoogleJson 文件。
  • 我知道包名称和包标识符是紧密耦合的,而且这些都不是我的问题,因为我知道 firebase 如何处理推送通知。我的问题是另外一回事

标签: visual-studio xamarin xamarin.forms xamarin.android xamarin.ios


【解决方案1】:

您必须编辑 *.csproj 文件。

使用a solution to use multiple Info.plist(逻辑名称标签)和Condition tag,您可以随意使用任何其他文件。

对于 Android,我在 Resources 文件夹中添加了两个 *.json 文件,并将这个 sn-p 添加到我的 *.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 进行“发布”构建配置,使用 dev-google-services.json 进行任何其他配置。

iOS 也一样。我在根文件夹中添加了两个 *.plist 文件,并将这个 sn-p 添加到我的 *.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 构建发布 apk。

当您省略 DEBUG 参数时,您将使用 release-google-services.json 构建发布 apk。

【讨论】:

  • 很好的解决方案 - 生活更安全!只是想说不要手动将 json 文件添加到项目中(通过添加现有文件),因为它会停止工作。它们只需要在文件夹中,VS 就会加载它们。
猜你喜欢
  • 1970-01-01
  • 2019-07-08
  • 2020-12-17
  • 2016-10-03
  • 2018-12-10
  • 2021-01-04
  • 2016-02-05
  • 2017-10-19
  • 2015-09-28
相关资源
最近更新 更多