Different `google-services.json/GoogleService-Info.plist` for iOS/Android project based on build configuration in Xamarin

折月煮酒 提交于 2020-01-05 01:09:28

问题


So I have a requirement where my project should use different GoogleServices files for Android/iOS while using different configurations like for eg while I am using the debug configuration it should use the debug version of the file and in the release, it should use the release version.

Something similar to Xamarin firebase different google-services,json for different build configurations

When I follow the accepted the answer I get a compile-time error saying

The command COPY /Y "$(ProjectDir)GoogleServices\google-services-development.json" "$(ProjectDir)google-services.json" exited with code 1.

I tried clean build and cleaning bin/obj nothing changed.

So I tried the other solution mentioned here and what happens is the GoogleServices files(all of them) are excluded from the project and nothing happens if I build and run. I am unsure if this is even working or not.

I have added the following lines in my csproj for release and debug respectively

  <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>

Where dev and prod are root folders in my native android project

Any suggestions are welcome.


回答1:


You have to edit *.csproj file.

Using a solution to use multiple Info.plist (LogicalName tag) and Condition tag you can play with any other files all you want.

For Android I added two *.json files to Resources folder and added this snippet to my *.csproj file:

<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>

In this example I use release-google-services.json for the "Release" build configuration, and dev-google-services.json for any other configurations.

Same for iOS. I added two *.plist files to root folder and added this snippet to my *.csproj file:

<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>

This approach works for me. I guess it doesn't matter where you put these files and how you name them. Just use the LogicalName that you need.

Also, you can combine it with other variables to compose more complicated conditions. For example, in order to build two *.apk in Release configuration with different *.json files you can:

<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>

Build your project like this:

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

When you use DEBUG parameter you build Release apk with dev-google-services.json.

When you omit DEBUG parameter you build Release apk with release-google-services.json.



来源:https://stackoverflow.com/questions/57258402/different-google-services-json-googleservice-info-plist-for-ios-android-projec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!