在應用程式開發和測試期間,您可能會發現停用 Performance Monitoring 很有用。
舉例來說,在應用程式建構程序中停用 Performance Monitoring,即可:
在偵錯版本中停用 Performance Monitoring 的特定功能 (例如 Performance Monitoring Gradle 外掛程式提供的功能),但在發布版本中重新啟用這些功能。
建構應用程式時停用 Performance Monitoring,但允許應用程式在執行階段重新啟用。
建構應用程式時停用 Performance Monitoring,且不允許應用程式在執行階段重新啟用。
您也可以使用Performance Monitoring 啟用來建構應用程式,但使用 Firebase Remote Config 可讓您彈性地在正式版應用程式中停用 (及重新啟用) Performance Monitoring。使用這個選項時,您甚至可以設定應用程式,讓使用者選擇是否要使用 Performance Monitoring。
在應用程式建構過程中停用 Performance Monitoring
您可以在建構程序中停用 Performance Monitoring,方法是停用 Performance Monitoring Gradle 外掛程式,和/或停用 Performance Monitoring Android 程式庫。
在開發和偵錯期間,停用外掛程式很有用,因為外掛程式的檢測設備可能會導致建構時間增加。不過,建議您啟用程式庫,這樣就能繼續查看應用程式啟動、應用程式在前台和應用程式在背景追蹤記錄的成效資料,以及應用程式中的任何自訂程式碼追蹤記錄。
停用 Performance Monitoring Gradle 外掛程式
如要停用 Performance Monitoring 外掛程式,請使用下列選項新增 instrumentationEnabled
旗標:
透過擴充功能屬性旗標停用外掛程式
使用 Extensions Property 旗標,即可在編譯時為特定建構變體停用 Performance Monitoring 外掛程式。
在根層級 (專案層級) Gradle 檔案 (
<project>/build.gradle.kts
或<project>/build.gradle
) 中,確認 Android Gradle 外掛程式依附元件指定為 3.4.0 以上版本。如果是舊版 Android Gradle 外掛程式,您仍可為特定建構變種版本停用 Performance Monitoring 外掛程式,但該變種版本的建構時間貢獻不會完全消除。
在模組 (應用程式層級) Gradle 檔案 (通常為
<project>/<app-module>/build.gradle.kts
或<project>/<app-module>/build.gradle
) 中加入下列標記,然後將其設為false
,即可停用 Performance Monitoring 外掛程式。Kotlin
import com.google.firebase.perf.plugin.FirebasePerfExtension // ... android { // ... buildTypes { getByName("debug") { configure<FirebasePerfExtension> { // 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. setInstrumentationEnabled(false) } } } }
Groovy
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 } } } }
透過專案屬性旗標停用外掛程式
使用「專案屬性」標記,即可在編譯時為所有建構變體停用 Performance Monitoring 外掛程式。
在 gradle.properties
檔案中新增下列標記,然後設為 false
即可停用 Performance Monitoring 外掛程式。
// ... // 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
停用 Performance Monitoring Android 程式庫
如果您在編譯時停用 Performance Monitoring 程式庫,可以選擇是否允許應用程式在執行階段啟用該程式庫。
在編譯時停用程式庫,但允許應用程式在執行階段啟用
在應用程式的 AndroidManifest.xml
檔案中新增下列 <meta-data>
元素:
<application> <meta-data android:name="firebase_performance_collection_enabled" android:value="false" /> </application>
在編譯時停用程式庫,但不允許應用程式在執行階段啟用
在應用程式的 AndroidManifest.xml
檔案中新增下列 <meta-data>
元素:
<application> <meta-data android:name="firebase_performance_collection_deactivated" android:value="true" /> </application>
使用 Remote Config 在執行階段停用應用程式
Firebase Remote Config 可讓您變更應用程式的行為和外觀,因此是停用已部署應用程式執行個體中 Performance Monitoring 的理想方式。
如要在下次啟動 Android 應用程式時停用 Performance Monitoring 資料收集功能,請使用下方的範例程式碼。如要進一步瞭解如何在 Android 應用程式中使用 Remote Config,請參閱「在 Android 上使用 Firebase Remote Config」。
請確認 Remote Config 位於模組 (應用程式層級) Gradle 檔案的
dependencies
區段 (通常是<project>/<app-module>/build.gradle.kts
或<project>/<app-module>/build.gradle
):Kotlin
implementation("com.google.firebase:firebase-config-ktx:23.0.0")
Java
implementation("com.google.firebase:firebase-config:23.0.0")
設定 Remote Config,並在
perf_disable
設為true
時停用 Performance Monitoring:Kotlin
// 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
,以擷取及啟用 Remote Config 值:Kotlin
// 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 控制台中停用 Performance Monitoring,請在應用程式專案中建立 perf_disable 參數,然後將值設為
true
。這項異動會將對 Performance Monitoring SDK 的呼叫設為「無運算」呼叫 (NOOP),因此在應用程式中使用 Performance Monitoring SDK 不會對應用程式效能造成重大影響。
如果將 perf_disable 的值設為
false
,Performance Monitoring 仍會保持啟用狀態。