停用 Firebase Performance Monitoring

在應用程式開發和測試期間,建議您停用 Performance Monitoring

例如: 在應用程式建構程序中停用 Performance Monitoring。 你可以:

  • 停用 Performance Monitoring 的某些功能 (例如 Performance Monitoring Gradle 外掛程式) ,但重新啟用應用程式所需的功能 發布子版本

  • 建構應用程式時停用 Performance Monitoring,但讓應用程式重新啟用 執行程式碼

  • 在建構應用程式時停用 Performance Monitoring,並禁止應用程式 在執行階段重新啟用。

您也可以在啟用 Performance Monitoring 的情況下建構應用程式,但 使用 Firebase 遠端設定 可讓您在正式版應用程式中停用 (及重新啟用) Performance Monitoring。取代為 這個選項,您甚至可以設定應用程式,讓使用者選擇加入或退出 並取得相關快訊

在應用程式建構程序中停用 Performance Monitoring

您可以在建構程序中停用 Performance Monitoring,步驟如下: 停用 Performance Monitoring Gradle 外掛程式,並/或依照 停用 Performance Monitoring Android 程式庫

在開發和偵錯期間,停用外掛程式很有幫助,因為 這個外掛程式能 進而延長建構時間不過,您可能會考慮保留 啟用後,您仍然可以從 應用程式啟動、前景和應用程式背景追蹤記錄 以及任何 自訂程式碼追蹤記錄

停用 Performance Monitoring Gradle 外掛程式

如要停用 Performance Monitoring 外掛程式,請新增 instrumentationEnabled 標記使用以下選項:

透過擴充功能屬性旗標停用外掛程式

只要使用 Extensions Property 旗標,即可停用 Performance Monitoring 外掛程式 在編譯期間針對特定建構變數設定。

  1. 在您的根層級 (專案層級) Gradle 檔案 (<project>/build.gradle.kts<project>/build.gradle),請確認已 Android Gradle 外掛程式依附元件指定為 3.4.0 以上版本。

    至於舊版 Android Gradle 外掛程式,您還是可以停用 特定建構變數的效能監控外掛程式,但建構時間 該變化版本的貢獻不會完全消除。

  2. 將下列標記新增至「module (app-level)」 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
          }
        }
      }
    }
    

透過專案屬性旗標停用外掛程式

只要使用 Project Property 旗標,即可停用下列應用程式的 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>

使用遠端設定在執行階段停用應用程式

Firebase 遠端設定可讓您變更行為和外觀 以便在應用程式層級停用 Performance Monitoring 所部署的應用程式執行個體

下次您 Android 應用程式要停用 Performance Monitoring 的資料收集功能 就使用下方的程式碼範例如要進一步瞭解如何 Android 應用程式中的遠端設定功能,請參閱 在 Android 上使用 Firebase 遠端設定

  1. 確定遠端設定位於以下項目的 dependencies 部分: 「module (應用程式層級)」Gradle 檔案 (通常 <project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle):

    Kotlin+KTX

      implementation("com.google.firebase:firebase-config-ktx:22.0.0")
    

    Java

      implementation("com.google.firebase:firebase-config:22.0.0")
    
  2. 設定遠端設定,並在下列情況下停用 Performance Monitoring: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
                    }
                }
            });
  3. 將下列程式碼新增至 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
                    }
                }
            });
  4. 如要在 Firebase 控制台中停用 Performance Monitoring,請建立 perf_disable 參數,然後將參數值設為 true

    這項變更會呼叫 Performance Monitoring SDK「無作業」通話 ,應用程式不會因使用 在應用程式中使用 Performance Monitoring SDK

    如果您將 perf_disable 的值設為 false,Performance Monitoring 將保持不變