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.
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.
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.
4. Go to properties > Build Action> GoogleServicesJson.
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...
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
7. Add your command to copy the file specified to your configuration.
Example with production configuration:
Repeat steps from 5 to 7 for each google-service.json you have.
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"
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With