在應用程式開發和測試期間,您可能會發現停用 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 旗標:
透過擴充功能屬性旗標停用外掛程式
您可以使用擴充功能屬性旗標,在編譯時間停用特定建構變化版本的 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 檔案 (通常是
<project>/<app-module>/build.gradle.kts或<project>/<app-module>/build.gradle) 的dependencies區段中:Kotlin
implementation("com.google.firebase:firebase-config-ktx:24.0.0")Java
implementation("com.google.firebase:firebase-config:24.0.0")如果
perf_disable設為true,請設定 Remote Config 並停用 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 仍會保持啟用狀態。