As part of adding Firebase to your Android project, you need to add the
Gradle plugin and a
configuration file to your project.
If you add Firebase to your Android project using the Firebase console, the Management REST API, or the Firebase CLI, you must manually add the Gradle plugin and JSON config file to your project. However, if you use the Firebase Assistant for Android Studio, these tasks are automatically done for you during setup.
Add the Gradle plugin
You add the
Google services Gradle plugin () to your
build.gradle file:
dependencies {
classpath 'com.google.gms:google-services:4.5.0'
// ...
}
The Google services Gradle plugin has two main functions:
Processes the
config file and produces Android resources that can be used in your app's code.google-services.json Adds dependencies for basic libraries required for the services you have enabled. This step requires that you apply the Google services Gradle plugin in your
app/build.gradlefile, like so:
apply plugin: 'com.google.gms.google-services'You can see the result of this step by running
./gradlew :app:dependencies.
Add the JSON config file
The config file is generally placed in the app/
directory (at the root of the Android Studio app module). As of version 2.2.0
the plugin supports build type and product flavor specific JSON files. All of
the following directory structures are valid:
// dogfood and release are build types.
app/
google-services.json
src/dogfood/google-services.json
src/release/google-services.json
...
When product flavors are in use these more complicated directory structures are also valid.
// free and paid are product flavors.
app/
google-services.json
src/dogfood/paid/google-services.json
src/release/free/google-services.json
...
How the JSON config file is processed
The config 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 type)
based on the following logic:
For each member of the
clientarray, the plugin checks the value ofclient_info/android_client_info/package_name. If the package name matches this value, it returns the member object.If none of the members of
clientmatch the package name, the plugin throws an exception.
The remainder of this document uses YOUR_CLIENT to
refer to the member of the client array determined by the logic described
above.
Production of XML files
The main result of the JSON processing is to produce two XML files that you can reference as Android resources in your code. The following are examples of each file:
app/build/generated/res/google-services/{build_type}/values/values.xml<?xml version="1.0" encoding="utf-8"?> <resources> <! -- Present in all applications --> <string name="google_app_id" translatable="false">1:1035469437089:android:73a4fb8297b2cd4f</string> <! -- Present in applications with the appropriate services configured --> <string name="gcm_defaultSenderId" translatable="false">1035469437089</string> <string name="default_web_client_id" translatable="false">337894902146-e4uksm38sne0bqrj6uvkbo4oiu4hvigl.apps.googleusercontent.com</string> <string name="ga_trackingId" translatable="false">UA-65557217-3</string> <string name="firebase_database_url" translatable="false">https://example-url.firebaseio.com</string> <string name="google_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string> <string name="google_crash_reporting_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string> <string name="project_id" translatable="false">mydemoapp</string> </resources>app/build/generated/res/google-services/{flavor}/{build_type}/xml/global_tracker.xml<?xml version="1.0" encoding="utf-8"?> <resources> <string name="ga_trackingId" translatable="false">UA-65557218-3</string> </resources>
Values in the XML files
Every value in the XML files is present in the
config file at the locations listed below. If your
Android project has some configuration that prevents you from using the
Gradle plugin, you can safely recreate the
XML files manually using these values.
google_app_idYOUR_CLIENT/client_info/mobilesdk_app_idgcm_defaultSenderIdproject_info/project_numberdefault_web_client_idYOUR_CLIENT/oauth_client/client_id (client_type == 3)ga_trackingIdYOUR_CLIENT/services/analytics-service/analytics_property/tracking_idfirebase_database_urlproject_info/firebase_urlgoogle_api_keyYOUR_CLIENT/api_key/current_keygoogle_crash_reporting_api_keyYOUR_CLIENT/api_key/current_keyproject_idproject_info/project_id
Troubleshooting
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.
The Firebase console
will help you download the config file.
After you've downloaded the config file, copy it into
the app/ folder of your Android Studio project, or into the
app/src/{build_type} folder if you're using multiple build types.
I can't find the symbols like R.string.gcm_defaultSenderId or R.xml.global_tracker.
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.