Uncaught exceptions in production Android apps destroy user trust. Firebase Crashlytics provides real-time crash reporting, stack trace de-obfuscation for ProGuard/R8 builds, and custom diagnostic logging to identify the exact state of an app before a crash occurs.

Step 1: Add Crashlytics Gradle Plugins

build.gradle.kts (Project Level)kotlin
plugins {
    id("com.google.gms.google-services") version "4.4.1" apply false
    id("com.google.firebase.crashlytics") version "2.9.9" apply false
}
build.gradle.kts (Module: app)kotlin
plugins {
    id("com.android.application")
    id("com.google.gms.google-services")
    id("com.google.firebase.crashlytics")
}
 
dependencies {
    implementation(platform("com.google.firebase:firebase-bom:32.8.0"))
    implementation("com.google.firebase:firebase-crashlytics")
}

Step 2: Log Custom Keys & Record Non-Fatal Exceptions

CrashlyticsLogger.ktkotlin
import com.google.firebase.crashlytics.FirebaseCrashlytics
 
fun logUserDataAndException(userId: String, e: Exception) {
    val crashlytics = FirebaseCrashlytics.getInstance()
    
    // Set custom diagnostic keys attached to crash reports
    crashlytics.setCustomKey("current_user_id", userId)
    crashlytics.setCustomKey("network_connected", true)
    
    // Log breadcrumb text log message
    crashlytics.log("Attempting to parse API response payload")
    
    // Record non-fatal exception without crashing app
    crashlytics.recordException(e)
}