<br />

[Firebase
Crashlytics](https://firebase.google.com/docs/crashlytics/android/get-started)
is a lightweight, realtime crash reporter that makes it easy for you to manage
stability issues in your app. Crashlytics saves you troubleshooting time by
intelligently grouping crashes and highlighting the circumstances that lead up
to them.

This guide describes how to integrate Crashlytics into your Android Studio
project so that you can log ad response IDs. Later, when you troubleshoot
crashes in your app, you can look up the ad response ID and use the Ad Review
Center in

[Ad Manager](https://support.google.com/admanager/answer/173120)

to find and block the ads.

## **Step 1:** Add Firebase to an Android application

1. If you would like to try logging with Firebase from a clean app, you can
   download or clone Google Mobile Ads SDK (Legacy) examples for Android
   [repository](https://github.com/googleads/googleads-mobile-android-examples) on
   GitHub. This guide specifically uses the

   [Banner Example](https://github.com/googleads/googleads-mobile-android-examples/tree/main/java/admanager/BannerExample).


   If you already have an app, you should be able to proceed to other steps
   with your app's package name. The same steps can also be applied to other
   examples in the repository with minor adaptations.
2. In order to use Firebase Crashlytics, you must create a Firebase project and
   add your app to it. If you haven't already, create a Firebase project. Make
   sure to [register your
   app](https://firebase.google.com/docs/android/setup#register-app) to it.

   1. In the Crashlytics page of the Firebase console, click **Set up
      Crashlytics**.

   2. In the screen that appears, click **No** \> **Set up a new Firebase
      app**.

      ![](https://developers.google.com/static/admob/images/crashlytics-setup.png)
3. In your build.gradle, add the dependencies for Google Analytics, Fabric, and
   Crashlytics.

   #### app/build.gradle

   ```
   apply plugin: 'com.android.application'
   apply plugin: 'com.google.gms.google-services'

   // Add the Fabric plugin
   apply plugin: 'io.fabric'

   dependencies {
       // ...

       // Add Google Mobile Ads SDK (Legacy)
       implementation 'com.google.android.gms:play-services-ads:25.5.0'

       // Add the Firebase Crashlytics dependency.
       implementation 'com.google.firebase:firebase-crashlytics:21.0.1'
   }
   ```

   #### project/build.gradle

   ```
   buildscript {
       repositories {
           // ...
           // Add Google's Maven repository.
           google()
       }

       dependencies {
           // ...

           classpath 'com.google.gms:google-services:4.5.0'

           // Add the Fabric Crashlytics plugin.
           classpath 'com.google.firebase:firebase-crashlytics-gradle:3.0.8'
       }
   }

   allprojects {
       // ...
       repositories {
          // Check that Google's Maven repository is included (if not, add it).
          google()

          // ...
       }
   }
   ```
4. Build and run your app to make sure to make sure Crashlytics is configured
   correctly. Once successful, you will be able to access the Crashlytics
   dashboard.

   ![](https://developers.google.com/static/admob/images/crashlytics-dashboard.png)

### **(Optional): Test your Setup**

By [Adding a crash
button](https://firebase.google.com/docs/crashlytics/android/test-implementation)
you can force a crash for causing an app crash with each button press.

Here's an example that shows how to add a crash button in the `onCreate()` method of an
`Activity`:

#### MainActivity (excerpt)

### Java

```java
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_my);

  // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
  // values/strings.xml.
  adView = findViewById(R.id.ad_view);

  // Start loading the ad in the background.
  adView.loadAd(new AdManagerAdRequest.Builder().build());

  // Add a crash button.
  Button crashButton = new Button(this);
  crashButton.setText("Crash!");
  crashButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      throw new RuntimeException("Test Crash"); // Force a crash
    }
  });

  addContentView(crashButton, new ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.WRAP_CONTENT));
}
```

### Kotlin

```kotlin
override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_my)

  // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
  // values/strings.xml.
  adView = findViewById(R.id.ad_view)

  // Start loading the ad in the background.
  adView.loadAd(AdManagerAdRequest.Builder().build())

  // Add a crash button.
  val crashButton = Button(this)
  crashButton.text = "Crash!"
  crashButton.setOnClickListener {
    throw RuntimeException("Test Crash") // Force a crash
  }

  addContentView(crashButton, ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.WRAP_CONTENT))
}
```

In Android Studio, build and run your app on an emulator or a connected device.
After the app is loaded, you can click the **Crash** button. Relaunch the app
from the device or Android Studio for the crash log to be uploaded to
Crashlyics.

> [!IMPORTANT]
> **Key Point:** While the crash and session data might be visible instantly in the Firebase console, custom keys may take an hour or more to propagate.

## **Step 2:** Log the ad response ID

If you load multiple ads and show them at different times, it is a good idea to
log each ad response Id with a separate key. For instance, this guide uses an
example that has only one banner ad. Hence, we log the ad response ID as the
`banner_ad_response_id` key in the following snippet. Indeed you can create
multiple custom key / value pairs in Firebase Crashlytics for different ad types
and ad events (see
[`AdListener`](https://developers.google.com/ad-manager/mobile-ads-sdk/android/reference/com/google/android/gms/ads/AdListener.md.txt) for
ad's life cycle). Visit [Customize your Firebase Crashlytics crash
reports](https://firebase.google.com/docs/crashlytics/android/customize-crash-reports)
for more information on custom logging.

Add the following code to your `MyActivity.java`. Essentially, it uses the
[`FirebaseCrashlytics.setCustomKey()`](https://firebase.google.com/docs/reference/android/com/google/firebase/crashlytics/FirebaseCrashlytics#public-void-setcustomkey-string-key,-long-value)
function in the `onAdLoaded()` callback function to ensure that the ad has
loaded before attempting to call `getResponseInfo()`.

### Java

```java
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_my);

  // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
  // values/strings.xml.
  adView = findViewById(R.id.ad_view);

  adView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
      String adResponseId = adView.getResponseInfo().getResponseId();
      FirebaseCrashlytics.getInstance().setCustomKey(
          "banner_ad_response_id", adResponseId);
    }
  });

  // Start loading the ad in the background.
  adView.loadAd(new AdManagerAdRequest.Builder().build());

  // Add a crash button.
  Button crashButton = new Button(this);
  crashButton.setText("Crash!");
  crashButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      throw new RuntimeException("Test Crash"); // Force a crash
    }
  });

  addContentView(crashButton, new ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.WRAP_CONTENT));
}
```

### Kotlin

```kotlin
override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_my)

  // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
  // values/strings.xml.
  adView = findViewById(R.id.ad_view)

  adView.adListener = object : AdListener() {
    override fun onAdLoaded() {
      mAdView.responseInfo?.responseId?.let { adResponseId ->
          FirebaseCrashlytics.getInstance().setCustomKey(
              "banner_ad_response_id", adResponseId)
      }
    }
  }

  // Start loading the ad in the background.
  adView.loadAd(AdManagerAdRequest.Builder().build())

  // Add a crash button.
  val crashButton = Button(this)
  crashButton.text = "Crash!"
  crashButton.setOnClickListener {
    throw RuntimeException("Test Crash") // Force a crash
  }

  addContentView(crashButton, ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.WRAP_CONTENT))
}
```

Congratulations! You will now see the most recent `banner_ad_response_id`
in the key section of crash sessions on your Crashlytics dashboard. Note that
some keys may take up to an hour to become visible in your dashboard.

![](https://developers.google.com/static/admob/images/crashlytics-keys.png)