在你開始之前
如果您尚未將 Firebase 新增至您的 Android 專案中,請將其新增至您的 Android 專案中。
第 1 步:將效能監控 SDK 新增至您的應用程式
在新增效能監控 SDK 後,Firebase 會自動開始收集應用程式的螢幕渲染資料以及與應用程式生命週期相關的資料(例如應用程式啟動時間)。要讓 Firebase 能夠監控網路請求,您還必須新增效能監控 Gradle 插件(下一步)。
在模組(應用程式層級)Gradle 檔案(通常
<project>/<app-module>/build.gradle.kts
或<project>/<app-module>/build.gradle
)中,新增效能監控的依賴項Android 的函式庫。我們建議使用Firebase Android BoM來控制函式庫版本控制。dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:32.6.0")) // 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 程式庫的相容版本。
正在尋找 Kotlin 特定的庫模組?從2023 年 10 月(Firebase BoM 32.5.0)開始,Kotlin 和 Java 開發人員都可以依賴主庫模組(有關詳細信息,請參閱有關此計劃的常見問題解答)。(替代方法)在不使用 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.5.1") }
重新編譯您的應用程式。
步驟 2 :將效能監控 Gradle 插件新增到您的應用程式中
新增效能監控 Gradle 插件後,Firebase 會自動開始收集HTTP/S 網路請求的資料。該插件還允許您使用@AddTrace 註釋來檢測自訂程式碼追蹤。
在根級(專案級) 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.3.0" apply false // Make sure that you have the Google services Gradle plugin dependency id("com.google.gms.google-services") version "4.4.0" 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.0' apply false // Add the dependency for the Performance Monitoring Gradle plugin id 'com.google.firebase.firebase-perf' version '1.4.2' apply false }
在模組(應用程式層級) 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 :產生初始資料顯示的效能事件
當您成功將 SDK 新增至您的應用程式時,Firebase 就會開始處理事件。如果您仍在本地開發,請與您的應用程式互動以產生用於初始資料收集和處理的事件。
透過在背景和前台之間多次切換應用程式、透過跨螢幕導航與應用程式互動和/或觸發網路請求來產生事件。
前往 Firebase 控制台的效能儀表板。您應該會在幾分鐘內看到初始資料顯示。
如果您沒有看到初始資料的顯示,請查看故障排除提示。
步驟 4 :(可選)查看效能事件的日誌訊息
透過將
<meta-data>
元素新增至應用程式的AndroidManifest.xml
檔案中,在建置時啟用效能監控的偵錯日誌記錄,如下所示:<application> <meta-data android:name="firebase_performance_logcat_enabled" android:value="true" /> </application>
檢查日誌訊息中是否有任何錯誤訊息。
效能監控使用
FirebasePerformance
標記其日誌訊息。使用 logcat 過濾,您可以透過執行以下命令專門查看持續時間追蹤和 HTTP/S 網路請求日誌記錄:adb logcat -s FirebasePerformance
檢查以下類型的日誌,這些日誌表明效能監控正在記錄效能事件:
-
Logging trace metric: TRACE_NAME , FIREBASE_PERFORMANCE_CONSOLE_URL
-
Logging network request trace: URL
-
按一下 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應用程式中的主進程。
下一步
了解有關效能監控自動收集的資料的更多資訊:
- 與應用生命週期相關的數據,例如應用程式啟動時間
- 應用程式中螢幕渲染的數據
- 您的應用程式發出的HTTP/S 網路請求的數據
在 Firebase 控制台中檢視、追蹤和篩選您的效能資料。
透過偵測自訂程式碼追蹤來新增對應用程式中特定任務或工作流程的監控。