android-emulator-runner のような Android Emulator の Actions を使うパターンもあるのですが、今回は Gradle Managed Device を使っての Baseline Profiles を生成する Actions を作ってみます。
実装
dependencies {
implementation 'androidx.benchmark:benchmark-macro-junit4:1.1.1'
...
}
benchmark-macro-junit4 は 1.2.0 の alpha 版を使用します。 1.1.0 から入っていました。
これは benchmark モジュールのテスト実行時に Baseline Profiles の生成以外のテストが走ってしまうのを防ぐ機能が 1.2.0 1.1.0-rc02 以降に入っているためです。
android {
...
testOptions {
managedDevices {
devices {
pixel2Api31(com.android.build.api.dsl.ManagedVirtualDevice) {
device = "Pixel 2"
apiLevel = 31
systemImageSource = "aosp"
}
}
}
}
}
benchmark モジュールに Gradle Managed Device の設定を追加します。
name: Generate Baseline Profiles
on: workflow_dispatch
jobs:
generate-baseline-profiles:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: Setup Android SDK
uses: android-actions/setup-android@v2
- name: set up JDK
uses: actions/setup-java@v3
with:
java-version: 11
distribution: zulu
cache: gradle
- name: Generate baseline profile
run: |
./gradlew :benchmark:cleanManagedDevices --unused-only
./gradlew :benchmark:pixel2Api31BenchmarkAndroidTest -Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect" -Pandroid.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=BaselineProfile
mv benchmark/build/outputs/managed_device_android_test_additional_output/pixel2Api31/BaselineProfileGenerator_generate-baseline-prof.txt app/src/main/baseline-prof.txt
- name: Create pull request
uses: peter-evans/create-pull-request@v4
with:
commit-message: Updated baseline profiles
title: Updated baseline profiles
body: ""
branch: update-baseline-profiles
解説
./gradlew :benchmark:pixel2Api31BenchmarkAndroidTest -Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect" -Pandroid.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=BaselineProfile
で Baseline Profiles を生成するテストを Gradle Managed Device で動かします。
Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect"
の設定は、GitHub Actions などのハードウェアレンダリングをサポートしていない環境で Gradle Managed Device を使うために必要な設定です。
Pandroid.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=BaselineProfile
の設定は、上記で話に出したbenchmark-macro-junit4
の 1.1.0 以降に追加された benchmark モジュールのテスト実行時に Baseline Profiles の生成だけをフィルタリングして動かすための設定です。
上記の例では Baseline Profiles を生成して PR にする Actions になりますが、Baseline Profiles のファイルサイズは大きくほぼ毎回差分が発生するものになります。
そのためアプリのリリースのワークフローにのみ Baseline Profiles の生成を組み込んで git 上で管理させない手段も取ることも考えるといいでしょう。