Running Android emulator tests in GitHub Actions

Share on:

Tests for your Android application can come in a couple different flavors:

  • Unit tests
  • Connected Android tests

You can execute the regular unit tests under the app/src/test directory without any special setup, but for the connected Android tests under app/src/androidTest you'll need a real device or an Android emulator. The connected Android tests can include testing UI interactions, on-device database queries, and more.

If you're using GitHub Actions for your CI, you can run your connected Android tests using an Android Emulator.

In this post we'll use the great GitHub Action ReactiveCircus/android-emulator-runner@v2 for setting up and configuring the emulator, running the connected tests, then shutting down the emulator.

The full source and documentation for the GitHub action is available at https://github.com/ReactiveCircus/android-emulator-runner

Getting started

To get started, write a GitHub Action job YAML file that runs on a MacOS GitHub runner:

1jobs:
2  connected-tests:
3    runs-on: macos-latest

Next, add the steps to check out your code and install the JDK. Finally, add a step for the android-emulator-runner action that will configure the emulator and run your tests:

 1jobs:
 2  connected-tests:
 3    runs-on: macos-latest
 4    
 5    steps:
 6      - name: Checkout
 7        uses: actions/checkout@v3
 8
 9      - name: Set up JDK 11
10        uses: actions/setup-java@v3
11        with:
12          java-version: 11
13          distribution: 'adopt'
14          cache: 'gradle'
15        
16      - name: Run connected tests
17        uses: ReactiveCircus/android-emulator-runner@v2
18        with:
19          api-level: 30
20          target: google_apis
21          arch: x86_64
22          script: ./gradlew connectedCheck

This configuration will set up an emulator with API level 30 and run the /gradlew connectedCheck command to run the connected tests once the emulator is ready.

Newer API levels don't have the default target available, so be sure to specify target: google_apis in the action config.

The full list of available configuration options is available in the action documentation at https://github.com/ReactiveCircus/android-emulator-runner#configurations

Starting the emulator

As the action waits for the Android emulator to start, you'll observe output messages like the below over and over. This is normal, it takes a bit for the emulator to come up and be available.

1/Users/runner/Library/Android/sdk/platform-tools/adb shell getprop sys.boot_completed
2adb: device offline
3The process '/Users/runner/Library/Android/sdk/platform-tools/adb' failed with exit code 1
4/Users/runner/Library/Android/sdk/platform-tools/adb shell getprop sys.boot_completed
5adb: device offline
6The process '/Users/runner/Library/Android/sdk/platform-tools/adb' failed with exit code 1

Recently I've seen it take 3-4 minutes for the emulator to be running and available. Once the emulator is ready, you'll observe an output message similar to:

1/Users/runner/Library/Android/sdk/platform-tools/adb shell getprop sys.boot_completed
2Emulator booted.

Running connected tests

In the action configuration we specified the Gradle command to execute as ./gradlew connectedCheck so the action will run the connectedCheck Gradle task to run our connected tests:

1> Task :app:connectedDebugAndroidTest
2Starting 17 tests on test(AVD) - 11
3
4> Task :app:connectedAndroidTest
5> Task :app:connectedCheck
6
7BUILD SUCCESSFUL in 4m 26s
869 actionable tasks: 22 executed, 47 from cache

Stopping the emulator

And finally the emulator will shut down after the tests finish:

1Terminate Emulator
2  /Users/runner/Library/Android/sdk/platform-tools/adb -s emulator-5554 emu kill
3  OK: killing emulator, bye bye
4  OK

Conclusion

With these steps to use the ReactiveCircus/android-emulator-runner@v2 GitHub Action, hopefully you can execute your valuable connected Android tests as part of your GitHub Actions-based CI setup.

Happy testing!