Skill 06 · Firebase Crashlytics
Subchapter 6.1
references/android_setup.mdMarkdown5 KBView on GitHub
Important references:
firebase-basics skills, particularly those for project and app
setup, before proceeding.Before you begin, ensure you have the following. If a google-services.json
file is present, then use that Firebase project and app. Otherwise you may need
to create them.
firebase-basics).npx -y firebase-tools@latest projects:create (see firebase-basics).npx -y firebase-tools@latest apps:create ANDROID <display-name> --package-name=<package-name>The google-services.json file must be present in the Android app’s module
directory. If missing, get the config using the Firebase CLI:
npx -y firebase-tools@latest apps:sdkconfig ANDROID <App-ID>.
These changes are made to your Android project’s Gradle files.
Add the latest version of the Crashlytics Gradle plugin to the plugins block.
Fetch the
latest version from the Google Maven repository (opens in a new tab)
before adding this.
plugins {
// ... other plugins
id("com.google.firebase.crashlytics") version "<latest_plugin_version>" apply false
}Add the Crashlytics plugin to the plugins block:
plugins {
// ... other plugins
id("com.google.firebase.crashlytics")
}Add the Firebase Crashlytics dependency to the dependencies block. It is
recommended to use the Firebase Bill of Materials (BoM) to manage SDK
versions. Fetch the
latest version from the Google Maven repository (opens in a new tab)
before adding this.
dependencies {
// ... other dependencies
// Import the Firebase BoM
implementation(platform("com.google.firebase:firebase-bom:<latest_bom_version>"))
// Add the dependencies for the Crashlytics and Analytics
implementation("com.google.firebase:firebase-crashlytics")
implementation("com.google.firebase:firebase-analytics")
}If your app uses native code (C/C++), or includes a library with native code, you can configure Crashlytics to report native crashes.
App-level build.gradle.kts (<project>/<app-module>/build.gradle.kts)
Add the firebase-crashlytics-ndk dependency:
dependencies {
// ... other dependencies
implementation("com.google.firebase:firebase-crashlytics-ndk")
}Enable the nativeSymbolUpload flag in your buildTypes configuration. This
will automatically upload symbol files for your native code, which are
required to symbolicate native crash reports.
import com.google.firebase.crashlytics.buildtools.gradle.CrashlyticsExtension
android {
// ... other config
buildTypes {
getByName("release") {
// ...
configure<CrashlyticsExtension> {
nativeSymbolUploadEnabled = true
}
}
}
}After these changes, Crashlytics will automatically report crashes in your app’s native code.
To verify that Crashlytics is correctly installed, you need to force a test crash in the app.
Add code to your main activity (e.g., in onCreate) to trigger a crash a few
seconds after app startup:
import android.os.Handler
import android.os.Looper
// ... in your Activity's onCreate method or similar startup logic
Handler(Looper.getMainLooper()).postDelayed({
throw RuntimeException("Test Crash") // Force a crash after 3 seconds
}, 3000)Run your app on a device or emulator. The app should crash after a short delay.
Restart the app. The Crashlytics SDK will send the crash report to Firebase on the next app launch.
After a few minutes, the crash should be available in the Firebase console. Go to DevOps & Engagement > Crashlytics to view your dashboard and crash reports.
get_report tool to check
that a crash was received.get_report tool or manually viewing it in the Firebase console - remove
the code from step 1 that triggers the crash. This prevents the application
from always crashing on start up after a delay.Customize reports to help you better understand what’s happening in your app and the circumstances around events reported to Crashlytics. See Customize Crash Reports for Android (opens in a new tab).