事前準備
如果您尚未將 Firebase 新增至 Android 專案,請先新增。
步驟 1:在應用程式中加入 Performance Monitoring SDK
新增 Performance Monitoring SDK 後,Firebase 會自動開始收集應用程式畫面算繪資料,以及與應用程式生命週期相關的資料 (例如應用程式開始時間)。如要讓 Firebase 監控網路要求,您也必須新增 Performance Monitoring Gradle 外掛程式 (下一步)。
在模組 (應用程式層級) 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:35.0.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:23.0.1") }
重新編譯應用程式。
步驟 2:在應用程式中加入 Performance Monitoring Gradle 外掛程式
新增 Performance Monitoring Gradle 外掛程式後,Firebase 會自動開始收集 HTTP/S 網路要求的資料。此外,您也可以使用 @AddTrace 註解,透過外掛程式檢測自訂程式碼追蹤記錄。
在根層級 (專案層級) 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.5.0" apply false // Add the dependency for the Performance Monitoring Gradle plugin id("com.google.firebase.firebase-perf") version "2.0.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.5.0' apply false // Add the dependency for the Performance Monitoring Gradle plugin id 'com.google.firebase.firebase-perf' version '2.0.2' apply false }
在模組 (應用程式層級) 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:產生效能事件,以便顯示初始資料
成功將 SDK 新增至應用程式後,Firebase 就會開始處理事件。如果您仍在本機開發,請與應用程式互動,產生事件,以便進行初始資料收集和處理。
在背景和前景之間切換應用程式數次、在不同畫面之間瀏覽,以及/或觸發網路要求,藉此與應用程式互動,產生事件。
前往 Firebase 控制台的「Performance」(效能) 資訊主頁。幾分鐘內,您應該會看到初始資料顯示。
如果沒有看到初始資料,請參閱疑難排解提示。
步驟 4:(選用) 查看效能事件的記錄訊息
如要在建構時啟用 Performance Monitoring 的偵錯記錄檔功能,請在應用程式的
AndroidManifest.xml檔案中加入<meta-data>元素,如下所示:<application> <meta-data android:name="firebase_performance_logcat_enabled" android:value="true" /> </application>查看記錄訊息,瞭解是否有任何錯誤訊息。
Performance Monitoring 會使用
FirebasePerformance標記記錄訊息。使用 logcat 篩選功能,您可以執行下列指令,專門查看觀看時間長度追蹤記錄和 HTTP/S 網路要求記錄:adb logcat -s FirebasePerformance
請檢查下列類型的記錄,確認 Performance Monitoring 是否正在記錄效能事件:
Logging trace metric: TRACE_NAME, FIREBASE_PERFORMANCE_CONSOLE_URLLogging network request trace: URL
按一下網址,即可在 Firebase 控制台中查看資料。資訊主頁中的資料可能需要幾分鐘才會更新。
如果應用程式未記錄效能事件,請參閱疑難排解提示。
步驟 5:(選用) 為特定程式碼新增自訂監控
如要監控與應用程式中特定程式碼相關聯的成效資料,您可以檢測自訂程式碼追蹤記錄。
透過自訂程式碼追蹤,您可以評估應用程式完成特定工作或一組工作所需的時間,例如載入一組圖片或查詢資料庫。自訂程式碼追蹤記錄的預設指標是時間長度,但您也可以新增自訂指標,例如快取命中和記憶體警告。
在程式碼中,您可以使用 Performance Monitoring SDK 提供的 API 定義自訂程式碼追蹤記錄的開頭和結尾 (並新增任何所需的自訂指標)。如果是 Android 應用程式,您也可以使用 @AddTrace 註解監控特定方法的持續時間。
請參閱「新增特定程式碼的監控功能」,進一步瞭解這些功能,以及如何將這些功能新增至應用程式。
步驟 6:部署應用程式並查看結果
使用一或多部測試裝置驗證 Performance Monitoring 後,即可向使用者部署更新版應用程式。
您可以在 Firebase 控制台的「Performance」(效能) 資訊主頁中監控成效資料。
已知問題
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 以上版本 (最新版本為 2.0.2)。
在根層級 (專案層級) Gradle 檔案 (
<project>/build.gradle.kts或<project>/build.gradle) 中,替換 Performance Monitoring 外掛程式依附元件行,如下所示: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 content-length 標頭中設定的值,回報 HTTP 網路要求的總酬載大小。這個值不一定準確。
Performance Monitoring 僅支援多程序 Android 應用程式中的主要程序。
後續步驟
進一步瞭解 Performance Monitoring 自動收集的資料:
- 與應用程式生命週期相關的資料,例如應用程式啟動時間
- 應用程式畫面轉譯資料
- 應用程式發出的 HTTP/S 網路要求資料
在 Firebase 控制台中查看、追蹤及篩選成效資料。
檢測自訂程式碼追蹤記錄,為應用程式中的特定工作或工作流程新增監控功能。