What Does Google-Services.JSON Really Do

What does google-services.json really do?

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 {

Firebase google-services.json

The Firebase Realtime Database can now exist in two locations, so it is not automatically created anymore when you create the project, but only once you explicitly go to the database panel in the Firebase console and select a location.

So to get the database URL in your google-services.json, create a database in the Firebase console, and then download the updated file.

google-services.json for different productFlavors

Google included support for flavors in version 2.0 of the play services plugin. Since this version of the gradle plugin com.google.gms:google-services:2.0.0-alpha3

you can do this

Step 1: add to gradle

// To auto-generate google map api key of google-services.json
implementation 'com.google.android.gms:play-services-maps:17.0.0'

Step 2: add to AndroidManifest.xml in the application tag

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key" />

Step 3: download each flavor JSON file from firebase and add it

app/src/
flavor1/google-services.json
flavor2/google-services.json

Version 3.0.0 of the plugin searches for the JSON file in these locations (considering you have a flavor flavor1 and a build type debug):

/app/src/debug/google-services.json
/app/src/debug/flavor1/google-services.json
/app/google-services.json

This worked for me even using flavorDimensions. I have free & paid in one dimension and Mock & Prod in the other dimension. I also have 3 buildTypes: debug, release, and staging. This is how it looks in my project for the FreeProd flavor:

Sample Image

How many google-services.json files will depend on your project's characteristics, but you will need at least one JSON file for every Google project.

If you want more details about what this plugin does with these JSON files, here it is:
https://github.com/googlesamples/google-services/issues/54#issuecomment-165824720

Link to the official docs:
https://developers.google.com/android/guides/google-services-plugin

Blog post with updated info: https://firebase.googleblog.com/2016/08/organizing-your-firebase-enabled-android-app-builds.html

And go here to check the latest version of this plugin: https://mvnrepository.com/artifact/com.google.gms/google-services?repo=google

dynamic google-service.json after apk is generated

What you're asking isn't possible. The values from google-services.json are read at build time and added to your APK. There's no way to go back and changes those values later.

If you want to initialize Firebase based on values that can only be known when the app is launched, then you should NOT apply the google-sevices plugin in build.gradle, and instead call FirebaseApp.initializeApp() manually and pass the correct values that you see in google-services.json.

Read:

  • https://firebase.googleblog.com/2017/03/take-control-of-your-firebase-init-on.html
  • https://developers.google.com/android/guides/google-services-plugin


Related Topics



Leave a reply



Submit