|
| 1 | +# Adding Firebase Crashlytics to an Android App |
| 2 | + |
| 3 | +This document provides a comprehensive guide for adding the Firebase Crashlytics SDK to an Android application, based on the latest official Firebase documentation. |
| 4 | + |
| 5 | +## Step 1: Update your Gradle configuration |
| 6 | + |
| 7 | +> **Note:** The following instructions are for projects using Gradle 8.0 or higher. If you are using an older version of Gradle, please refer to the official Firebase documentation for the appropriate setup. |
| 8 | +
|
| 9 | +### Project-level `settings.gradle.kts` (or `settings.gradle`) |
| 10 | + |
| 11 | +In your project's root `settings.gradle.kts` or `settings.gradle` file, add the following to the `pluginManagement` block: |
| 12 | + |
| 13 | +**Kotlin (`settings.gradle.kts`):** |
| 14 | + |
| 15 | +```kotlin |
| 16 | +pluginManagement { |
| 17 | + repositories { |
| 18 | + google() |
| 19 | + mavenCentral() |
| 20 | + gradlePluginPortal() |
| 21 | + } |
| 22 | + plugins { |
| 23 | + id("com.android.application") version "8.4.1" apply false |
| 24 | + id("com.google.gms.google-services") version "4.4.2" apply false |
| 25 | + id("com.google.firebase.crashlytics") version "3.0.1" apply false |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +**Groovy (`settings.gradle`):** |
| 31 | + |
| 32 | +```groovy |
| 33 | +pluginManagement { |
| 34 | + repositories { |
| 35 | + google() |
| 36 | + mavenCentral() |
| 37 | + gradlePluginPortal() |
| 38 | + } |
| 39 | + plugins { |
| 40 | + id 'com.android.application' version '8.4.1' apply false |
| 41 | + id 'com.google.gms.google-services' version '4.4.2' apply false |
| 42 | + id 'com.google.firebase.crashlytics' version '3.0.1' apply false |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### App-level `build.gradle.kts` (or `build.gradle`) |
| 48 | + |
| 49 | +In your app's `build.gradle.kts` or `build.gradle` file (usually located at `app/build.gradle`), apply the Crashlytics plugin and add the SDK dependencies. |
| 50 | + |
| 51 | +**Kotlin (`build.gradle.kts`):** |
| 52 | + |
| 53 | +```kotlin |
| 54 | +plugins { |
| 55 | + alias(libs.plugins.android.application) |
| 56 | + alias(libs.plugins.google.gms.google.services) |
| 57 | + alias(libs.plugins.google.firebase.crashlytics) |
| 58 | +} |
| 59 | + |
| 60 | +dependencies { |
| 61 | + // Import the Firebase BoM |
| 62 | + implementation(platform("com.google.firebase:firebase-bom:33.1.2")) |
| 63 | + |
| 64 | + // Add the dependency for the Firebase Crashlytics library |
| 65 | + // When using the BoM, you don't specify versions in Firebase library dependencies |
| 66 | + implementation("com.google.firebase:firebase-crashlytics-ktx") |
| 67 | + |
| 68 | + // To get breadcrumb logs for crash reports, it's recommended to also add the Firebase Analytics dependency |
| 69 | + implementation("com.google.firebase:firebase-analytics-ktx") |
| 70 | + |
| 71 | + // For apps with native code, it's recommended to also add the Crashlytics NDK dependency |
| 72 | + implementation("com.google.firebase:firebase-crashlytics-ndk") |
| 73 | +} |
| 74 | + |
| 75 | +// If your app uses native code, configure the Crashlytics extension to upload native symbols. |
| 76 | +firebaseCrashlytics { |
| 77 | + // Enable processing and uploading of native symbols to Crashlytics. |
| 78 | + // This flag is disabled by default because it requires you to have the Android NDK installed. |
| 79 | + nativeSymbolUploadEnabled.set(true) |
| 80 | + |
| 81 | + // Enable uploading of ProGuard/R8 mapping files |
| 82 | + // This is required for de-obfuscating stack traces if your app is minified. |
| 83 | + mappingFileUploadEnabled.set(true) |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +**Groovy (`build.gradle`):** |
| 88 | + |
| 89 | +```groovy |
| 90 | +plugins { |
| 91 | + id 'com.android.application' |
| 92 | + id 'com.google.gms.google-services' |
| 93 | + id 'com.google.firebase.crashlytics' |
| 94 | +} |
| 95 | +
|
| 96 | +dependencies { |
| 97 | + // Import the Firebase BoM |
| 98 | + implementation platform('com.google.firebase:firebase-bom:33.1.2') |
| 99 | +
|
| 100 | + // Add the dependency for the Firebase Crashlytics library |
| 101 | + // When using the BoM, you don't specify versions in Firebase library dependencies |
| 102 | + implementation 'com.google.firebase:firebase-crashlytics-ktx' |
| 103 | +
|
| 104 | + // To get breadcrumb logs for crash reports, it's recommended to also add the Firebase Analytics dependency |
| 105 | + implementation 'com.google.firebase:firebase-analytics-ktx' |
| 106 | +
|
| 107 | + // For apps with native code, it's recommended to also add the Crashlytics NDK dependency |
| 108 | + implementation 'com.google.firebase:firebase-crashlytics-ndk' |
| 109 | +} |
| 110 | +
|
| 111 | +// If your app uses native code, configure the Crashlytics extension to upload native symbols. |
| 112 | +firebaseCrashlytics { |
| 113 | + // Enable processing and uploading of native symbols to Crashlytics. |
| 114 | + // This flag is disabled by default because it requires you to have the Android NDK installed. |
| 115 | + nativeSymbolUploadEnabled true |
| 116 | + |
| 117 | + // Enable uploading of ProGuard/R8 mapping files |
| 118 | + // This is required for de-obfuscating stack traces if your app is minified. |
| 119 | + mappingFileUploadEnabled true |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +## Step 1.1: Troubleshooting Native Symbols |
| 124 | + |
| 125 | +If you are using NDK and dont see symbolicated stack traces: |
| 126 | + |
| 127 | +1. Ensure you have the **Android NDK** installed in Android Studio (SDK Manager > SDK Tools > NDK (Side by side)). |
| 128 | +2. Ensure `unstrippedNativeLibsDir` is pointing to the correct location if you are using a custom build system. |
| 129 | +3. Force a refresh of dependencies: `./gradlew clean app:assembleDebug --refresh-dependencies`. |
| 130 | + |
| 131 | +## Step 1.2: Understanding ANRs |
| 132 | + |
| 133 | +Crashlytics automatically captures ANR (Application Not Responding) events on Android 11+ devices. |
| 134 | +* **Requirement:** The app must be installed from the Google Play Store (or recognized by Play Services) for ANRs to be reported in many cases, though strictly local debugging often catches them too. |
| 135 | +* **No Extra Code:** You generally do not need extra code to enable ANR reporting with the latest SDKs. |
| 136 | +``` |
| 137 | +
|
| 138 | +> **Note:** For the BoM and plugin versions, please refer to the official Firebase documentation for the latest versions. |
| 139 | +
|
| 140 | +## Step 2: Implement User Consent (Optional but Recommended) |
| 141 | +
|
| 142 | +It is a best practice to allow users to opt-out of crash reporting. You can control data collection by enabling or disabling it programmatically. |
| 143 | +
|
| 144 | +In your `Application` class or main activity, you can add the following code: |
| 145 | +
|
| 146 | +```java |
| 147 | +import com.google.firebase.crashlytics.FirebaseCrashlytics; |
| 148 | +
|
| 149 | +// ... |
| 150 | +
|
| 151 | +// Check for user's preference and enable/disable collection accordingly |
| 152 | +FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(userHasOptedIn); |
| 153 | +``` |
| 154 | + |
| 155 | +You can also do this in your `AndroidManifest.xml` by adding `<meta-data android:name="firebase_crashlytics_collection_enabled" android:value="false" />` inside the `<application>` tag. The programmatic approach is more flexible as it allows you to change the setting at runtime. |
| 156 | + |
| 157 | +## Step 3: Using Crashlytics |
| 158 | + |
| 159 | +Once initialized, Crashlytics will automatically report crashes. You can also use it to log custom events and non-fatal exceptions. |
| 160 | + |
| 161 | +### Log Custom Messages |
| 162 | + |
| 163 | +```java |
| 164 | +FirebaseCrashlytics.getInstance().log("User performed a custom action"); |
| 165 | +``` |
| 166 | + |
| 167 | +### Record Non-Fatal Exceptions |
| 168 | + |
| 169 | +```java |
| 170 | +try { |
| 171 | + // ... |
| 172 | +} catch (Exception e) { |
| 173 | + FirebaseCrashlytics.getInstance().recordException(e); |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +### Set User Identifiers |
| 178 | + |
| 179 | +```java |
| 180 | +FirebaseCrashlytics.getInstance().setUserId("user123"); |
| 181 | +``` |
| 182 | + |
| 183 | +### Set Custom Keys |
| 184 | + |
| 185 | +```java |
| 186 | +FirebaseCrashlytics.getInstance().setCustomKey("level", "5"); |
| 187 | +FirebaseCrashlytics.getInstance().setCustomKey("score", "10000"); |
| 188 | +``` |
| 189 | + |
| 190 | +## Step 4: Final Steps |
| 191 | + |
| 192 | +1. Sync your project with the Gradle files. |
| 193 | +2. Run your app to verify the implementation. |
| 194 | +3. To test your implementation, you can force a test crash: |
| 195 | + ```java |
| 196 | + throw new RuntimeException("Test Crash"); // Force a crash |
| 197 | + ``` |
| 198 | +4. After the app crashes, run it again so the Crashlytics SDK can upload the report. |
| 199 | +5. Check the Firebase Crashlytics dashboard to see the crash report. |
0 commit comments