Android 向け Performance Monitoring を使ってみる

始める前に

まだ追加していない場合は、Firebase を Android プロジェクトに追加します

ステップ 1: アプリに Performance Monitoring SDK を追加する

Performance Monitoring SDK を追加すると、Firebase はアプリの画面レンダリングとアプリのライフサイクルに関するデータ(アプリの起動時間など)のデータ収集を自動的に開始します。Firebase でネットワーク リクエストをモニタリングできるようにするには、Performance Monitoring Gradle プラグインも追加する必要があります(次のステップ)。

  1. モジュール(アプリレベル)の Gradle ファイル(通常は <project>/<app-module>/build.gradle.kts または <project>/<app-module>/build.gradle)に、Android 用 Performance Monitoring ライブラリの依存関係を追加します。ライブラリのバージョニングの制御には、Firebase Android 部品構成表(BoM)を使用することをおすすめします。

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.8.0"))
    
        // Add the dependency for the Performance Monitoring library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-perf")
    }
    

    Firebase Android 部品構成表を使用すると、アプリは常に互換性のあるバージョンの Firebase Android ライブラリを使用します。

    (代替方法)BoM を使用せずに Firebase ライブラリの依存関係を追加する

    Firebase BoM を使用しない場合は、依存関係の行でそれぞれの Firebase ライブラリのバージョンを指定する必要があります。

    アプリで複数の Firebase ライブラリを使用する場合は、すべてのバージョンの互換性を確保するため、BoM を使用してライブラリのバージョンを管理することを強くおすすめします。

    dependencies {
        // Add the dependency for the Performance Monitoring library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-perf:20.5.2")
    }
    
    Kotlin 固有のライブラリ モジュールをお探しの場合は、2023 年 10 月(Firebase BoM 32.5.0)以降は、Kotlin と Java のどちらのデベロッパーもメイン ライブラリ モジュールを利用できるようになります(詳しくは、このイニシアチブに関するよくある質問をご覧ください)。

  2. アプリを再コンパイルします。

ステップ 2: アプリに Performance Monitoring Gradle プラグインを追加する

Performance Monitoring Gradle プラグインを追加すると、Firebase は HTTP/S ネットワーク リクエストのデータ収集を自動的に開始します。このプラグインでは、@AddTrace アノテーションを使用してカスタムコード トレースを計測可能にすることもできます。

  1. ルートレベル(プロジェクト レベル)の Gradle ファイル(<project>/build.gradle.kts または <project>/build.gradle)に、Performance Monitoring Gradle プラグインを追加します。

    Kotlin

    plugins {
        // To benefit from the latest Performance Monitoring plugin features,
        // update your Android Gradle plugin dependency to at least v3.4.0
        id("com.android.application") version "7.3.0" apply false
    
        // Make sure that you have the Google services Gradle plugin dependency
        id("com.google.gms.google-services") version "4.4.1" apply false
    
        // Add the dependency for the Performance Monitoring Gradle plugin
        id("com.google.firebase.firebase-perf") version "1.4.2" apply false
    }
    

    Groovy

    plugins {
        // To benefit from the latest Performance Monitoring plugin features,
        // update your Android Gradle plugin dependency to at least v3.4.0
        id 'com.android.application' version '7.3.0' apply false
    
        // Make sure that you have the Google services Gradle plugin dependency
        id 'com.google.gms.google-services' version '4.4.1' apply false
    
        // Add the dependency for the Performance Monitoring Gradle plugin
        id 'com.google.firebase.firebase-perf' version '1.4.2' apply false
    }
    
  2. モジュール(アプリレベル)の Gradle ファイル(通常は <project>/<app-module>/build.gradle.kts または <project>/<app-module>/build.gradle)に Performance Monitoring Gradle プラグインを追加します。

    Kotlin

    plugins {
        id("com.android.application")
    
        // Make sure that you have the Google services Gradle plugin
        id("com.google.gms.google-services")
    
        // Add the Performance Monitoring Gradle plugin
        id("com.google.firebase.firebase-perf")
        ...
    }
    

    Groovy

    plugins {
        id 'com.android.application'
    
        // Make sure that you have the Google services Gradle plugin
        id 'com.google.gms.google-services'
    
        // Add the Performance Monitoring Gradle plugin
        id 'com.google.firebase.firebase-perf'
        ...
    }
    
  3. アプリを再コンパイルします。

ステップ 3: 初期データ表示用のパフォーマンス イベントを生成する

