Xamarin firebase different google-services.json for different build configurations
Asked Answered
S

6

6

In a xamarin.android project, we have 2 build configuration (Basic and Pro) also different Package Name.

On firebase we have registered two apps basic and pro in same project. So now we have two google-services.json file.

Now Problem is that - How can we handle different google-services.json file on different Build Configuration.

Shufu answered 25/1, 2019 at 15:44 Comment(0)
S
11

Solved: Xamarin firebase different google-services.json for different compilation settings.

After trying @SushiHangover solution, the following exception occurred when doing build, something like this:

Failed to Read or Deserialize GoogleServicesJson file: google-services.json System.IO.FileNotFoundException: Could not find file [your root directory]\google-services.json'.

at

[your packages directory]\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.

Solution: Using Pre-Build event command line

1. Create the folder that contains the different google-services.json files at your [ProjectDir].

Example:

[ProjectDir]/GoogleServices >

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

where [ProjectDir] is your project directory path.

2. Create a fake google-services.json at your [ProjectDir] level.

3. Include that at your project.

enter image description here

4. Go to properties > Build Action> GoogleServicesJson.

enter image description here

5. Create your build configuration. This is what you will use to change the configuration before compiling and executing the project and choosing which GoogleServiceJson will take action with your configuration.

Go to Build> Configuration manager...> Active solution configuration > New...

enter image description here

Set the name of your configuration and click Ok.

6. Select the configuration you just created (Go to Build> Configuration manager...> Active solution configuration and select it, then close) and configure the command to copy the file from your folder GoogleServices to the [ProjectDir] in the pre-compiled event.

Go to Project> [Your project name] properties... > Build > Pre-Build event command line> Edit pre-build

enter image description here

7. Add your command to copy the file specified to your configuration.

Example with production configuration:

enter image description here

Repeat steps from 5 to 7 for each google-service.json you have.

Visual Studio for Windows Commands:

Example command for development configuration:

COPY /Y "$(ProjectDir)GoogleServices\google-services-development.json" "$(ProjectDir)google-services.json"

Example command for production configuration:

COPY /Y "$(ProjectDir)GoogleServices\google-services-production.json" "$(ProjectDir)google-services.json"

Example command for any configuration:

COPY /Y "$(ProjectDir)GoogleServices\google-services-any.json" "$(ProjectDir)google-services.json"

Visual Studio for MAC Commands:

Example command for development configuration:

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

Example command for production configuration:

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

Example command for any configuration:

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

Now you can switch between Build configurations dinamically to execute the json you want. The Logic is create a Fake file to replace with the selected configuration using Copy and Replace commands as Pre-Build commands.

Sideslip answered 13/2, 2019 at 22:13 Comment(3)
Andrespengineer couldn't you just leave the development one in the [ProjectDir] level and copy the Production one on the Prod build? – Roseroseann
@j.t.h.I don't know now, but not then. What happens is that the files are replaced. If you have a file A (dev) and another B (prod) and replace A with B (prod build), when you switch to dev build, how will you replace B(prod) with A(dev), if A(dev) was literally removed with prod build? You need A => replace C and B => replace C, A and B should never be changed by a build operation. – Sideslip
@ Andrespengineer ah I understand now. It replaces the file in the directory and not just for the build. Did you find this solution to work better than the one mention by @SushiHangover? I tried his solution and I read about switching the FILE and INCLUDE but it shows both google-services-josn file in the project directory. – Roseroseann
R
4

You can conditionally include/exclude items in the .csproj based upon the Build Configuration

Note: You need to manually edit the .csproj, so make a backup 😜

So assuming you have two debug configurations called DebugPro|DebugBasic, you could include a different google-services.json (say from a different directory) like so:

<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 supports a specific set of conditions that can be applied wherever a Condition attribute is allowed. The following table explains those conditions.

(see link for condition expressions)

Ropable answered 25/1, 2019 at 16:7 Comment(7)
So if I put different json files in different directory it still looks for googleservices json file in root directory.If I remove json file from root then build fails – Auten
Are you using the Link property? – Ropable
Yes..I am using exact code like you have provided..but after that build is failing saying could not find googleservices.json file in root folder – Auten
@SushiHangover-Are you able to implement this with your approach..Could you please guide me with detailed steps – Auten
@Auten The text of the Include attribute and the Link element should be the other way round, so Include is the path to the real file and Link is just google-services.json. – Permatron
As @Permatron suggested the text between the Link and Include needs to be changed. If you get a Failed to Read or Deserialize GoogleServicesJson file: google-services.json it also might be to the occurence of special characters in the json file – Pronate
Hey @Ropable I tried replicating the above-proposed solution but I am not sure if this is working or not can you please take a look #57258902 – Myrtlemyrvyn
C
2

I had same issue, in MainActivity you can set dynamic values from ApplicationKey and APIKey based on the package name for dynamic package name I have create different Manifest file and added in .csproj

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'qaDebug|AnyCPU'">
    <AndroidManifest>Properties\AndroidManifest-qa.xml</AndroidManifest>

https://github.com/jamontes79/xamarin-forms-firebase-sample/blob/master/Droid/MainActivity.cs

Carnegie answered 12/2, 2020 at 13:58 Comment(0)
A
0

The solution from Andrespengineer is a good idea, but I couldn't find out how he's doing the switching of the build configuration, because you cannot change it at the build event page.

So I just entered it once and moved it from the end of the .csproj file to the end of my property group:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release-Prod|AnyCPU'">
    ...
    <PreBuildEvent>COPY /Y "$(ProjectDir)GoogleServices\prod\google-services.json" "$(ProjectDir)google-services.json"</PreBuildEvent>
</PropertyGroup>
Arie answered 23/9, 2019 at 12:35 Comment(1)
How to get this work in Mac? COPY command doesn't exist when build using Visual Studio on Mac. – Fill
P
0

For Visual Studio for Mac (ver.8.9.4) the syntax that worked for me is:

cp "${ProjectDir}/GoogleServices/google-services-development.json" "${ProjectDir}/google-services.json"
Pye answered 1/4, 2021 at 9:28 Comment(0)
C
0

For people with VisualStudio on Mac (mine is v 17.6), based on @Andrespengineer answer, I had to add a custom-command this way:

  1. Right click on Android project and choose "Properties"
  2. Click on Build -> Custom Commands
  3. I chose "Before Build"
  4. Command: cp <directory>/google-services-<custom name extension>.json google-services.json
  5. Repeat steps 3-4 for each configuration
Code answered 12/7 at 16:37 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.