As part of adding Firebase to your Android project, you need to add the
`google-services` Gradle plugin and a
`google-services.json` configuration file to your project.

If you add Firebase to your Android project using
the [Firebase console](https://firebase.google.com/docs/android/setup.md.txt#console),
the [Management REST API](https://firebase.google.com/docs/projects/api/workflow_set-up-and-manage-project.md.txt?platform=android#add-apps),
or the [Firebase CLI](https://firebase.google.com/docs/cli.md.txt#management-commands),
you must manually add the Gradle plugin and JSON config file to your project.
However, if you use the
[Firebase Assistant for Android Studio](https://firebase.google.com/docs/android/learn-more.md.txt#firebase-assistant),
these tasks are automatically done for you during setup.

> [!NOTE]
> **Note:** The `google-services.json` config file contains unique, but non-secret identifiers for your project. To learn more about this config file, visit [Understand Firebase Projects](https://firebase.google.com/docs/projects/learn-more.md.txt).

## Add the Gradle plugin

You add the
Google services Gradle plugin (`google-services`) 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 `google-services.json` config file and produces Android
  resources that can be used in your app's code.

- 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.gradle` file, 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 `google-services.json` 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
        ...

> [!NOTE]
> **Note:** Providing a `google-services.json` config file in the release directory lets you maintain a separate Firebase project for your production APKs.

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 `google-services.json` 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 `client` array, the plugin checks the value of
  `client_info/android_client_info/package_name`. If the package name matches
  this value, it returns the member object.

- If none of the members of `client` match 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
  <?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
  <?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
`google-services.json` config file at the locations listed below. If your
Android project has some configuration that prevents you from using the
`google-services` Gradle plugin, you can safely recreate the
XML files manually using these values.

- `google_app_id`

      YOUR_CLIENT/client_info/mobilesdk_app_id

- `gcm_defaultSenderId`

      project_info/project_number

- `default_web_client_id`

      YOUR_CLIENT/oauth_client/client_id (client_type == 3)

- `ga_trackingId`

      YOUR_CLIENT/services/analytics-service/analytics_property/tracking_id

- `firebase_database_url`

      project_info/firebase_url

- `google_api_key`

      YOUR_CLIENT/api_key/current_key

- `google_crash_reporting_api_key`

      YOUR_CLIENT/api_key/current_key

- `project_id`

      project_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](https://console.firebase.google.com/project/_/?useAutoProject=true)
will help you download the `google-services.json` config file.

After you've downloaded the `google-services.json` 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.