在应用程序开发和测试期间,您可能会发现禁用性能监控很有用。
例如,通过在应用构建过程中禁用性能监控,您可以:
在调试构建中禁用性能监控的某些功能(例如性能监控 Gradle 插件提供的功能),但为发布构建重新启用这些功能。
在构建您的应用程序时禁用性能监控,但允许您的应用程序在运行时重新启用它。
在构建您的应用程序时禁用性能监控,并且不允许您的应用程序在运行时重新启用它。
您还可以在启用性能监控的情况下构建您的应用程序,但使用 Firebase 远程配置可以让您灵活地在生产应用程序中禁用(和重新启用)性能监控。使用此选项,您甚至可以将您的应用配置为让用户选择加入或退出使用性能监控。
在您的应用构建过程中禁用性能监控
您可以通过禁用 Performance Monitoring Gradle 插件和/或禁用 Performance Monitoring Android library在构建过程中禁用 Performance Monitoring。
在开发和调试期间,禁用插件很有用,因为插件的检测会增加构建时间。不过,您可能会考虑保持库处于启用状态,这样您仍然可以从应用程序启动、应用程序在前台和应用程序在后台跟踪以及应用程序中的任何自定义代码跟踪中查看性能数据。
禁用性能监控 Gradle 插件
您可以通过使用以下选项添加instrumentationEnabled
标志来禁用性能监控插件:
通过扩展属性标志禁用插件
通过使用扩展属性标志,您可以在编译时为特定构建变体禁用性能监控插件。
在您的根级(项目级)
build.gradle
文件中,确保您的 Android Gradle 插件依赖项指定为 v3.4.0 或更高版本。对于早期版本的 Android Gradle 插件,您仍然可以禁用特定构建变体的性能监控插件,但不会完全消除该变体的构建时间贡献。
将以下标志添加到您的模块(应用程序级)
build.gradle
文件中,然后将其设置为false
以禁用性能监控插件。android { // ... buildTypes { debug { FirebasePerformance { // Set this flag to 'false' to disable @AddTrace annotation processing and // automatic monitoring of HTTP/S network requests // for a specific build variant at compile time. instrumentationEnabled false } } } }
通过项目属性标志禁用插件
通过使用项目属性标志,您可以在编译时为所有构建变体禁用性能监控插件。
将以下标志添加到您的gradle.properties
文件,然后将其设置为false
以禁用性能监控插件。
// ... // Set this flag to 'false' to disable @AddTrace annotation processing and // automatic monitoring of HTTP/S network requests // for all build variants at compile time. firebasePerformanceInstrumentationEnabled=false
禁用性能监控 Android 库
如果您在编译时禁用性能监控库,您可以选择是否允许您的应用程序在运行时启用该库。
在编译时禁用该库,但允许您的应用程序在运行时启用它
将以下<meta-data>
元素添加到应用的AndroidManifest.xml
文件中:
<application> <meta-data android:name="firebase_performance_collection_enabled" android:value="false" /> </application>
在编译时禁用该库,但不允许您的应用程序在运行时启用它
将以下<meta-data>
元素添加到应用的AndroidManifest.xml
文件中:
<application> <meta-data android:name="firebase_performance_collection_deactivated" android:value="true" /> </application>
使用远程配置在运行时禁用您的应用
Firebase Remote Config 允许您更改应用程序的行为和外观,因此它提供了一种理想的方式让您在已部署的应用程序实例中禁用性能监控。
要在您的 Android 应用程序下次启动时禁用性能监控数据收集,请使用下面显示的示例代码。有关在 Android 应用程序中使用 Remote Config 的更多信息,请参阅在 Android 上使用 Firebase Remote Config 。
确保 Remote Config 位于模块 Gradle 文件(通常是
app/build.gradle
)的dependencies
项部分:implementation 'com.google.firebase:firebase-config:21.2.1'
如果
perf_disable
设置为true
,则设置远程配置并禁用性能监控:Kotlin+KTX
// Setup remote config val config = Firebase.remoteConfig // You can uncomment the following two statements to permit more fetches when // validating your app, but you should comment out or delete these lines before // distributing your app in production. // val configSettings = remoteConfigSettings { // minimumFetchIntervalInSeconds = 3600 // } // config.setConfigSettingsAsync(configSettings) // Load in-app defaults from an XML file that sets perf_disable to false until you update // values in the Firebase Console // Observe the remote config parameter "perf_disable" and disable Performance Monitoring if true config.setDefaultsAsync(R.xml.remote_config_defaults) .addOnCompleteListener { task -> if (task.isSuccessful) { Firebase.performance.isPerformanceCollectionEnabled = !config.getBoolean("perf_disable") } else { // An error occurred while setting default parameters } }
Java
// Setup remote config final FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance(); // You can uncomment the following two statements to permit more fetches when // validating your app, but you should comment out or delete these lines before // distributing your app in production. // FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder() // .setMinimumFetchIntervalInSeconds(3600) // .build(); // config.setConfigSettingsAsync(configSettings); // Load in-app defaults from an XML file that sets perf_disable to false until you update // values in the Firebase Console //Observe the remote config parameter "perf_disable" and disable Performance Monitoring if true config.setDefaultsAsync(R.xml.remote_config_defaults) .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { if (config.getBoolean("perf_disable")) { FirebasePerformance.getInstance().setPerformanceCollectionEnabled(false); } else { FirebasePerformance.getInstance().setPerformanceCollectionEnabled(true); } } else { // An error occurred while setting default parameters } } });
将以下代码添加到
MainActivity.java
以获取和激活远程配置值:Kotlin+KTX
// Remote Config fetches and activates parameter values from the service val config = Firebase.remoteConfig config.fetch(3600) .continueWithTask { task -> if (!task.isSuccessful) { task.exception?.let { throw it } } config.activate() } .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // Parameter values successfully activated // ... } else { // Handle errors } }
Java
//Remote Config fetches and activates parameter values from the service final FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance(); config.fetch(3600) .continueWithTask(new Continuation<Void, Task<Boolean>>() { @Override public Task<Boolean> then(@NonNull Task<Void> task) throws Exception { if (!task.isSuccessful()) { throw task.getException(); } return config.activate(); } }) .addOnCompleteListener(new OnCompleteListener<Boolean>() { @Override public void onComplete(@NonNull Task<Boolean> task) { if (task.isSuccessful()) { // Parameter values successfully activated // ... } else { // Handle errors } } });
要在 Firebase 控制台中禁用性能监控,请在您的应用项目中创建一个perf_disable参数,然后将其值设置为
true
。此更改将调用 Performance Monitoring SDK“无操作”调用 (NOOP),从而消除在您的应用中使用 Performance Monitoring SDK 对应用性能的任何重大影响。
如果将perf_disable的值设置为
false
,性能监控将保持启用状态。