在你開始之前
如果您還沒有,請將 Firebase 添加到您的 Android 項目中。
第 1 步:將性能監控 SDK 添加到您的應用程序
添加 Performance Monitoring SDK 後,Firebase 會自動開始收集應用的屏幕渲染數據和與應用生命週期相關的數據(例如應用啟動時間)。要使 Firebase 能夠監控網絡請求,您還必須添加性能監控 Gradle 插件(下一步)。
在您的模塊(應用程序級)Gradle 文件(通常為
<project>/<app-module>/build.gradle
)中,添加性能監控 Android 庫的依賴項。我們建議使用Firebase Android BoM來控制庫版本。Kotlin+KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:32.1.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-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.3.2' }
Java
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:32.1.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 庫。
(備選)在不使用 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.3.2' }
重新編譯您的應用程序。
第 2 步:將性能監控 Gradle 插件添加到您的應用程序
添加 Performance Monitoring Gradle 插件後,Firebase 會自動開始收集HTTP/S 網絡請求的數據。該插件還使您能夠使用@AddTrace 註釋來檢測自定義代碼跟踪。
在根級(項目級) Gradle 文件 (
<project>/build.gradle
) 中,將 Performance Monitoring Gradle 插件添加為 buildscript 依賴項:buildscript { repositories { // Make sure that you have the following two repositories google() // Google's Maven repository mavenCentral() // Maven Central repository } dependencies { ... // To benefit from the latest Performance Monitoring plugin features, // update your Android Gradle plugin dependency to at least v3.4.0 classpath 'com.android.tools.build:gradle:7.2.0' // Make sure that you have the Google services Gradle plugin dependency classpath 'com.google.gms:google-services:4.3.15' // Add the dependency for the Performance Monitoring Gradle plugin classpath 'com.google.firebase:perf-plugin:1.4.2' } }
在您的模塊(應用程序級) Gradle 文件(通常是
<project>/<app-module>/build.gradle
)中,添加 Performance Monitoring Gradle 插件: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過濾,可以具體查看duration trace和HTTP/S網絡請求日誌,運行如下命令:adb logcat -s FirebasePerformance
檢查以下類型的日誌,這些日誌表明性能監控正在記錄性能事件:
-
Logging trace metric: TRACE_NAME , FIREBASE_PERFORMANCE_CONSOLE_URL
-
Logging network request trace: URL
-
單擊 URL 以在 Firebase 控制台中查看您的數據。數據在儀表板中更新可能需要一些時間。
如果您的應用未記錄性能事件,請查看故障排除提示。
第 5 步:(可選)為特定代碼添加自定義監控
要監控與應用中特定代碼關聯的性能數據,您可以檢測自定義代碼跟踪。
使用自定義代碼跟踪,您可以測量您的應用程序完成特定任務或一組任務所需的時間,例如加載一組圖像或查詢數據庫。自定義代碼跟踪的默認指標是其持續時間,但您也可以添加自定義指標,例如緩存命中和內存警告。
在您的代碼中,您使用 Performance Monitoring SDK 提供的 API 定義自定義代碼跟踪的開始和結束(並添加任何所需的自定義指標)。對於 Android 應用程序,您還可以使用@AddTrace 註釋監視特定方法的持續時間。
訪問為特定代碼添加監控以了解有關這些功能以及如何將它們添加到您的應用程序的更多信息。
第 6 步:部署您的應用程序然後查看結果
使用一台或多台測試設備驗證性能監控後,您可以將應用程序的更新版本部署給您的用戶。
您可以在 Firebase 控制台的性能儀表板中監控性能數據。
已知的問題
Performance Monitoring Gradle plugin 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 文件 (
build.gradle
) 中的性能監控插件依賴行,如下所示: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 控制台中查看、跟踪和過濾您的性能數據。
通過檢測自定義代碼跟踪,在您的應用程序中添加對特定任務或工作流的監控。