3

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.

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
  • If you are changing GoogleJson file means you need to package/Bundle identifier too. – R15 Jul 31 '19 at 10:36
  • Not actually you do not need to change bundle identifiers just define the same ones in both projects.. – FreakyAli Jul 31 '19 at 11:00
  • As for as I know, package name and bundle identifier is tightly coupled with GoogleJson file when we are creating it from Firebase console. You just can't replace it with other GoogleJson file. – R15 Jul 31 '19 at 11:03
  • I am aware of the fact that the package name and bundle identifiers are tightly coupled, and those are anyway not my issue as I am aware of how firebase works with push notifications. My problem is something else all together – FreakyAli Jul 31 '19 at 11:12

1 Answers1

6

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.

LGFox
  • 306
  • 4
  • 11
  • Great solution - life safer ! Just wanted to say that don't add the json files manually to the project (via Add Existing File) as it stops it working. They just need to be in the folder and VS will load them. – GuybrushThreepwood Jan 13 '21 at 13:26