開始使用適用於 Android 的性能監控

在你開始之前

如果您尚未將 Firebase 添加到您的 Android 項目中,請將其添加到您的 Android 項目中。

第 1 步:將性能監控 SDK 添加到您的應用程序

添加性能監控 SDK 後,Firebase 會自動開始收集應用的屏幕渲染數據以及與應用生命週期相關的數據(例如應用啟動時間)。要使 Firebase 能夠監控網絡請求,您必須添加性能監控 Gradle 插件(下一步)。

  1. 模塊(應用程序級)Gradle 文件(通常<project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle )中,添加性能監控的依賴項安卓庫。我們建議使用Firebase Android BoM來控制庫版本控制。

    Kotlin+KTX

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.3.1"))
    
        // 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-ktx")
    }
    

    通過使用Firebase Android BoM ,您的應用將始終使用 Firebase Android 庫的兼容版本。

    (替代方法)在不使用 BoM 的情況下添加 Firebase 庫依賴項

    如果您選擇不使用 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-ktx:20.4.1")
    }
    

    Java

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.3.1"))
    
        // 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 庫的兼容版本。

    (替代方法)在不使用 BoM 的情況下添加 Firebase 庫依賴項

    如果您選擇不使用 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:20.4.1")
    }
    

  2. 重新編譯您的應用程序。

第 2 步:將性能監控 Gradle 插件添加到您的應用程序中

添加性能監控 Gradle 插件後,Firebase 會自動開始收集HTTP/S 網絡請求的數據。該插件還允許您使用@AddTrace 註釋來檢測自定義代碼跟踪。

  1. 根級別(項目級別) Gradle 文件( <project>/build.gradle.kts<project>/build.gradle )中,添加性能監控 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.2.0" apply false
    
        // Make sure that you have the Google services Gradle plugin dependency
        id("com.google.gms.google-services") version "4.3.15" 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.2.0' apply false
    
        // Make sure that you have the Google services Gradle plugin dependency
        id 'com.google.gms.google-services' version '4.3.15' 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 )中,添加性能監控 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 添加到您的應用時,Firebase 開始處理事件。如果您仍在本地開發,請與您的應用程序交互以生成用於初始數據收集和處理的事件。

  1. 通過在後台和前台之間多次切換應用程序、通過跨屏幕導航與應用程序交互和/或觸發網絡請求來生成事件。

  2. 轉到 Firebase 控制台的性能儀表板。您應該會在幾分鐘內看到初始數據顯示。

    如果您沒有看到初始數據的顯示,請查看故障排除提示

步驟 4 :(可選)查看性能事件的日誌消息

  1. 通過將<meta-data>元素添加到應用程序的AndroidManifest.xml文件中,在構建時啟用性能監控的調試日誌記錄,如下所示:

    <application>
        <meta-data
          android:name="firebase_performance_logcat_enabled"
          android:value="true" />
    </application>
    
  2. 檢查日誌消息中是否有任何錯誤消息。

  3. 性能監控使用FirebasePerformance標記其日誌消息。使用 logcat 過濾,您可以通過運行以下命令專門查看持續時間跟踪和 HTTP/S 網絡請求日誌記錄:

    adb logcat -s FirebasePerformance
  4. 檢查以下類型的日誌,這些日誌表明性能監控正在記錄性能事件:

    • Logging trace metric: TRACE_NAME , FIREBASE_PERFORMANCE_CONSOLE_URL
    • Logging network request trace: URL
  5. 單擊 URL 可在 Firebase 控制台中查看您的數據。儀表板中的數據更新可能需要一些時間。

如果您的應用沒有記錄性能事件,請查看故障排除提示

步驟 5 :(可選)為特定代碼添加自定義監控

要監控與應用程序中特定代碼相關的性能數據,您可以檢測自定義代碼跟踪

通過自定義代碼跟踪,您可以測量應用程序完成特定任務或一組任務(例如加載一組圖像或查詢數據庫)所需的時間。自定義代碼跟踪的默認指標是其持續時間,但您也可以添加自定義指標,例如緩存命中和內存警告。

在代碼中,您可以使用性能監控 SDK 提供的 API 定義自定義代碼跟踪的開始和結束(並添加任何所需的自定義指標)。對於 Android 應用程序,您還可以使用@AddTrace 註解監視特定方法的持續時間。

請訪問添加特定代碼的監控以了解有關這些功能以及如何將它們添加到您的應用程序的更多信息。

第 6 步:部署您的應用程序然後查看結果

使用一台或多台測試設備驗證性能監控後,您可以向用戶部署應用程序的更新版本。

您可以在 Firebase 控制台的性能儀表板中監控性能數據。

已知的問題

  • 性能監控 Gradle 插件 v1.1.0 可能會導致 Guava 依賴項不匹配,從而導致以下錯誤:

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

    如果您看到此錯誤,您可以:

    • 將性能監控插件升級到v1.1.1或更高版本(最新的是v1.4.2)。

    • 替換根級別(項目級別) 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'
          }
        }
      }
      
  • 性能監控根據 HTTP 內容長度標頭中設置的值報告 HTTP 網絡請求的總負載大小。該值可能並不總是準確的。

  • 性能監控僅支持多進程Android應用程序中的主進程。

下一步