開始使用 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 控制台,在 >「專案設定」中,透過「Integrations」分頁啟用 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.gradle建議您使用 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.0.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 程式庫版本。

(替代方法) 新增 Firebase 程式庫依附元件,「不必」使用 BoM

如果選擇不使用 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.0.0")
    implementation("com.google.firebase:firebase-analytics:22.0.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.1" apply false
    
        // Add the dependency for the Crashlytics Gradle plugin
        id("com.google.firebase.crashlytics") version "3.0.0" 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.1' apply false
    
        // Add the dependency for the Crashlytics Gradle plugin
        id 'com.google.firebase.crashlytics' version '3.0.0' 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+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));
    
  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 的 Electric Eel 版本 (穩定版) 中存取 AQI 視窗,或試用 Flamingo (Beta 版) 的全新 AQI 功能下載所需 Android Studio 版本。
    • 請參閱 Android Studio 說明文件,瞭解如何使用 AQI 視窗
    • 歡迎您將使用心得與我們分享!歡迎提交錯誤報告,針對 AQI 視窗提供意見回饋。