Skip to content

Instantly share code, notes, and snippets.

@illuzor
Last active June 22, 2024 13:54
Show Gist options
  • Save illuzor/988385c493d3f7ed7193a6e3ce001a68 to your computer and use it in GitHub Desktop.
Save illuzor/988385c493d3f7ed7193a6e3ce001a68 to your computer and use it in GitHub Desktop.
Config for gitlab ci android with unit tests and instrumented tests
image: ubuntu:22.04
variables:
ANDROID_COMPILE_SDK: "33"
ANDROID_BUILD_TOOLS: "33.0.2"
EMULATOR_IMAGE: "24"
SDK_TOOLS: "9477386" # from https://developer.android.com/studio/#command-tools
before_script:
# install required packages
- apt-get update --yes
- apt-get install wget gnupg gnupg2 unzip --yes
# install liberica jdk 11 https://bell-sw.com/libericajdk/
- wget -q -O - https://download.bell-sw.com/pki/GPG-KEY-bellsoft | apt-key add -
- echo "deb [arch=amd64] https://apt.bell-sw.com/ stable main" | tee /etc/apt/sources.list.d/bellsoft.list
- apt-get --quiet update --yes
- apt-get install bellsoft-java11-lite --yes
- java -version
# download and unzip android sdk
- wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-${SDK_TOOLS}_latest.zip
- echo A | unzip -q android-sdk.zip -d android-sdk-linux
- rm android-sdk.zip
# export gradle home path
- export GRADLE_USER_HOME=$PWD/.gradle
# export android sdk path
- export ANDROID_SDK_ROOT=$PWD/android-sdk-linux
# export android sdk executables paths
- export PATH=$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin/
- export PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools/
- export PATH=$PATH:$ANDROID_SDK_ROOT/emulator/
# hack with moving cmdline-tools to cmdline-tools/latest, no idea how to install directly
- mkdir $ANDROID_SDK_ROOT/cmdline-tools/latest
- mv $ANDROID_SDK_ROOT/cmdline-tools/{lib,bin,source.properties,NOTICE.txt} $ANDROID_SDK_ROOT/cmdline-tools/latest
# update and install common android sdk components
- sdkmanager --sdk_root=${ANDROID_SDK_ROOT} --update
- echo y | sdkmanager --sdk_root=${ANDROID_SDK_ROOT} "platforms;android-${ANDROID_COMPILE_SDK}" "build-tools;${ANDROID_BUILD_TOOLS}"
- chmod +x ./gradlew
stages:
- test
unit tests:
stage: test
script:
- ./gradlew testDebugUnitTest
artifacts:
when: always
reports:
junit: [
./**/build/test-results/testDebugUnitTest/TEST-*.xml,
]
instrumented tests:
stage: test
script:
# install packages needed by emulator
- apt-get install libx11-dev libpulse0 libgl1 libnss3 libxcomposite-dev libxcursor1 libasound2 --yes
# install android sdk components and emulator
- sdkmanager --sdk_root=${ANDROID_SDK_ROOT} "platform-tools" "emulator" "system-images;android-${EMULATOR_IMAGE};default;armeabi-v7a"
# download script for emulator waiting
- wget --quiet --output-document=android-wait-for-emulator https://raw.githubusercontent.com/travis-ci/travis-cookbooks/0f497eb71291b52a703143c5cd63a217c8766dc9/community-cookbooks/android-sdk/files/default/android-wait-for-emulator
- chmod +x android-wait-for-emulator
# create virtual device named "test"
- echo no | avdmanager -v create avd -n test -k "system-images;android-${EMULATOR_IMAGE};default;armeabi-v7a"
# start adb server
- adb start-server
# run emulator and tests
- emulator -avd test -no-boot-anim -no-window -no-audio -no-snapshot &
- ./android-wait-for-emulator
- adb shell input keyevent 82
- ./gradlew connectedDebugAndroidTest
artifacts:
when: always
reports:
junit: [
./**/build/outputs/androidTest-results/connected/TEST-*.xml,
]
@koen08
Copy link

koen08 commented Sep 29, 2023

"Waiting for emulator to start" - at some point it freezes on these lines and nothing else happens, does anyone know how to fix it?

@ernestguevara
Copy link

The last armeabi-v7a was for system image 25, after that only viable option was arm64-v8a . Anyone tried system image 26 and up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment