Android - What does google-services.json really do?
What is this file really for
google-services.json contains developer credentials and configuration settings, which is needed to verify while connecting with GoogleApiClient. Though your service is working fine with your test device as it is detecting your developer account but after releasing your app in public, it will not work without the json file. So don’t delete it.
The Official Documentation says:
The application builds a GoogleApiClient, specifying which scopes and APIs the application will access. When the GoogleApiClient connects, the user is signed in.
See the how it works section
How does google-services.json file affects your android studio project?
I investigated a bit regarding the google-services plugin and json and found the sources to this plugin.
First things first
The gradle-plugin google-services that is referenced by classpath and with apply is a build-time plugin only! So it only influences the build-process of your app, but not the runtime-process!
This plugin is only meant as a quickstart-helper to integrating Google-services quickly in your app. Obviously, the process is somewhat convoluted and not documented, so Google should have made it clear what this process does.
In fact, I found the source code for the plugin version com.google.gms:google-services:1.4.0-beta3 and didnt find any specific reference in it regarding appinvites nor did I find any Google API for App Invites! (But maybe it just uses a generic API project with its project id, I didnt try this)
What it does
The google-services gradle-plugin looks for the mentioned google-services.json file in your app-module. Then it looks for configured settings like project-id’s and tracking-id’s and such, generated by the Google API developer console into the google-services.json file. From the settings it found, Android resource values are generated into the following path:
"$project.buildDir/generated/res/google-services/$variant.dirName/values/values.xml"
For example for a debug-build of your app:
just in short for second url, if you add google-services.json in your project there must be a auto-generated
google-services
folder fordebug
variant in this path
"app/build/generated/res/google-services/debug/values/values.xml"
E.g. if you followed the GCM tutorial, the JSON file would include the API project’s id as the following android-resource:
<string name="gcm_defaultSenderId">project-id</string>
So this plugin and JSON file are not essential to running or publishing your app, it is just a quickstart helper to generate some basic android-resource files for easier integration of specific Google API features.
Notice in the source code referenced below that the google-services plugin always generates those android-resources for every app-variant that is defined in your app/build.gradle.
If you don’t want that, you should use those generated resources in the app-variants you want, and delete the others. Don’t forget to remove the google-services plugin apply from app/build.gradle, or else it will be regenerated for all app-variants.
What it does not
This plugin and JSON-file do NOT directly influence the inner workings of said Google-features for your app! If you already have followed older tutorials on developer.android.com on how to integrate e.g. GCM or Google Analytics, then you don’t even need to integrate either the gradle-plugin google-services or the google-services.json file!
Notice about where I found the sources
After you integrated the google-services gradle-plugin and when sync your project, Gradle automatically downloads the google-services dependency to a path similar to this (on Windows, you might need to look into your home/.gradle for Linux):
C:\Users\user\.gradle\caches\modules-2\files-2.1\com.google.gms\google-services\1.4.0-beta3\f1580f62e3be313eba041ce19b64fd3f44cf8951\google-services-1.4.0-beta3-sources.jar
If you extract this jar-file, you will find two files:
GoogleServicesPlugin.groovy
GoogleServicesTask.java
which contain the plain source code of the gradle-plugin.
GoogleServicesPlugin.groovy
contains the handling of the app-variants and basic definitions of paths etc.
GoogleServicesTask.java
contains the actual task-definition, look for the following method to see what it really does:
@TaskAction
public void action() throws IOException {
What to do, to make it done?
add google-services dependency in project_level
build.gradle, you can also use version 3.0.0
if you are using app_compact library.
// Top-level build.gradle file
classpath 'com.google.gms:google-services:2.1.2'
now in app_level
build.gradle you have to add at the bottom.
// app-level build.gradle file
apply plugin: 'com.google.gms.google-services'
Note: Adding this line at the bottom of the gradle file is really important. Otherwise Gradle builds won’t give you any errors, but it won’t just work properly.
Where to put google-service.json file in your structure.
case 1.) if you have no build_flavor just put it in inside /app/google-service.json
folder.
case 2.) if you have multiple build_flavor and you have different-different google_services.json files put inside app/src/build_flavor/google-service.json
.
case 3.) if you have multiple build_flavor and you have single google_services.json file put inside app/google-service.json
.