Last active
June 22, 2024 13:54
-
-
Save illuzor/988385c493d3f7ed7193a6e3ce001a68 to your computer and use it in GitHub Desktop.
Config for gitlab ci android with unit tests and instrumented tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
] |
@Ridje Thank you very much for your code! Indeed, the unit tests do work now as expected!
Edit: The instrumentation tests run now, too. See the configuration that I used below.
image: openjdk:11-jdk
variables:
ANDROID_COMPILE_SDK: "33"
SDK_TOOLS: "9123335" #latest version from https://developer.android.com/studio/#command-tools
EMULATOR_VERSION: "24"
stages:
- build
- test
before_script:
- apt-get --quiet update --yes
- wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-${SDK_TOOLS}_latest.zip
- unzip -q android-sdk.zip -d android-sdk-linux
- rm android-sdk.zip
- export ANDROID_SDK_ROOT=$PWD/android-sdk-linux
# create a latest folder that will have the updated version of the dependencies
- mkdir $ANDROID_SDK_ROOT/cmdline-tools/tools
# the current hierarchy is android-sdk-linux/cmdline-tools/tools (but the depenencies are not in the tools folder)
# move the necessary dependencies into the tools folder
- mv $ANDROID_SDK_ROOT/cmdline-tools/{lib,bin,source.properties,NOTICE.txt} $ANDROID_SDK_ROOT/cmdline-tools/tools
# create a latest folder that will have the updated version of the dependencies
- mkdir $ANDROID_SDK_ROOT/cmdline-tools/latest
# define PATH for all executable files we will run (skdmanger, avdmanager, abd, emulator)
- export PATH=$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/cmdline-tools/tools/bin:$ANDROID_SDK_ROOT/platform-tools
# sdkmanager is located in the tools/bin folder which we have specified in our PATH $ANDROID_SDK_ROOT/cmdline-tools/tools/bin
- sdkmanager --sdk_root=${ANDROID_SDK_ROOT} --update > update.log
- echo y | sdkmanager --sdk_root=${ANDROID_SDK_ROOT} "platforms;android-${ANDROID_COMPILE_SDK}" "extras;google;m2repository" "extras;android;m2repository" > installPlatform.log
- chmod +x ./gradlew
build-project-job:
stage: build
script:
- ./gradlew assemble
unit-test-job: # This job runs in the test stage.
stage: test
script:
- ./gradlew test
### Experimental AndroidTest execution
android-test-job:
stage: test
script:
- apt-get --quiet update --yes
- apt-get --quiet install --yes libx11-dev libpulse0 libgl1 libnss3 libxcomposite-dev libxcursor1 libasound2
- 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
- sdkmanager --update > update.log
- sdkmanager "platform-tools" "emulator" "system-images;android-${EMULATOR_VERSION};default;armeabi-v7a" > installEmulator.log
# create the emulator
- echo no | avdmanager create avd -n test -k "system-images;android-${EMULATOR_VERSION};default;armeabi-v7a"
# for some reasons the emulator doesnt start automatically, so we have to start it
- adb start-server
# run in the background without window and audio
- android-sdk-linux/emulator/emulator -avd test -no-window -no-audio &
- ./android-wait-for-emulator
- adb shell input keyevent 82
- ./gradlew connectedCheck #or ./gradlew connectedAndroidTest
The config has been updated:
- comments added;
- outdated jdk changed to liberica jdk;
- os changed to the latest ubuntu lts;
- tests artifacts added, now all tests can be seen in gitlab pipeline.
I have a question, this configuration only works for gradle + appium, but if I want to use Webdriverio + appium or RobotFramework + Appium what modifications should I do?
"Waiting for emulator to start" - at some point it freezes on these lines and nothing else happens, does anyone know how to fix it?
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
I am using this right now for unit tests and it works. But I removed instrumented tests as I don't need it.