StackTips

How to Use Firebase Crash Reporting in Android

stacktips avtar

Written by

Editorial,  8 min read,  3.05K views, updated on Sept. 17, 2023

Firebase Crash Reporting is yet another Firebase feature announced at Google I/O 2016. It enables you to collect the detailed report of errors in your app.

Ones your app is published, it is difficult the understand the reason why the app is crashed. And hence it becomes very difficult to fix them. Crash reporting frameworks log the crashes and grouped into clusters of similar stack traces, and triaged by the severity of impact on your users.

Firebase crash reporting automatically logs the crashes and you may also log custom events to help capture the steps leading up to a crash.

In this tutorial, we will see how to configure and use Firebase Crash Reporting platform in your Android application.

Configure the Firebase SDK.

Let us begin with creating a Firebase project in the Firebase console. You will be asked to log in with your Google Mail account. Once you logged in, select Add App button to create a new project in Firebase Console.

  1. Select the Android platform to continue with Android Firebase configuration.

Create new project in Firebase console- Select Platform

  1. If you have not created any app before, you will be asked to select the Project Name and Country Region. Provide the details and Continue with the configuration.
  2. Enter your Android app package name. This should be same as defined in your android AndroidMaifest.xml file.

Firebase Android Configuration

  1. You may provide the debug keystore SHA-1 certificate. However this is optional and hence you may leave it empty.
  2. Select Add App button. This will download the google-services.json configuration file for your app. Copy this file into your project’s module folder, typically inside app/ directory.
Project Structure
Screenshot depicts project structure after adding google-services.json file
  1. Now, add the required Firebase SDK dependencies to your project. Modify the project level build.gradle (<project>/build.gradle) file and add the google-services.
buildscript {
  dependencies {
    // Add this line
    classpath 'com.google.gms:google-services:3.0.0'
  }
}

And, add the following to app module level build.gradle file.

apply plugin: 'com.android.application'
android {
   ....
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    ...
    //Add this
    compile 'com.google.firebase:firebase-crash:9.4.0'
}

// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'

After the dependencies are added Sync your project with gradle file changes. With this we’re done with the project configuration.

Reporting Custom Crash

Firebase Crash Reporting automatically generates reports for fatal errors. However, if you want to report your own exception, you can do it using FirebaseCrash.report(exception) method. For example:

Exception exception = new Exception("Oops! Firebase non-fatal error!");
FirebaseCrash.report(exception);

Optionally, you can report custom logs by calling log() method.

FirebaseCrash.log("Button clicked!");

Please note, it usually takes up to 20 minutes to get your error logs on Firebase console.

Firebase Crash Reporting Android