Firebase Performance Monitoring を無効にする

アプリの開発時やテスト時に 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 Gradle プラグインPerformance Monitoring Android ライブラリ、またはその両方を無効にすると、Performance Monitoring がビルドプロセス中に無効になります。

プラグインによるインストルメンテーションは、ビルド時間の増加につながる場合があるため、開発時やデバッグ時はプラグインを無効にすると便利です。ただし、アプリの起動、フォアグラウンド アプリ、バックグラウンド アプリのトレース、およびアプリ内のカスタム コード トレースのパフォーマンス データを確認できるようにライブラリを有効にしておくことも検討してください。

Performance Monitoring Gradle プラグインを無効にする

Performance Monitoring プラグインを無効にするには、次のいずれかのオプションを使用して instrumentationEnabled フラグを追加します。

拡張プロパティ フラグでプラグインを無効にする

拡張プロパティ フラグを使用すると、コンパイル時に特定のビルド バリアントの Performance Monitoring プラグインを無効にすることができます。

  1. ルートレベル(プロジェクト レベル)の Gradle ファイル(<project>/build.gradle.kts または <project>/build.gradle)で、Android Gradle プラグインの依存関係が v3.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>

Remote Config を使用してランタイム時にアプリを無効にする

Firebase Remote Config を使用すると、アプリの動作や外観を変更できるため、デプロイされたアプリのインスタンスで Performance Monitoring を無効にする場合に最適です。

次回 Android アプリが起動したときに Performance Monitoring のデータ収集を無効にするには、下記のサンプルコードを使用します。Android アプリで Remote Config を使用する方法については、Android で Firebase Remote Config を使ってみるをご覧ください。

  1. モジュール(アプリレベル)の Gradle ファイル(通常は <project>/<app-module>/build.gradle.kts または <project>/<app-module>/build.gradle)の dependencies セクションに次の記述を追加して、Remote Config が使用されるようにします。

    Kotlin+KTX

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

    Java

      implementation("com.google.firebase:firebase-config:21.6.3")
    
  2. Remote Config を設定します。perf_disabletrue に設定されている場合は、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 に追加して、Remote Config の値をフェッチして有効にします。

    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 の呼び出しが NoOps(オペレーション レス)の呼び出しとなり、Performance Monitoring SDK の使用によるアプリのパフォーマンス低下がなくなります。

    perf_disable の値を false に設定すると、Performance Monitoring は引き続き有効になります。