How to Use Firebase Crashlytics for Crash Reporting
To use
Firebase Crashlytics, add it to your app via the Firebase console, integrate the Crashlytics SDK in your app code, and initialize it. Crashlytics automatically collects crash reports and lets you view detailed crash data in the Firebase console.Syntax
Firebase Crashlytics setup involves adding the Crashlytics SDK to your app and initializing it in your app's main code. The key parts are:
implementation 'com.google.firebase:firebase-crashlytics': Adds Crashlytics library.FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true): Enables crash data collection.FirebaseCrashlytics.getInstance().log(): Logs custom messages to help debug.FirebaseCrashlytics.getInstance().recordException(): Manually records caught exceptions.
java
dependencies {
implementation 'com.google.firebase:firebase-crashlytics:18.4.1'
}
// In your app initialization code
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
crashlytics.setCrashlyticsCollectionEnabled(true);
// To log custom info
crashlytics.log("User clicked button");
// To record caught exceptions
try {
// code that may throw
} catch (Exception e) {
crashlytics.recordException(e);
}Example
This example shows how to initialize Firebase Crashlytics in an Android app and log a custom message before forcing a crash to test reporting.
java
import com.google.firebase.crashlytics.FirebaseCrashlytics; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); crashlytics.setCrashlyticsCollectionEnabled(true); crashlytics.log("App started"); // Force a crash for testing findViewById(R.id.crashButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { throw new RuntimeException("Test Crash"); } }); } }
Output
App crashes with RuntimeException: Test Crash; crash report sent to Firebase console
Common Pitfalls
Common mistakes when using Firebase Crashlytics include:
- Not enabling crashlytics collection with
setCrashlyticsCollectionEnabled(true), so no data is sent. - Forgetting to add the Crashlytics SDK dependency in your build file.
- Not initializing Firebase properly before using Crashlytics.
- Expecting immediate crash reports; it can take a few minutes to appear in the Firebase console.
java
/* Wrong: Crashlytics collection disabled, no reports sent */ FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(false); /* Right: Enable collection to send crash data */ FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true);
Quick Reference
| Action | Code Snippet | Description |
|---|---|---|
| Add SDK | implementation 'com.google.firebase:firebase-crashlytics:18.4.1' | Include Crashlytics library in your app |
| Enable Collection | FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true); | Turn on crash data sending |
| Log Message | FirebaseCrashlytics.getInstance().log("message"); | Add custom logs to crash reports |
| Record Exception | FirebaseCrashlytics.getInstance().recordException(e); | Report caught exceptions manually |
| Force Crash | throw new RuntimeException("Test Crash"); | Test crash reporting by crashing app |
Key Takeaways
Add the Firebase Crashlytics SDK to your app dependencies to start using it.
Enable crash data collection explicitly with setCrashlyticsCollectionEnabled(true).
Use log() and recordException() to add helpful info to crash reports.
Crash reports appear in the Firebase console a few minutes after a crash.
Test your setup by forcing a crash and verifying the report in Firebase.