As part of adding Firebase to your Android project, you need to add the
Gradle plugin (com.google.gms.google-services)
and a configuration file to your project. The plugin
and configuration file are used specifically with Firebase services and the
Firebase SDK for Android.
When you register an Android app with a Firebase project, Firebase provides a
config file that you add to your app's codebase. This
config file contains values (referred to as Firebase options) that Firebase
SDKs need to communicate with Firebase APIs and identify which resources to
interact with. At build time, the Google services Gradle plugin
() converts this file into Android string resources;
at runtime, Firebase SDKs read those resources to initialize and connect your
app to your Firebase project and its provisioned resources (such as databases
and storage buckets). Some of these values are required (API key, Project ID,
and Firebase App ID), while others are optional and depend on the Firebase
services and features that you use.
If you add Firebase to your Android project using the Firebase console, the Management REST API, or the Firebase CLI, then you must manually add the Gradle plugin and JSON config file to your Android project. However, if you use the Firebase Assistant for Android Studio or the Firebase agent skills, then these tasks are automatically done for you during setup.
Despite its name, the Google services Gradle plugin is specific to Firebase and is distinct from the Google Play services SDKs.
If you encounter build or configuration issues when setting up the plugin or JSON config file, check out the Android troubleshooting & FAQ page.
Add the Gradle plugin
The Google services Gradle plugin () has two main
functions:
Processes the
config file: Converts the Firebase configuration values in the JSON file into Android string resources that Firebase SDKs read at runtime to connect your app to your Firebase project and resources.google-services.json Checks dependency version compatibility: Verifies that the resolved versions of Firebase and Google Play services libraries in your app's build meet each library's version requirements, and throws a build error if incompatible versions are detected.
Here's how to add the Google services Gradle plugin to your Android project:
In your root-level (project-level) Gradle file (
<project>/build.gradle.ktsor<project>/build.gradle), add the Google services Gradle plugin as a dependency:Kotlin
plugins { id("com.android.application") version "7.3.0" apply false // ... // Add the dependency for the Google services Gradle plugin id("com.google.gms.google-services") version "4.6.0" apply false }
Groovy
plugins { id 'com.android.application' version '7.3.0' apply false // ... // Add the dependency for the Google services Gradle plugin id 'com.google.gms.google-services' version '4.6.0' apply false }
In your module (app-level) Gradle file (usually
<project>/<app-module>/build.gradle.ktsor<project>/<app-module>/build.gradle), apply the Google services Gradle plugin:Kotlin
plugins { id("com.android.application") // Add the Google services Gradle plugin id("com.google.gms.google-services") // ... }
Groovy
plugins { id 'com.android.application' // Add the Google services Gradle plugin id 'com.google.gms.google-services' // ... }
Customize the plugin
You can customize the behavior of the Google services Gradle plugin by adding
an optional googleServices block in your module (app-level) Gradle file
with either or both of the following optional values:
disableVersionCheck:- Disables checking whether Firebase and Google Play services library dependencies have compatible versions.
- Valid values are
trueorfalse; defaults tofalse.
missingGoogleServicesStrategy:Configures how the build responds if the
config file is missing (for example, if a specific build variant doesn't use Firebase).google-services.json Valid values are
MissingGoogleServicesStrategy.IGNORE,MissingGoogleServicesStrategy.WARN, orMissingGoogleServicesStrategy.ERROR; defaults toMissingGoogleServicesStrategy.ERROR.
Here's an example of configuring the googleServices block:
Kotlin
import com.google.gms.googleservices.GoogleServicesPlugin.MissingGoogleServicesStrategy
googleServices {
disableVersionCheck = false // Set to true only if you need to bypass version checks
missingGoogleServicesStrategy = MissingGoogleServicesStrategy.WARN // Log a warning instead of failing the build
}
Groovy
import com.google.gms.googleservices.GoogleServicesPlugin.MissingGoogleServicesStrategy
googleServices {
disableVersionCheck = false // Set to true only if you need to bypass version checks
missingGoogleServicesStrategy = MissingGoogleServicesStrategy.WARN // Log a warning instead of failing the build
}
Add the JSON config file
The config file is generally placed in the
module (app-level) root directory of your Android project (usually app/).
The plugin also supports placing variant-specific JSON files in build-type,
product-flavor, or combined variant directories under app/src/. All of the
following directory structures are valid:
app/
google-services.json // Module root (default fallback)
src/debug/google-services.json // Build type (for example, debug or release)
src/free/google-services.json // Product flavor (for example, free or paid)
src/free/release/google-services.json // Product flavor and build type
src/paidDebug/google-services.json // Combined variant directory
...
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 Firebase
project, while each entry in the client array contains configuration
information for a Firebase Android App registered in your Firebase project.
When processing the JSON file for your Android app, the plugin checks each entry
in the client array and uses the client object whose
client_info/android_client_info/package_name value matches the current build
variant's Android package name (configured as applicationId in your module
(app-level) Gradle file). If no entry in client matches the applicationId,
the plugin throws a
No matching client found for package name
exception during the build.
Generated XML resource file
Processing the config file produces an XML resource
file (values.xml), which contains the Android string resources that
Firebase SDKs read at runtime to initialize Firebase in your app.
This file is located at:
app/build/generated/res/process{build_variant}GoogleServices/values/values.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Required Firebase options (always present) -->
<string name="google_app_id" translatable="false">1:1035469437089:android:73a4fb8297b2cd4f</string>
<string name="project_id" translatable="false">mydemoapp</string>
<string name="google_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
<!-- Always present in Firebase Android configurations -->
<string name="gcm_defaultSenderId" translatable="false">1035469437089</string>
<string name="google_storage_bucket" translatable="false">mydemoapp.firebasestorage.app</string>
<!-- Added when the corresponding Firebase services are configured -->
<string name="firebase_database_url" translatable="false">https://mydemoapp-default-rtdb.firebaseio.com</string>
<string name="default_web_client_id" translatable="false">337894902146-e4uksm38sne0bqrj6uvkbo4oiu4hvigl.apps.googleusercontent.com</string>
<!-- Legacy resource emitted for backwards compatibility -->
<string name="google_crash_reporting_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
</resources>
Values in the XML resource file
Every value in values.xml provides a Firebase option sourced from the
config file at the JSON paths listed below (where
YOUR_CLIENT refers to the entry in the client
array that matches your app's applicationId).
Different Firebase services require different options, but all Firebase
services (including Firebase installations) require three minimum options:
google_app_id (Firebase App ID), project_id (Firebase Project ID), and
google_api_key (Firebase API key). Other values are optional and depend on the
Firebase services and features that you use.
If your Android project has a custom build setup that prevents you from using
the Gradle plugin, you can safely define these
string resources manually in your app's res/values/ directory using these
values.
google_app_id- Firebase Android App ID; this is the unique identifier for the Firebase Android App, not the Android package name or Android application ID
- Always present; required for all Firebase services
YOUR_CLIENT/client_info/mobilesdk_app_idproject_id- Firebase project ID
- Always present; required for all Firebase services
project_info/project_idgoogle_api_key- Firebase API key for the Firebase Android App
- Always present; required for all Firebase services
YOUR_CLIENT/api_key/current_keygcm_defaultSenderId- Default sender ID for Firebase Cloud Messaging (retains the legacy
gcm_prefix) - Always present
project_info/project_number- Default sender ID for Firebase Cloud Messaging (retains the legacy
google_storage_bucket- Default Cloud Storage for Firebase bucket for the Firebase project
- Always present (note that you have to provision this bucket in the Firebase project for this value to be usable by the app)
project_info/storage_bucketfirebase_database_url- Default Firebase Realtime Database URL for the Firebase project
- Added when the first Firebase Realtime Database instance is provisioned in the Firebase project
project_info/firebase_urldefault_web_client_id- OAuth 2.0 web client ID, used by Firebase Authentication (such as Google Sign-In)
- Added when an OAuth 2.0 web client is configured in the Firebase project
YOUR_CLIENT/oauth_client/client_id (client_type == 3)