停用 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,方法是停用 Performance Monitoring Gradle 外掛程式,並/或停用 Performance Monitoring Android 程式庫

在開發和偵錯期間,停用外掛程式會很有幫助,因為外掛程式的檢測作業可能會增加建構時間。不過,建議您讓程式庫保持啟用狀態,以便繼續查看來自應用程式啟動、應用程式內部和應用程式背景追蹤記錄的成效資料,以及應用程式中的所有自訂程式碼追蹤記錄

停用 Performance Monitoring Gradle 外掛程式

您可以使用下列選項新增 instrumentationEnabled 標記來停用 Performance Monitoring 外掛程式:

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

使用擴充功能屬性標記,即可在編譯期間停用特定建構變數的 Performance Monitoring 外掛程式。

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

    使用舊版 Android Gradle 外掛程式時,您還是可以針對特定建構變數停用 Performance Monitoring 外掛程式,但該變數的建構時間貢獻不會完全消除。

  2. 將以下標記新增至模組 (應用程式層級) 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>

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

Firebase 遠端設定可讓您變更應用程式的行為和外觀,因此是可讓您在已部署應用程式執行個體中停用 Performance Monitoring 的理想方式。

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

  1. 確認遠端設定位於模組 (應用程式層級) Gradle 檔案 (通常是 <project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle) 的 dependencies 區段中:

    Kotlin+KTX

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

    Java

      implementation("com.google.firebase:firebase-config:22.0.0")
    
  2. 如果 perf_disable 設為 true,請完成遠端設定並停用 Performance Monitoring:

    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「無作業」(NOOP) 呼叫,從而消除應用程式使用 Performance Monitoring SDK 對應用程式效能造成的任何重大影響。

    如果您將 perf_disable 的值設為 false,Performance Monitoring 會保持啟用狀態。