I am including feature of gcm in my app, For that i need to maintain two google-services.json one for debug and one for release build. How to do that ??
can i configure gcm without using google-services.json ??
There is no way to use two google-services. json files in a single Android app. The file name is the same between them and they need to be in the same location. So one will overwrite the other in that case.
Usage: How to use this file for different product flavors. There are only 3 steps first step is so simple just remove the google-service. json file from the root of the app/ module and save it your local system directory for the save side, & then make your product flavors like Production & Staging environment.
Adding the JSON File The google-services. json file is generally placed in the app/ directory (at the root of the Android Studio app module).
The current plugin (com.google.gms:google-services:2.1.X
) supports flavors but not types.
So if you create a productflavor you can put the json file in src/$flavorname
Example:
app/src/
flavor1/google-services.json
flavor2/google-services.json
Currently it doesn't work with types (debug, release...) but you can use somenthing like this:
app/src/release/google-services.json
app/google-services.json
In this case the plugin looks in the locations and stops when it finds a google-services.json file.
If you are using a flavor it becomes:
app/src/foo/release/google-services.json
app/src/foo/google-services.json
You can find updated info here.
I'm currently using the following versions: com.google.gms:google-services:4.3.3, com.google.firebase:firebase-messaging:20.2.0
Place your google-services.json
file in your $projectName/app/src/$buildType
directory. For example, place one json file in src/release
and another in src/debug
. You will likely need to create the release & debug folders.
Note: It's a common mistake to add these files in the app folder, be sure you add this in the src folder as described above.
The google-services plugin always looks for the google-services.json file in two directories: First, on the $projectName/app/src/$buildType/google-services.json. If it does not find it here, it goes one level above, to the $projectName/app/google-services.json. So, when you are building the debug version of your app, it will search for the google-services.json on the $projectName/app/src/debug/ directory.
At the link below, see David Ojeda's response.
First, place the respective google_services.json for each buildType in the following locations:
app/src/debug/google_services.json
app/src/test/google_services.json
app/google_services.json
Note: Root app/google_services.json This file should be there according to the build variants copy the json code in the root json file
Now, let’s whip up some gradle tasks in your: app’s build.gradle to automate moving the appropriate google_services.json to app/google_services.json
copy this in the app/Gradle file
task switchToDebug(type: Copy) {
description = 'Switches to DEBUG google-services.json'
from "src/debug"
include "google-services.json"
into "."
}
task switchToRelease(type: Copy) {
description = 'Switches to RELEASE google-services.json'
from "src/release"
include "google-services.json"
into "."
}
Great — but having to manually run these tasks before you build your app is cumbersome. We would want the appropriate copy task above run sometime before: assembleDebug or :assembleRelease is run. Let’s see what happens when :assembleRelease is run: copy this one in the /gradlew file
Zaks-MBP:my_awesome_application zak$ ./gradlew assembleRelease
Parallel execution is an incubating feature.
.... (other tasks)
:app:processReleaseGoogleServices
....
:app:assembleRelease
Notice the :app:processReleaseGoogleServices task. This task is responsible for processing the root google_services.json file. We want the correct google_services.json to be processed, so we must run our copy task immediately beforehand. Add this to your build.gradle. Note the afterEvaluate enclosing.
copy this in the app/Gradle file
afterEvaluate {
processDebugGoogleServices.dependsOn switchToDebug
processReleaseGoogleServices.dependsOn switchToRelease
}
Now, anytime :app:processReleaseGoogleServices is called, our newly defined :app:switchToRelease will be called beforehand. Same logic for the debug buildType. You can run :app:assembleRelease and the release version google_services.json will be automatically copied to your app module’s root folder.
Credit goes to the : Zak Taccardi https://medium.com/google-cloud/automatic-per-variant-google-services-json-configurations-with-gradle-d3d3e40abc0e
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