What does google-services.json really do?

AndroidJsonGoogle AnalyticsGoogle Play-ServicesGoogle Cloud-Messaging

Android Problem Overview


I work on adding Google Analytics and GCM services to my current app. On the guide for both services implementation, google asks developer to generate a json file: google-services.json and put it under root directory of the app.

I found that even if I delete this json file from my app, the services still works.

Just want to know for sure, what is this file really for? What its usage and how does it work?

Android Solutions


Solution 1 - Android

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:

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 { 

Solution 2 - Android

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.

Solution 3 - Android

Add google-services.json to your module and do a CLEAN and A REBUILD. A xml file will be generated in app/build/generated/res/google-services/debug/values/values.xml with your project properties and you can easilly access then like normal xml string. Example:

String serverClientId = getString(R.string.default_web_client_id);

there is a list with all strings and more info in google-service.json doc

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArthur WangView Question on Stackoverflow
Solution 1 - Androidarne.jansView Answer on Stackoverflow
Solution 2 - AndroidMohammad ArmanView Answer on Stackoverflow
Solution 3 - AndroidBeto CaldasView Answer on Stackoverflow