Mobile applications often face issues that cause crashes, which can negatively impact user experience. As an Android developer, it’s important to detect and fix these crashes quickly. Firebase Crashlytics is a free crash-reporting tool that helps you monitor, track, and fix crashes in real time. It provides detailed crash reports, making it easier to identify issues and improve your app’s stability.
In this guide, we’ll walk you through setting up Firebase Crashlytics in your Android application to automatically collect crash logs and gain insights into your app’s stability.
Why Use Firebase Crashlytics for Android?
Firebase Crashlytics helps you:
- Track crashes in real time: Crashlytics sends crash reports in real time, helping you fix issues before they impact a large number of users.
- Gain actionable insights: It provides detailed stack traces, highlighting the specific lines of code where crashes occur.
- Improve user experience: By quickly identifying and fixing crashes, you can enhance the stability of your app and keep users happy.
Prerequisites
Before setting up Firebase Crashlytics, ensure that:
- You have a Firebase account. If you don’t have one, sign up at firebase.google.com.
- Your Android project is using Android Studio.
- You have a Google Services JSON file, which is required to link your app to Firebase.
Step-by-Step Guide to Set Up Firebase Crashlytics
Step 1: Add Firebase to Your Android Project
The first step is to link your Android app with Firebase.
- Go to the Firebase Console, click on Add Project, and follow the instructions to create a new project.
- Once the project is created, click on Add App and choose the Android platform.
- Register your app by entering your app’s package name, then click Register App.
- Download the
google-services.json
file and place it in theapp/
directory of your Android project. - Add the following classpath to your project-level
build.gradle
file:
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.10'
}
}
- In your
app/build.gradle
file, apply thegoogle-services
plugin:
apply plugin: 'com.google.gms.google-services'
Step 2: Add Crashlytics SDK to Your Project
Now that Firebase is linked, you can add the Crashlytics SDK to your project.
- In the
app/build.gradle
file, add the following dependencies:
dependencies {
implementation 'com.google.firebase:firebase-crashlytics:18.2.4'
implementation 'com.google.firebase:firebase-analytics:21.2.0' // Optional but recommended for analytics
}
- Sync your project with Gradle files to download the necessary libraries.
Step 3: Initialize Firebase Crashlytics
To ensure that Firebase Crashlytics is initialized properly, you need to modify your MainActivity
or the application class where you want Crashlytics to start tracking crashes.
If you are using an Application class, you can initialize Crashlytics here:
import android.app.Application;
import com.google.firebase.crashlytics.FirebaseCrashlytics;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Enable automatic data collection
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true);
}
}
If you don’t have an Application
class, add this initialization in your MainActivity
under the onCreate()
method:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.firebase.crashlytics.FirebaseCrashlytics;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Firebase Crashlytics
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true);
}
}
This will enable automatic crash reporting when your app crashes.
Step 4: Generate a Test Crash
To verify that Firebase Crashlytics is working correctly, you can force a test crash in your app. Add the following code snippet to trigger a crash:
// Add this to a button click or in the onCreate method
FirebaseCrashlytics.getInstance().log("Testing crash logging");
throw new RuntimeException("Test crash for Firebase Crashlytics");
When you run the app, this will intentionally crash it, and the crash report will be sent to Firebase.
Step 5: View Crash Reports in Firebase Console
Once your app crashes, the crash report will appear in the Firebase Console. You can log in to Firebase, navigate to your project, and click on Crashlytics from the left-hand menu to view the detailed crash logs.
Crashlytics provides insights such as:
- The exact line of code where the crash occurred.
- The type of exception that caused the crash.
- The percentage of users affected by the crash.
Additional Configuration Options
- Custom Logging: You can add custom logs to your crash reports to make debugging easier:
FirebaseCrashlytics.getInstance().log("App started successfully");
- Set User Identifiers: You can set user IDs to track crashes specific to individual users:
FirebaseCrashlytics.getInstance().setUserId("user123");
- Handle Non-Fatal Errors: You can log non-fatal errors that don’t crash the app but need attention:
FirebaseCrashlytics.getInstance().recordException(new Exception("Non-fatal error occurred"));
Setting up Firebase Crashlytics for your Android app is essential for monitoring crashes and improving your app’s stability. With Crashlytics, you can automatically collect crash logs, get real-time crash insights, and resolve issues faster. Whether you’re developing a new app or maintaining an existing one, Firebase Crashlytics can help you improve the user experience by quickly identifying and addressing crashes.
By following this step-by-step guide, you should now have Firebase Crashlytics set up in your Android project, allowing you to track crashes effectively and make your app more stable.