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,
]
@williamobiana
Copy link

williamobiana commented Nov 25, 2022

This is an updated version for the config of gitlab ci android with unit tests and instrumented tests

image: openjdk:11-jdk

variables:
  ANDROID_COMPILE_SDK: "30"
  ANDROID_BUILD_TOOLS: "30.0.3"
  SDK_TOOLS: "8512546" # modify with latest version from https://developer.android.com/studio/#command-tools
  EMULATOR_VERSION: "24"
  
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

  # The hierarchy of the files have been changed, so you will have to re-arrange it

  # ANDROID_HOME is deprecated, we will use ANDROID_SDK_ROOT
  - export ANDROID_SDK_ROOT=$PWD/android-sdk-linux

  # 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:$ANDROID_SDK_ROOT/emulator

  # 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}" "build-tools;${ANDROID_BUILD_TOOLS}" "extras;google;m2repository" "extras;android;m2repository" > installPlatform.log

  - chmod +x ./gradlew  #if you are using a working directory, cd into it before running the gradlew command

stages:
  - test
  
unitTests:
  stage: test
  script:
    - ./gradlew test

instrumentedTests:
  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
    - abd start-server
    
    # run in the background without window and audio
    - emulator -avd test -no-window -no-audio &
    - ./android-wait-for-emulator
    - adb shell input keyevent 82
    - ./gradlew connectedCheck      #or ./gradlew connectedAndroidTest

@illuzor
Copy link
Author

illuzor commented Nov 26, 2022

@williamobiana thank you

@Ridje
Copy link

Ridje commented Dec 8, 2022

 - echo y | sdkmanager --sdk_root=${NDROID_SDK_ROOT} "platforms;android-${ANDROID_COMPILE_SDK}" "build-tools;${ANDROID_BUILD_TOOLS}" "extras;google;m2repository" "extras;android;m2repository" > installPlatform.log

@williamobiana I suppose there should be ANDROID_SDK_ROOT instead of NDROID_SDK_ROOT

@williamobiana
Copy link

williamobiana commented Dec 8, 2022

Yes @Ridje , Typo has been corrected. Thanks

@Alexminator99
Copy link

Im getting this:

Warning: Failed to find package 'system-images;android-32;default;armeabi-v7a'

In gitlab... How can I solve it?

@BenjyTec
Copy link

For me, the pipeline fails, too. This is the error:
mv: target '/builds/BenjyTec/issues-radar/android-sdk-linux/cmdline-tools/tools' is not a directory
I am using the CLI version 9123335, buildTools version 33.0.1 and android compile SDK 33.
I was getting the same error when using the file exactly as it was provided, without any changes.

@illuzor
Copy link
Author

illuzor commented Jan 17, 2023

@BenjyTec looks like folder with tools was changed.

@Ridje
Copy link

Ridje commented Jan 23, 2023

For me, the pipeline fails, too. This is the error: mv: target '/builds/BenjyTec/issues-radar/android-sdk-linux/cmdline-tools/tools' is not a directory I am using the CLI version 9123335, buildTools version 33.0.1 and android compile SDK 33. I was getting the same error when using the file exactly as it was provided, without any changes.

image: openjdk:11-jdk

variables:
  ANDROID_COMPILE_SDK: "31"
  SDK_TOOLS: "9123335" #latest version from https://developer.android.com/studio/#command-tools

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
  only:
    - merge_requests
  script:
    - ./gradlew assemble

unit-test-job: # This job runs in the test stage.
  stage: test
  only:
    - merge_requests
  script:
    - ./gradlew test

I am using this right now for unit tests and it works. But I removed instrumented tests as I don't need it.

@BenjyTec
Copy link

BenjyTec commented Jan 23, 2023

@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

@illuzor
Copy link
Author

illuzor commented Feb 13, 2023

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.

@c0v3r
Copy link

c0v3r commented Jun 9, 2023

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?

@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