Run Android instrumentation unit tests with GitLab CI + Docker

An instrumentation unit test is a unit test that is executed on an actual machine or emulator. For this test, you can take advantage of the Android framework API and supporting APIs such as AndroidX Test. [^ 1] This article describes how to run unit tests on the Android emulator on Docker without using the actual device. Unit tests are run automatically by GitLab CI.

What to use & version confirmed to work

Advance preparation

  1. Install Docker on Ubuntu PC
  2. Allow non-root users to run Docker
    Steps: https://docs.docker.com/engine/install/linux-postinstall/
  3. Install GitLab Runner on Ubuntu PC
  4. Register GitLab Runner with GitLab
    You can check the token entered with Enter the registration token: in the GitLab admin area http: // <GitLab address>/admin/runners. ..
    Enter docker inEnter an executor:

Run on Android emulator on Docker

Use Android Emulator Container Scripts.

Start Docker container

docker run \
  -d \
  --restart=always \
  -e ADBKEY="$(cat ~/.android/adbkey)" \
  -e EMULATOR_PARAMS="-wipe-data" \
  --device /dev/kvm \
  --publish 8554:8554/tcp \
  --publish 5555:5555/tcp \
  --name android-emulator \
  us-docker.pkg.dev/android-emulator-268719/images/30-google-x64:30.1.2

By specifying the option --restart = always, the Android emulator will be started automatically in the following cases. [^ 2]

By specifying the environment variable EMULATOR_PARAMS ="-wipe-data ", the user data on the emulator will be deleted when the Android emulator is started. [^ 3] For example, exiting the Android emulator with the adb reboot -p command will launch the emulator with user data removed. Repeating unit tests may leave unnecessary files in shared storage, etc. They will be erased. Of course, it should be a unit test that doesn't leave such files so that it doesn't affect other unit test results.

Delete Docker container

The steps above will always launch the Android emulator on your Docker container. When you no longer need the Android emulator, delete the container with the following command.

docker rm -f android-emulator

Set up CI/CD for your Android project on GitLab

Create a .gitlab-ci.yml file in the root directory of your repository.

yml:.gitlab-ci.yml


image: androidsdk/android-30:latest

cache:
  paths:
    - .gradle/wrapper
    - .gradle/caches

before_script:
  - export GRADLE_USER_HOME="${PWD}/.gradle"
  - chmod +x ./gradlew

stages:
  - check
  - connectedCheck

check:
  interruptible: true
  stage: check
  script:
    - ./gradlew -Pci --console=plain assembleDebug lintDebug testDebugUnitTest
  artifacts:
    expire_in: 1 week
    paths:
      - app/build/outputs/
      - app/build/reports/

connectedCheck:
  interruptible: true
  stage: connectedCheck
  before_script:
    - adb connect 172.17.0.1:5555
  script:
    - ./gradlew -Pci --console=plain connectedDebugAndroidTest
  artifacts:
    expire_in: 1 week
    paths:
    - app/build/reports/androidTests/

172.17.0.1 is the IP address of the host PC as seen from the Docker container. This may vary depending on your environment and Docker network settings. For Docker Desktop for Mac and Docker Desktop for Windows You can also access it with host.docker.internal instead of the IP address. [^ 4]

Recommended Posts

Run Android instrumentation unit tests with GitLab CI + Docker
Run Pico with docker
Run Payara with Docker
Run TAO Core with Docker
Set up GitLab with docker
Run Rails whenever with docker
Java unit tests with Mockito
Run lambda with custom docker image
Run SQL Server with Docker ToolBox
When building docker image with GitLab CI, wait until docker daemon starts
Make JupyterLab run anywhere with docker
Communicate Gitlab and Gitlab Runner launched with Docker
Run Ubuntu + ROS with Docker on Mac
How to run Blazor (C #) with Docker
Run JSP Hello World with Tomcat on Docker
Create jupyter notebook with Docker and run ruby