Disable Firebase Performance Monitoring

During app development and testing, you might find it useful to disable Performance Monitoring.

For example, by disabling Performance Monitoring during your app build process, you can:

  • Disable certain functionalities of Performance Monitoring (such as those provided by the Performance Monitoring Gradle plugin) in your debug builds, but re-enable the functionalities for your release build.

  • Disable Performance Monitoring when building your app, but allow your app to re-enable it at runtime.

  • Disable Performance Monitoring when building your app, and do not allow your app to re-enable it at runtime.

You can also build your app with Performance Monitoring enabled, but use Firebase Remote Config to give you flexibility to disable (and re-enable) Performance Monitoring in your production app. With this option, you can even configure your app to let users opt-in or opt-out of using Performance Monitoring.

Disable Performance Monitoring during your app build process

You can disable Performance Monitoring during your build process by disabling the Performance Monitoring Gradle plugin and/or by disabling the Performance Monitoring Android library.

During development and debugging, disabling the plugin is useful because instrumentation by the plugin can contribute to increased build time. You might, though, consider keeping the library enabled so that you can still view performance data from app start, app-in-foreground, and app-in-background traces as well as any custom code traces in your app.

Disable the Performance Monitoring Gradle Plugin

You can disable the Performance Monitoring plugin by adding an instrumentationEnabled flag using the following options:

Disable the plugin via an Extension Property flag

By using an Extensions Property flag, you can disable the Performance Monitoring plugin for a specific build variant at compile time.

  1. In your root-level (project-level) Gradle file (<project>/build.gradle.kts or <project>/build.gradle), make sure your Android Gradle Plugin dependency is specified as v3.4.0 or later.

    For earlier versions of the Android Gradle Plugin, you can still disable the Performance Monitoring plugin for a specific build variant, but the build time contribution for that variant won’t be completely eliminated.

  2. Add the following flag to your module (app-level) Gradle file (usually <project>/<app-module>/build.gradle.kts or <project>/<app-module>/build.gradle), then set it to false to disable the Performance Monitoring plugin.

    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
          }
        }
      }
    }
    

Disable the plugin via a Project Property flag

By using a Project Property flag, you can disable the Performance Monitoring plugin for all build variants at compile time.

Add the following flag to your gradle.properties file, then set it to false to disable the Performance Monitoring plugin.

// ...

// 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

Disable the Performance Monitoring Android library

If you disable the Performance Monitoring library at compile time, you can choose whether to allow your app to enable the library at runtime.

Disable the library at compile time, but allow your app to enable it at runtime

Add the following <meta-data> element to your app’s AndroidManifest.xml file:

  <application>
    <meta-data
      android:name="firebase_performance_collection_enabled"
      android:value="false" />
  </application>

Disable the library at compile time, but do not allow your app to enable it at runtime

Add the following <meta-data> element to your app’s AndroidManifest.xml file:

  <application>
    <meta-data
      android:name="firebase_performance_collection_deactivated"
      android:value="true" />
  </application>

Disable your app at runtime using Remote Config

Firebase Remote Config lets you make changes to the behavior and appearance of your app, so it provides an ideal way to let you disable Performance Monitoring in deployed instances of your app.

To disable Performance Monitoring data collection the next time that your Android app starts, use the example code shown below. For more information about using Remote Config in an Android app, see Use Firebase Remote Config on Android.

  1. Ensure that Remote Config is in the dependencies section of your module (app-level) Gradle file (usually <project>/<app-module>/build.gradle.kts or <project>/<app-module>/build.gradle):

    Kotlin+KTX

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

    Java

      implementation("com.google.firebase:firebase-config:21.6.3")
    
  2. Set up Remote Config and disable Performance Monitoring if perf_disable is set to 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. Add the following code to MainActivity.java to fetch and activate Remote Config values:

    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. To disable Performance Monitoring in the Firebase console, create a perf_disable parameter in your app's project, then set its value to true.

    This change will make calls to the Performance Monitoring SDK "no operation" calls (NOOPs), eliminating any significant effects on app performance from using the Performance Monitoring SDK in your app.

    If you set the value of perf_disable to false, Performance Monitoring remains enabled.