When you publish your application in Android playstore and user starts using it, its possible that your app may crash on some mobiles / devices. When certain crash happens, its very important to get the crash report logs so the respective developer can fix the issue and we publish the updated app again to playstore.
To collect those crash logs, android firebase supports Crashlytics feature. In this post we will show how this crashlytics can be enabled in your android app source code.
Whats is Firebase Crashlytics ?
Firebase Crashlytics is a lightweight, realtime crash reporter that 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.
Setting up Firebase Crashlytics in Firebase Console.
- Login to your Firebase console
- Click Crashlytics in the left-hand nav panel of the Firebase console.
- If your Firebase project has multiple apps registered in it, select the app you just added from the dropdown next to Crashlytics in the top bar of the console.
- Click Enable Crashlytics.
Once you are done with this, and click on “Crashlytics” tab in left menu, you will see the message as,
“We’ll be listening for your app to communicate with our servers.” It means, its now time to integrate crashlytics in your android code make sure your app collects crash and report to the server.
For android app changes, follow below steps,
Modify build.gradle as,
dependencies {
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.4.1'
}
Modify app/build.gradle as,
apply plugin: 'com.google.firebase.crashlytics'
dependencies {
implementation platform('com.google.firebase:firebase-bom:26.4.0')
implementation 'com.google.firebase:firebase-crashlytics-ktx'
implementation 'com.google.firebase:firebase-analytics-ktx'
}
Above modifications in app/build.gradle are when your app code is written in Kotlin, if you are using Java for apps, just remove “-ktx” from above changes.
If you compile the above code and open your app, you can check for following message to make sure your Crashlytics setup is done properly.
$ adb shell logcat | grep FirebaseApp
I FirebaseApp: Device unlocked: initializing all Firebase APIs for app [DEFAULT]
I FirebaseInitProvider: FirebaseApp initialization successful
Testing if Crashlytics setup is working properly…
The above changes are sufficient to make sure your app crash’s are reported to firebase console. But if you are not able to reproduce the similar crash as some people might have observed, then you can generate a crash manually to test and make sure the Crashlytics setup is proper.
For this simulation, add following code into onCreate of some activity in your code,
import android.view.ViewGroup
import android.widget.Button
override fun onCreate(savedInstanceState: Bundle?) {
val crashButton = Button(this)
crashButton.text = "Crash!"
crashButton.setOnClickListener {
throw RuntimeException("Test Crash") // Force a crash
}
addContentView(crashButton, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT))
}
Now, when you open your app and navigate to activity in which you added this code, you would see “CRASH!” button, once you click this, your app would get crash.. (To test the implementation, press the button in your app to force the crash, then reopen your app so that Crashlytics can send the crash report to Firebase. It may take up to five minutes for the report to appear in the Firebase console.)
You can check this crash related logs in adb shell as,
$ adb logcat -s FirebaseCrashlytics
--------- beginning of crash
FirebaseCrashlytics: Crashlytics is handling uncaught exception "java.lang.RuntimeException: Test Crash" from thread main
Reference – https://firebase.google.com/docs/crashlytics?authuser=0