Android From Scratch: Firebase Crash Reporting

There are well over a billion people using Android devices today, and thousands are joining them every day. Making sure that all those people can enjoy your app is no easy task. 

The characteristics of Android devices vary wildly. There are differences in API levels, screen sizes, pixel densities, and hardware sensor availability. To make matters worse, hardware vendors customize Android extensively, often introducing little quirks that are unique to their devices. 

With so many variables at play, your app is likely to crash on several devices and in several situations. By collecting and analyzing information about those crashes, you can add fixes to your app that make it more robust and your users happier.

In this quick tip, I'll show you how to use Firebase as a crash reporting solution. It's free, offers lots of useful features, and is easy to integrate with any Android app.

1. Creating a Firebase Configuration File

Every Android Studio project that uses Firebase must be registered with Google's Firebase console. Once you log in to the console, click on the Create New Project button to start the registration process.

Firebase console welcome screen

In the dialog that pops up, give the project an appropriate name and specify the country you are in.

Create a project dialog

Next, click on the Add Firebase to your Android app button.

Platform selection screen

You will now be prompted to enter the package name of the Android app. Make sure that the value you provide matches the package name your Android Studio project uses.

App configuration dialog

Finally, press the Add app button. Your browser should now automatically start downloading a file called google-services.json, which contains configuration details—such as the Firebase project ID, API key, and app ID—tailored for your app.

Once the download is complete, move the file to the app directory of your Android Studio project.

2. Configuring Your App

To be able to use Firebase in your Android Studio project, you must add the Google Services Gradle plugin as a dependency in the top-level build.gradle file, which is present in the root directory of the project.

Additionally, you must apply the Google Services Gradle plugin. Therefore, add the following line at the end of the app module's build.gradle file:

You can now add the Firebase Crash Reporting SDK as a compile dependency.

That's all you need to do. From this point on, Firebase will automatically generate a crash report every time your app crashes.

3. Generating Crash Reports Manually

Apps crash when they encounter uncaught exceptions or errors. But what about those exceptions that are caught using try...catch blocks? Well, Firebase won't report them automatically because it assumes that you already know about them. Nevertheless, sometimes, you might still want to see them in your reports.

To generate a crash report manually, you must use the FirebaseCrash.report() method. As its only argument, it expects a Throwable object. Therefore, you would usually call it inside a catch block.  The following code snippet shows you how to use it while handling an IOException:

Here's what the crash report looks like in the Firebase console:

Crash reports screen of Firebase console

As you can see, similar crash reports are grouped together to form a cluster. By clicking on a cluster and pressing the View Details button, you can take a look at the individual crash reports that are inside it.

Crash details including stack trace

4. Adding Log Messages to Crash Reports

Although Firebase crash reports include stack traces and a lot of diagnostic data, sometimes you might be interested in knowing more specific details about the conditions that caused your app to crash. For example, you might want to know the exact values of some important local variables. 

An easy way to include such details in your reports is to generate custom log messages using the FirebaseCrash.log() method, which expects a string as its only argument.

Here's a code snippet that generates a custom log message:

Note that a custom log message is included only in the subsequent crash report.

In the Firebase console, you can see the log messages towards the end of the crash report.

Log messages in Firebase console

Conclusion

Crashes should never be ignored because they tend to make users lose interest in your app. Moreover, they can lead to data being lost. By using services such as Firebase, you can regularly monitor your app for crashes and quickly upload appropriate fixes. It's also a good idea to include details about the fixes in your app's change log so that your users know that you care about them!

To learn more about Firebase crash reporting, you can refer to its official documentation. For more about Firebase on Android and other related technologies, check out some of our other tutorials!

Tags:

Comments

Related Articles