開始使用 Firebase Crashlytics


本快速入門說明如何使用 Firebase Crashlytics SDK 在應用程式中設定 Firebase Crashlytics,以便您在 Firebase 主控台中取得完整的當機報告。使用 Android 版 Crashlytics,您可以取得當機、非嚴重錯誤和「應用程式無回應」(ANR) 錯誤的報表。

設定 Crashlytics 需要在 Firebase 主控台和 IDE 中執行多項工作 (例如新增 Firebase 設定檔和 Crashlytics SDK)。如要完成設定,您必須強制測試當機,才能將第一份當機報告傳送至 Firebase。

事前準備

  1. 如果您尚未將 Firebase 新增至 Android 專案,請先新增。如果您沒有 Android 應用程式,可以下載範例應用程式

  2. 建議:如要自動取得導覽標記記錄,瞭解導致當機、非致命或 ANR 事件的使用者動作,您必須在 Firebase 專案中啟用 Google Analytics

    • 如果現有的 Firebase 專案未啟用 Google Analytics,您可以前往 Firebase 控制台的 >「專案設定」整合分頁標籤,啟用 Google Analytics

    • 如果您要建立新的 Firebase 專案,請在專案建立工作流程中啟用 Google Analytics

  3. 請確認應用程式具備以下最低必要版本:

    • Gradle 8.0
    • Android Gradle 外掛程式 8.1.0 版
    • Google 服務 Gradle 外掛程式 4.4.1 版

步驟 1:在應用程式中加入 Crashlytics SDK

<project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradleCrashlytics建議您使用 Firebase Android BoM 來控制程式庫版本。

如要充分運用麵包屑記錄,請將 Google Analytics 專用的 Firebase SDK 新增至應用程式。請確認 Firebase 專案已啟用 Google Analytics

dependencies {
    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:33.8.0"))

    // 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:19.4.0")
    implementation("com.google.firebase:firebase-analytics:22.2.0")
}
想尋找 Kotlin 專屬的程式庫模組嗎?2023 年 10 月 (Firebase BoM 32.5.0)起,Kotlin 和 Java 開發人員都可以依附主要程式庫模組 (詳情請參閱這項計畫的常見問題)。

步驟 2:將 Crashlytics Gradle 外掛程式新增至應用程式

  1. 根層級 (專案層級) Gradle 檔案 (<project>/build.gradle.kts<project>/build.gradle) 中,將 Crashlytics Gradle 外掛程式新增至 plugins 區塊:

    Kotlin

    plugins {
        // Make sure that you have the AGP plugin 8.1+ dependency
        id("com.android.application") version "8.1.4" apply false
        // ...
    
        // Make sure that you have the Google services Gradle plugin 4.4.1+ dependency
        id("com.google.gms.google-services") version "4.4.2" apply false
    
        // Add the dependency for the Crashlytics Gradle plugin
        id("com.google.firebase.crashlytics") version "3.0.2" apply false
    }

    Groovy

    plugins {
        // Make sure that you have the AGP plugin 8.1+ dependency
        id 'com.android.application' version '8.1.4' apply false
        // ...
    
        // Make sure that you have the Google services Gradle plugin 4.4.1+ dependency
        id 'com.google.gms.google-services' version '4.4.2' apply false
    
        // Add the dependency for the Crashlytics Gradle plugin
        id 'com.google.firebase.crashlytics' version '3.0.2' apply false
    }
  2. 模組 (應用程式層級) Gradle 檔案 (通常是 <project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle) 中,新增 Crashlytics Gradle 外掛程式:

    Kotlin

    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")
    }

    Groovy

    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 資訊主頁中看見初始資料。

  1. 在應用程式中新增可用於強制測試當機的程式碼。

    您可以在應用程式的 MainActivity 中使用以下程式碼,為應用程式新增按鈕,當按下按鈕時會導致應用程式當機。按鈕標示為「Test Crash」。

    Kotlin

    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));
  2. 建構並執行應用程式。

  3. 強制執行測試當機,以便傳送應用程式的首份當機報告:

    1. 在測試裝置或模擬器中開啟應用程式。

    2. 在應用程式中,按下您使用上述程式碼新增的「Test Crash」按鈕。

    3. 應用程式當機後,請重新啟動應用程式,以便將當機報告傳送至 Firebase。

  4. 前往 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 說明文件,瞭解如何使用空氣品質指標視窗
    • 歡迎您將使用心得與我們分享!如要提供空氣品質資訊視窗的意見回饋,請回報錯誤