Firebase Crashlytics — The Saviour

Firebase Crashlytics — The Saviour

Storytime:

Recently, I was working on a legacy Android application, which had to deal with a lot of heavy libraries and dependencies to perform live-streamed graph calculations to collect the spectrum from any light source.

After adding a new feature, I started the testing on every possible Android device, and as expected thing worked fine until the day the app was released. I started receiving a lot of emails regarding the app getting crashed very frequently while calculating the spectrum data.

At first, it was very strange to me, and the very first thing that came in my mind - "If it is working at my end, why it is getting crashed at the user's end?". Even though the device I use is having a pretty outdated version of Android.

I tried my best to fix it but I was clueless, where and why it is getting failed. Time was running out fast and I had to find a way to debug the issue and resolve it as soon as possible. Solution: I finally decided to integrate the Firebase Crashlytics with my Android app to get real-time crash reports from the user.

How to integrate Firebase Crashlytics in your existing Android app?

Follow the steps if you want to integrate this amazing library: First, register your app on Firebase. You can follow the official doc for the same. Then, in your project level build.gradle file, update the google-service to v3.1.2 or higher and add the Fabric build tool which will take care of all the settings and other configurations during the build process. To do this, add the Maven repo and the Fabric Gradle tool dependency.

In your app-level build.gradle, update firebase-coreto version v11.4.2 or later and add the Crashlytics dependencies.

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

dependencies {
    // Support AndroidX migration
    implementation 'androidx.appcompat:appcompat:1.0.0'

    // Check for v11.4.2 or higher
    implementation 'com.google.firebase:firebase-core:17.1.0'

    // Add dependency
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
}

And that's it! Wasn't that simple? But how can I test if this thing really works? Okay, for this we can mock a crash scenario to test how Crashlytics behaves whenever a crash event occurs. Add the following block of code in your MainActivity.java file.

Button crashButton = new Button(this);
crashButton.setText("Crash!");
crashButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Crashlytics.getInstance().crash(); // Force a crash
    }
});

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

Now when you click on this newly added crash button, Firebase will track it and BANG, you will get a detailed stack trace of the latest crash event in the Firebase Crashlytics Console, similar to the one attached below!!

1_6_Dwon-wS4Kpg6vXEAXR4A.png

1_O2EkxndMTkQFiTiB_8JGFA.png

Summary:

Within no time I figured out the bug which was leading to the crash and resolved it immediately. The further enhancement I did by creating a small Slack app which notifies me every time an app crash event is received, which I will explain in my future post. That's all for now! Happy coding.