Firebase for Android: Crashlytics Android 02.12.2017

How to report a crash in Android via Firebase Crash Reporting

Firebase Crashlytics is a lightweight, realtime crash reporter that helps helps you track, prioritize, and fix stability issues that erode your app quality. Crashlytics saves you troubleshooting time by intelligently grouping crashes and highlighting the circumstances that lead up to them.

Crashlytics is the new, primary crash reporter for Firebase. If your app uses Firebase Crash Reporting, there's good news: Crashlytics offers enhanced crash reporting with nearly the same setup process you're used to

Key capabilities are:

  • Curated crash reports. Crashlytics synthesizes an avalanche of crashes into a manageable list of issues, provides contextual information, and highlights the severity and prevalence of crashes so you can pinpoint the root cause faster.
  • Enhanced environment info. Crashlytics allows you to filter reports by operating system, version, hardware configuration, and more. Find out if your app is crashing more frequently on a particular manufacturer's devices, with certain API versions, or even in a specific screen orientation.
  • Realtime alerts. Get realtime alerts for new issues, regressed issues, and growing issues that might require immediate attention.

To get started, you need a Firebase app with Firebase Crashlytics enabled.

If you're using the latest version of Android Studio (version 2.2 or later), it's recommend using the Firebase Assistant to connect your app to Firebase. The Firebase Assistant can connect your existing project or create a new one for you and automatically install any necessary gradle dependencies.

To open the Firebase Assistant in Android Studio:

  1. Click Tools > Firebase to open the Assistant window.
  2. Click to expand one of the listed features (for example, Analytics), then click the provided tutorial link (for example, Log an Analytics event).
  3. Click the Connect to Firebase button to connect to Firebase and add the necessary code to your app.

Following steps is nesessary to add the Crashlytics SDK to your project.

Add the Crashlytics repositories and dependency to your project-level build.gradle file:

buildscripts {
    repositories {
        // ...
        maven {
           url 'https://maven.fabric.io/public'
        }
    }
    dependencies {
        // ...
        classpath 'io.fabric.tools:gradle:1.25.1'
    }
}

allprojects {
    // ...
    repositories {
       // ...
       maven {
           url 'https://maven.google.com/'
       }
    }
}

Add the Crashlytics dependencies to your app-level build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

dependencies {
    // ...
    compile 'com.google.android.gms:play-services:11.4.2'
    compile 'com.google.firebase:firebase-core:11.4.2'
    compile 'com.crashlytics.sdk.android:crashlytics:2.9.1'
}

Once you've added the SDK to your app, Crashlytics automatically gets to work listening for and collecting crash reports.

Finally, enable Crashlytics in the Firebase console by clicking Set up Crashlytics.

To test Firebase Crashlytics add following snippet somewhere in your code

Crashlytics.getInstance().crash();

The report should appear in the Firebase console within five minutes.

To give yourself more context for the events leading up to a crash, you can add custom Crashlytics logs to your app. Crashlytics associates the logs with your crash data and makes them visible in the Firebase console.

Use Crashlytics.log to help pinpoint issues. Crashlytics.log can write logs to a crash report and with Log.println(), or just to the next crash report.

Crash report and Log.println:

Crashlytics.log(int priority, String tag, String msg);

Crash report only:

Crashlytics.log(msg);

You can even log handled exceptions which will help you in analyzing the application flow.

try {
    // some exception
} catch (UnsupportedEncodingException e) {
    Log.e(TAG, "Exception: ", e);
    Crashlytics.logException(e);
} 

Custom keys

Crashlytics allows you to associate arbitrary key/value pairs with your crash reports, which are viewable right from the Crashlytics dashboard. Also resetting the same key updates the value. So where can we use this? Suppose you have a detail page which displays movie information and it crashes your app for a movie but you don’t know which one. That’s exactly where we can use custom keys so whenever you open this page first set custom key just like below.

Crashlytics.setString("movieId", "olBlOD2fOxQPHAuSx");

Remember resetting same key updates the value so you’ll get the name of recently opened movie in crashlytics dashboard. Below are other custom key methods.

void setBool(String key, boolean value);
void setDouble(String key, double value);
void setFloat(String key, float value);
void setInt(String key, int value);
void setString(String key, int value);

User Information

If you set below information in crashlytics then you can later search for users with given email or identifier in crashlytics dashboard. So if a user gives bad review or writes an angry mail to report a crash or something, you can check his email id in crashlytics and see for any issues.

void Crashlytics.setUserIdentifier(String identifier);
void Crashlytics.setUserName(String name);
void Crashlytics.setUserEmail(String email);