事前準備
如果您尚未將 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:33.1.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 程式庫版本。
(替代做法) 新增 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") }
重新編譯應用程式。
步驟 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.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 }
在模組 (應用程式層級) 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:(選用) 查看效能事件的記錄訊息
將
<meta-data>
元素新增至應用程式的AndroidManifest.xml
檔案,即可在建構期間啟用 Performance Monitoring 的偵錯記錄功能,如下所示:<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_URL
Logging 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 以上版本 (最新版本為 1.4.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 內容長度標頭中設定的值,回報 HTTP 網路要求的總酬載大小。這個值不一定準確。
Performance Monitoring 僅支援多程序 Android 應用程式中的主要程序。
後續步驟
進一步瞭解 Performance Monitoring 自動收集的資料:
- 與應用程式生命週期相關的資料,例如應用程式開始時間
- 應用程式中的畫面轉譯資料
- 應用程式發出的 HTTP/S 網路要求相關資料
在 Firebase 控制台中查看、追蹤及篩選效能資料。
檢測自訂程式碼追蹤記錄,為應用程式中的特定工作或工作流程新增監控功能。