SDK がアプリに正常に追加されると、Firebase によりイベントの処理が開始されます。引き続きローカルで開発を行っている場合は、アプリを操作して、初期データの収集と処理用のイベントを生成します。

  1. アプリをバックグラウンドとフォアグラウンドの間で数回切り替えたり、画面間を移動してアプリを操作したり、ネットワーク リクエストをトリガーしたりして、イベントを生成します。

  2. Firebase コンソールのパフォーマンス ダッシュボードに移動します。初期データが数分以内に表示されます。

    初期データが表示されない場合は、トラブルシューティングのヒントをご覧ください。

ステップ 4:(省略可)パフォーマンス イベントのログメッセージを表示する

  1. ビルド時に Performance Monitoring のデバッグ ロギングを有効にするには、次のようにアプリの AndroidManifest.xml ファイルに <meta-data> 要素を追加します。

    <application>
        <meta-data
          android:name="firebase_performance_logcat_enabled"
          android:value="true" />
    </application>
    
  2. ログ メッセージにエラー メッセージがないか確認します。

  3. Performance Monitoring は、ログ メッセージに FirebasePerformance のタグを付けます。logcat フィルタリングを使用すると、次のコマンドを実行して、所要時間トレースと HTTP/S ネットワーク リクエストのロギングを表示できます。

    adb logcat -s FirebasePerformance
  4. Performance Monitoring がパフォーマンス イベントをロギングしていることを示す次の種類のログを確認します。

    • Logging trace metric: TRACE_NAME, FIREBASE_PERFORMANCE_CONSOLE_URL
    • Logging network request trace: URL
  5. URL をクリックして Firebase コンソールでデータを表示します。ダッシュボードでデータが更新されるまでに少し時間がかかることがあります。

アプリがパフォーマンス イベントをロギングしていない場合は、トラブルシューティングのヒントをご覧ください。

ステップ 5: (省略可)特定のコードにカスタム モニタリングを追加する

アプリの特定のコードに関連付けられたパフォーマンス データをモニタリングするには、カスタムコード トレースを計測可能にします。

カスタム コード トレースを使用すると、一連の画像の読み込みやデータベースのクエリなど、アプリが特定のタスクや一連のタスクを完了するのにかかる時間を測定できます。カスタムコード トレースのデフォルトの指標は期間ですが、キャッシュ ヒットやメモリ警告などのカスタム指標を追加することもできます。

コードで、Performance Monitoring SDK に用意された API を使用して、カスタムコード トレースの開始と終了を定義(および必要なカスタム指標を追加)します。Android アプリの場合は、@AddTrace アノテーションを使用して特定のメソッドの期間をモニタリングすることもできます。

これらの機能の詳細とアプリに追加する方法については、特定のコードにモニタリングを追加するをご覧ください。

ステップ 6: アプリをデプロイして結果を確認する

1 つ以上のテストデバイスを使用して Performance Monitoring を検証したら、アプリのアップデート バージョンをユーザーにデプロイします。

Firebase コンソールのパフォーマンス ダッシュボードでパフォーマンス データをモニタリングできます。

既知の問題

  • Performance Monitoring Gradle プラグイン v1.1.0 で、Guava の依存関係が一致せず、次のエラーが発生する可能性があります。

    Error:Execution failed for task ':app:packageInstantRunResourcesDebug'.
    > com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;

    このエラーが発生した場合は、次のいずれかを行います。

    • Performance Monitoring プラグインを v1.1.1 以降にアップグレードする(最新バージョンは v1.4.2)。

    • ルートレベル(プロジェクト レベル)の Gradle ファイル(<project>/build.gradle.kts または <project>/build.gradle)で、Performance Monitoring プラグインの依存関係を記述している行を次のように置き換える。

      Kotlin

      buildscript {
        // ...
      
        dependencies {
          // ...
      
          // Replace the standard Performance Monitoring plugin dependency line, as follows:
          classpath("com.google.firebase:perf-plugin:1.1.0") {
              exclude(group = "com.google.guava", module = "guava-jdk5")
          }
        }
      }
      

      Groovy

      buildscript {
        // ...
      
        dependencies {
          // ...
      
          // Replace the standard Performance Monitoring plugin dependency line, as follows:
          classpath('com.google.firebase:perf-plugin:1.1.0') {
              exclude group: 'com.google.guava', module: 'guava-jdk5'
          }
        }
      }
      
  • Performance Monitoring は、HTTP content-length ヘッダーの値に基づいて HTTP ネットワーク リクエストのペイロード サイズの合計を報告します。この値は正確とは限りません。

  • Performance Monitoring は、マルチプロセスの Android アプリのメインプロセスのみをサポートしています。

次のステップ