在你开始之前
如果您还没有,请将 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:31.2.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.1' }
Java
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:31.2.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.1' }
重新编译您的应用程序。
第 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 控制台中查看、跟踪和过滤您的性能数据。
通过检测自定义代码跟踪,在您的应用程序中添加对特定任务或工作流的监控。