The Google Services Gradle Plugin

Introduction

This document explains the functionality of one major component of the Firebase configuration flow: the Google Services gradle plugin.

The google-services plugin has two main functions:

  1. Process the google-services.json file and produce Android resources that can be used in your application's code. See Adding the JSON File more information.
  2. Add dependencies for basic libraries required for the services you have enabled. This step requires that the apply plugin: 'com.google.gms.google-services' line be at the end of your app/build.gradle file so that no dependency collisions are introduced. You can see the result of this step by running ./gradlew :app:dependencies.

Adding the JSON File

The google-services.json file is generally placed in the app/ directory, but as of version 2.0.0-alpha3 of the plugin support was added for build flavors, which would make the following directory structure valid:

app/src/
    main/google-services.json
    dogfood/google-services.json
    myflavor1/google-services.json
    ...

Processing the JSON File

The google-services.json file has the following basic structure:

{
  "project_info": {...},
  "client": [...],
}

The project_info object contains general information about your project, while each member of the client array contains information about the clients (Android apps) that you have added to the project.

When processing the JSON file for your Android app, the plugin only uses the client object that matches your package name (for the current build flavor) based on the following logic:

  • For each member of the client array:
    • Check the value of client_info/android_client_info/package_name
    • If the package name matches this value, return the member object.
  • If none of the members of client match the package name, an exception is thrown.

For the rest of this document we will use {YOUR_CLIENT} to refer to the member of the client array determined by the procedure above.

The main result of the JSON processing is to produce two XML files which you can reference as Android resources in your Java code. Below is an example of each file:

app/build/generated/res/google-services/{build_flavor}/values/values.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="default_web_client_id" translatable="false">1035469437078-e8mr503bun5eaevqctn4u807q4hpi44s.apps.googleusercontent.com</string>
    <string name="gcm_defaultSenderId" translatable="false">1035469437078</string>
    <string name="firebase_database_url" translatable="false">https://myapp.firebaseio.com</string>
    <string name="google_app_id" translatable="false">1:1035469437078:android:73a4fb8297b2cd4f</string>
    <string name="google_api_key" translatable="false">AIzaSyFE3G2eN0XZPl-IeEvZhzKOAgC9vy9imVE</string>
    <string name="google_storage_bucket" translatable="false">myapp.appspot.com</string>
</resources>

gcm_defaultSenderId:

project_info/project_number

google_app_id:

{YOUR_CLIENT}/client_info/mobilesdk_app_id

google_api_key:

{YOUR_CLIENT}/services/api_key/current_key

google_storage_bucket:

project_info/storage_bucket

firebase_database_url:

project_info/firebase_url

default_web_client_id:

{YOUR_CLIENT}/oauth_client/[first client_type 3]

Troubleshooting

Q: When building I get the following error message: "File google-services.json is missing from module root folder. The Google Services Plugin cannot function without it".

A: Go to the Firebase console and follow the steps to connect your application. At the end of the process you will download a file called google-services.json. Copy the file into the app/ folder of your Android Studio project, or into the app/src/{build_flavor} folder if you are using multiple build flavors.

Q: I can't find the symbol "R.string.gcm_defaultSenderId", etc.

A: Make sure the package name in your build.gradle file matches the package name you entered when creating the google-services.json file. If you are not sure, run through the getting started flow again and get a new json file.

Send feedback about...

Need help? Visit our support page.