Open In App

Firebase Crashlytics in Android with Example

Improve
Improve
Like Article
Like
Save
Share
Report

Firebase A lightweight, real-time crash reporter called Crashlytics makes it easier to identify, manage, and resolve stability problems that lower the caliber of your app. By logically categorizing crashes and highlighting the factors that precede them, Crashlytics helps you reduce the amount of time you spend debugging. See if a certain crash is having a significant effect on the number of users. Receive notifications when a problem suddenly becomes more serious. Identify the code snippets that are resulting in crashes.

The core added benefits over to the other available crash detectors are:

  1. In order to help you identify the root cause more quickly, Crashlytics synthesizes an avalanche of crashes into a manageable list of issues, gives contextual information, and highlights the severity and occurrence of crashes.
  2. Crashlytics provides Crash Insights, practical advice that highlights typical stability issues, and lists resources that make it simpler to identify, analyze, and fix them.
  3. Receive real-time alerts for issues that have emerged, regressed, or are increasing and may need rapid care.
  4. We can easily and amiably keep track of all these issues thanks to Firebase Crashlytics. It is invaluable information to learn whether a specific crash is having a significant influence on the number of users, to receive alerts when a problem suddenly becomes more serious, or to identify the lines of code that are responsible for crashes. When a crash occurs, Firebase Crashlytics gathers a lot of data from the user, including the device specs, the battery life, the screen rotation, the network connection, and many other important metrics.

Example

Step #1: Setting up the project

We only need to navigate to the Crashlytics tab in our Firebase dashboard and click on Set Up Crashlytics to begin establishing Firebase Crashlytics in our application. As soon as Crashlytics has been activated in the Firebase console, our project has to have the SDK installed. First, we must add the following lines to our project’s build.gradle file:

 Adding the plugin to the project

Image #1: Adding the plugin to the project

Step #2: Adding the dependency for the file

Next up is to add the lines which will import the project into the android app, to do this, simply add this line in the dependency section and you’re good to go:

Adding the dependency

Image #2: Adding the dependency

With this straightforward configuration, Crashlytics will automatically begin listening for and gathering crash reports.

Step #3: Handling the uncaught exceptions to the crashlytics too!

Including an UncaughtExceptionHandler to provide more details
Even while the data offered by Crashlytics is comprehensive on its own, larger applications may require further information, such as the phone’s total or available heap memory or the activity stack. We can add an UncaughtExceptionHadnler to our program to handle these situations. When a Thread unexpectedly ends as a result of an uncaught exception, this Handler is called. With the help of this, we can add specific arguments to the uncaught exceptions so that our Firebase Console can display them.

Crashylytics starts to work

Image #3: Crashylytics starts to work

Kotlin




class CrashlyticsHandler(val defaultUncaughtExceptionHandler: Thread.UncaughtExceptionHandler) : Thread.UncaughtExceptionHandler {
  
        val runtime by lazy { Runtime.getRuntime() }    
  
        override fun uncaughtException(thread: Thread?, ex: Throwable?) {
            // Our custom logic goes here. For example calculate the memory heap
            val maxMemory = runtime.maxMemory()
            val freeMemory = runtime.freeMemory()
            val chunkedUpMemory = runtime.totalMemory() - freeMemory
            val freedUpMemory = maxMemory - chunkedUpMemory    
  
            // Set values to Crashlytics
            Crashlytics.setLong("chunkedUp_memory", chunkedUpMemory)
            Crashlytics.setLong("freedUp_memory", freedUpMemory)    
  
            // This will make Crashlytics do its job
            defaultUncaughtExceptionHandler.uncaughtException(thread, ex)
        }
    }


The detected crashes

Image #4: The detected crashes

That’s it, firebase crashlytics are included and integrated successfully into your app.



Last Updated : 27 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads