開始使用 Android 版 Performance Monitoring

事前準備

如果還沒試過 將 Firebase 新增至您的 Android 專案

步驟 1:在應用程式中新增 Performance Monitoring SDK

新增 Performance Monitoring SDK 後,Firebase 就會自動開始收集 應用程式的畫面轉譯和資料 與應用程式的生命週期相關 (例如 應用程式開始時間)。目的地: 如要讓 Firebase 監控網路要求,您必須同時新增 Performance Monitoring Gradle 外掛程式 (下一步)。

  1. 模組 (應用程式層級) Gradle 檔案中 (通常為 <project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle)、 新增 Android Performance Monitoring 程式庫的依附元件。建議您使用 Firebase Android BoM 管理程式庫版本管理

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:33.1.2"))
    
        // Add the dependency for the Performance Monitoring library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-perf")
    }
    

    使用 Firebase Android BoM 應用程式一律會使用相容的 Firebase Android 程式庫版本。

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

    如果選擇不使用 Firebase BoM,則須指定各個 Firebase 程式庫版本 都屬於依附元件行

    請注意,如果您在應用程式中使用多個 Firebase 程式庫,強烈建議您 建議使用 BoM 管理程式庫版本 相容。

    dependencies {
        // Add the dependency for the Performance Monitoring library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-perf:21.0.1")
    }
    
    在尋找 Kotlin 專用的程式庫模組嗎?距離開始還有 2023 年 10 月 (Firebase BoM 32.5.0),Kotlin 和 Java 開發人員都能 依附於主要程式庫模組 (詳情請參閱 這項計畫的常見問題)。

  2. 重新編譯應用程式。

步驟 2:在應用程式中新增 Performance Monitoring Gradle 外掛程式

新增 Performance Monitoring Gradle 外掛程式後,Firebase 會自動啟動 收集資料 HTTP/S 網路要求。 外掛程式也能讓您使用 @AddTrace 註解

  1. 在您的根層級 (專案層級) Gradle 檔案 (<project>/build.gradle.kts<project>/build.gradle),請將 Performance Monitoring Gradle 外掛程式:

    Kotlin

    plugins {
        // To benefit from the latest Performance Monitoring plugin features,
        // update your Android Gradle plugin dependency to at least v3.4.0
        id("com.android.application") version "7.3.0" apply false
    
        // Make sure that you have the Google services Gradle plugin dependency
        id("com.google.gms.google-services") version "4.4.2" apply false
    
        // Add the dependency for the Performance Monitoring Gradle plugin
        id("com.google.firebase.firebase-perf") version "1.4.2" apply false
    }
    

    Groovy

    plugins {
        // To benefit from the latest Performance Monitoring plugin features,
        // update your Android Gradle plugin dependency to at least v3.4.0
        id 'com.android.application' version '7.3.0' apply false
    
        // Make sure that you have the Google services Gradle plugin dependency
        id 'com.google.gms.google-services' version '4.4.2' apply false
    
        // Add the dependency for the Performance Monitoring Gradle plugin
        id 'com.google.firebase.firebase-perf' version '1.4.2' apply false
    }
    
  2. 模組 (應用程式層級) Gradle 檔案中 (通常為 <project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle),新增 Performance Monitoring 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 Performance Monitoring Gradle plugin
        id("com.google.firebase.firebase-perf")
        ...
    }
    

    Groovy

    plugins {
        id 'com.android.application'
    
        // Make sure that you have the Google services Gradle plugin
        id 'com.google.gms.google-services'
    
        // Add the Performance Monitoring Gradle plugin
        id 'com.google.firebase.firebase-perf'
        ...
    }
    
  3. 重新編譯應用程式。

步驟 3:產生初始資料顯示的效能事件

成功將 SDK 加進您的 應用程式。如果您仍在本機開發,可與應用程式互動來產生 收集及處理初次資料收集和處理的事件

  1. 將應用程式切換為背景或前景以產生事件 反覆切換不同畫面來與應用程式互動 及/或觸發網路要求

  2. 前往「效能資訊主頁 也可使用 Firebase 控制台您應該會看到 稍等幾分鐘。

    如果沒看到初始資料,請查看疑難排解 提示

步驟 4(選用) 查看效能事件的記錄訊息

  1. 新增 <meta-data>,在建構期間啟用 Performance Monitoring 的偵錯記錄功能 加入到應用程式的 AndroidManifest.xml 檔案中,如下所示:

    <application>
        <meta-data
          android:name="firebase_performance_logcat_enabled"
          android:value="true" />
    </application>
    
  2. 檢查記錄訊息,看看是否有任何錯誤訊息。

  3. Performance Monitoring 會使用 FirebasePerformance 標記記錄訊息。使用 logcat 就能特別查看持續時間追蹤記錄和 HTTP/S 網路 來要求記錄:

    adb logcat -s FirebasePerformance
  4. 查看下列類型的記錄檔,瞭解效能監控功能是否為 記錄效能事件:

    • Logging trace metric: TRACE_NAME, FIREBASE_PERFORMANCE_CONSOLE_URL
    • Logging network request trace: URL
  5. 按一下網址,即可在 Firebase 控制台查看資料。這項作業可能需要 資料更新。

如果您的應用程式未記錄效能事件,請參閱疑難排解 提示

步驟 5(選用) 為特定程式碼新增自訂監控功能

如要監控應用程式中與特定程式碼相關聯的效能資料,您可以 檢測自訂程式碼追蹤記錄

透過自訂程式碼追蹤記錄,即可評估應用程式完成時間 是一組特定的工作或一組工作,例如載入一組圖片或查詢 資料庫。自訂程式碼追蹤記錄的預設指標為持續時間,但 您也可以新增自訂指標,例如快取命中和記憶體警告。

您在程式碼中定義自訂程式碼追蹤記錄的開頭和結尾 ( 新增任何需要的自訂指標)。請使用 Performance Monitoring SDK 提供的 API。 如果是 Android 應用程式,您也可以監控 具體方式 @AddTrace 註解

請參閱新增特定程式碼的監控功能。 深入瞭解這些功能,以及如何將這些功能新增至應用程式。

步驟 6:部署應用程式並查看結果

使用一或多個測試裝置驗證 Performance Monitoring 後,即可 為使用者部署新版應用程式

您可以在 「效能」資訊主頁 也可使用 Firebase 控制台

已知問題

  • Performance Monitoring Gradle 外掛程式 1.1.0 版可能導致 Guava 出現不一致的情況 依附元件,造成下列錯誤:

    Error:Execution failed for task ':app:packageInstantRunResourcesDebug'.
    > com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;

    如果看到這則錯誤訊息,您可以採取下列其中一種做法:

    • 將 Performance Monitoring 外掛程式升級至 1.1.1 以上版本 (最新的 1.4.2 版)。

    • 將 Performance Monitoring 外掛程式的依附元件行 根層級 (專案層級) Gradle 檔案 (<project>/build.gradle.kts) 或 <project>/build.gradle),如下所示:

      Kotlin

      buildscript {
        // ...
      
        dependencies {
          // ...
      
          // Replace the standard Performance Monitoring plugin dependency line, as follows:
          classpath("com.google.firebase:perf-plugin:1.1.0") {
              exclude(group = "com.google.guava", module = "guava-jdk5")
          }
        }
      }
      

      Groovy

      buildscript {
        // ...
      
        dependencies {
          // ...
      
          // Replace the standard Performance Monitoring plugin dependency line, as follows:
          classpath('com.google.firebase:perf-plugin:1.1.0') {
              exclude group: 'com.google.guava', module: 'guava-jdk5'
          }
        }
      }
      
  • Performance Monitoring 會根據 HTTP 網路要求回報總酬載大小 HTTP 內容長度標頭中設定的值這個值可能 不一定正確

  • Performance Monitoring 僅支援多程序 Android 應用程式中的主要程序。

後續步驟