本快速入門介紹瞭如何使用 Firebase Crashlytics SDK 在您的應用中設置 Firebase Crashlytics,以便您可以在 Firebase 控制台中獲得全面的崩潰報告。使用適用於 Android 的 Crashlytics,您可以獲得有關崩潰、非致命錯誤和“應用程序無響應”(ANR) 錯誤的報告。
設置 Crashlytics 需要在 Firebase 控制台和您的 IDE 中執行任務(例如添加 Firebase 配置文件和 Crashlytics SDK)。要完成設置,您需要強制測試崩潰以將您的第一個崩潰報告發送到 Firebase。
在你開始之前
如果您還沒有,請將 Firebase 添加到您的 Android 項目中。如果您沒有 Android 應用程序,可以下載示例應用程序。
推薦:要獲得無崩潰用戶、麵包屑日誌和速度警報等功能,您需要在 Firebase 項目中啟用 Google Analytics。
如果您現有的 Firebase 項目沒有啟用 Google Analytics,您可以從 Firebase 控制台中集成選項卡啟用 Google Analytics。
>項目設置的如果您要創建新的 Firebase 項目,請在項目創建工作流程中啟用 Google Analytics。
第 1 步:將 Crashlytics SDK 添加到您的應用
在您的模塊(應用程序級)Gradle 文件(通常為<project>/<app-module>/build.gradle
)中,添加 Crashlytics Android 庫的依賴項。我們建議使用Firebase Android BoM來控制庫版本。為了獲得最佳的 Crashlytics 體驗,我們建議在您的 Firebase 項目中啟用 Google Analytics ,並將適用於 Google Analytics 的 Firebase SDK 添加到您的應用程序中。
Kotlin+KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:31.2.3') // Add the dependencies for the Crashlytics and Analytics libraries // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-crashlytics-ktx' implementation 'com.google.firebase:firebase-analytics-ktx' }
通過使用Firebase Android BoM ,您的應用將始終使用兼容版本的 Firebase Android 庫。
(備選)在不使用 BoM 的情況下添加 Firebase 庫依賴項
如果您選擇不使用 Firebase BoM,則必須在其依賴項行中指定每個 Firebase 庫版本。
請注意,如果您在應用中使用多個Firebase 庫,我們強烈建議您使用 BoM 來管理庫版本,以確保所有版本都兼容。
dependencies { // Add the dependencies for the Crashlytics and Analytics libraries // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-crashlytics-ktx:18.3.5' implementation 'com.google.firebase:firebase-analytics-ktx:21.2.0' }
Java
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:31.2.3') // Add the dependencies for the Crashlytics and Analytics libraries // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-crashlytics' implementation 'com.google.firebase:firebase-analytics' }
通過使用Firebase Android BoM ,您的應用將始終使用兼容版本的 Firebase Android 庫。
(備選)在不使用 BoM 的情況下添加 Firebase 庫依賴項
如果您選擇不使用 Firebase BoM,則必須在其依賴項行中指定每個 Firebase 庫版本。
請注意,如果您在應用中使用多個Firebase 庫,我們強烈建議您使用 BoM 來管理庫版本,以確保所有版本都兼容。
dependencies { // Add the dependencies for the Crashlytics and Analytics libraries // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-crashlytics:18.3.5' implementation 'com.google.firebase:firebase-analytics:21.2.0' }
第 2 步:將 Crashlytics Gradle 插件添加到您的應用
在根級(項目級) Gradle 文件 (
<project>/build.gradle
) 中,將 Crashlytics Gradle 插件添加為 buildscript 依賴項:buildscript { repositories { // Make sure that you have the following two repositories google() // Google's Maven repository mavenCentral() // Maven Central repository } dependencies { ... classpath 'com.android.tools.build:gradle:7.2.0' // Make sure that you have the Google services Gradle plugin dependency classpath 'com.google.gms:google-services:4.3.15' // Add the dependency for the Crashlytics Gradle plugin classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.4' } }
在您的模塊(應用程序級) Gradle 文件(通常是
<project>/<app-module>/build.gradle
)中,添加 Crashlytics Gradle 插件:plugins { id 'com.android.application' // Make sure that you have the Google services Gradle plugin id 'com.google.gms.google-services' // Add the Crashlytics Gradle plugin id 'com.google.firebase.crashlytics' ... }
第 3 步:強制測試崩潰以完成設置
要完成 Crashlytics 的設置並在 Firebase 控制台的 Crashlytics 儀表板中查看初始數據,您需要強制測試崩潰。
將可用於強制測試崩潰的代碼添加到您的應用程序。
您可以在應用的
MainActivity
中使用以下代碼向您的應用添加一個按鈕,按下該按鈕會導致崩潰。該按鈕標記為“測試崩潰”。Kotlin+KTX
val crashButton = Button(this) crashButton.text = "Test Crash" crashButton.setOnClickListener { throw RuntimeException("Test Crash") // Force a crash } addContentView(crashButton, ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
Java
Button crashButton = new Button(this); crashButton.setText("Test Crash"); crashButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { throw new RuntimeException("Test Crash"); // Force a crash } }); addContentView(crashButton, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
構建並運行您的應用程序。
強制測試崩潰以發送您的應用程序的第一個崩潰報告:
從您的測試設備或模擬器打開您的應用程序。
在您的應用中,按下您使用上述代碼添加的“測試崩潰”按鈕。
在您的應用程序崩潰後,重新啟動它,以便您的應用程序可以將崩潰報告發送到 Firebase。
轉到 Firebase 控制台的Crashlytics 儀表板以查看您的測試崩潰。
如果您刷新了控制台,但五分鐘後仍未看到測試崩潰,請啟用調試日誌記錄以查看您的應用程序是否正在發送崩潰報告。
就是這樣! Crashlytics 現在正在監控您的應用程序是否存在崩潰、非致命錯誤和 ANR。訪問Crashlytics 儀表板以查看和調查您的所有報告和統計信息。
下一步
- 通過添加選擇加入報告、日誌、密鑰和非致命錯誤跟踪來自定義您的崩潰報告設置。
- 與 Google Play 集成,以便您可以直接在 Crashlytics 儀表板中按 Google Play 跟踪過濾 Android 應用程序的崩潰報告。這使您可以更好地將儀表板集中在特定構建上。
- 在 Android Studio 中,查看和過濾 Crashlytics 數據。
- 使用 Android Studio 中的App Quality Insights (AQI) 窗口查看代碼旁邊的 Crashlytics 數據——無需在 Crashlytics 儀表板和 IDE 之間來回跳轉即可開始調試最重要的問題。
- 在 Android Studio 的 Electric Eel 版本(穩定版)中訪問 AQI 窗口,或在 Flamingo(測試版)中試用新的 AQI 功能。下載所需的 Android Studio 版本。
- 在 Android Studio 文檔中了解如何使用 AQI 窗口。
- 我們很樂意聽取您的意見!通過提交錯誤報告向我們發送有關 AQI 窗口的反饋。