Skip to content

Instantly share code, notes, and snippets.

@illuzor
Last active April 25, 2024 05:22
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • 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,
]
@HerHde
Copy link

HerHde commented Dec 15, 2018

Thank you very much for this CI conf!
I've tested it successfully with `EMULATOR_VERSION: "24", if you wanna take a look.

@kenton1818
Copy link

thank you so much , help me a lot !!

@kevincianfarini
Copy link

I'm seeing several places where running instrumentation tests in gitlab ci has emulator version 24. Is there any reason for this? The latest version of the emulator is on 29

@illuzor
Copy link
Author

illuzor commented Jun 19, 2019

I'm seeing several places where running instrumentation tests in gitlab ci has emulator version 24. Is there any reason for this? The latest version of the emulator is on 29

Newer version may not start or may have problems with installing the application. But maybe something was changed on the gitlab ci.
You can try any version. Maybe it will work, maybe not.

@DmitriyYakovlev
Copy link

DmitriyYakovlev commented Nov 1, 2019

Note:
this can help
#
- adb shell input tap 233 293

# Send keystroke Arrow Right
- sleep 3; adb shell input keyevent 22
# Send keystroke Arrow Right again
- sleep 3; adb shell input keyevent 22
# Send keystroke Enter to press a button on the dialog
- sleep 3; adb shell input keyevent 66

when happens something like this:
java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not request layout for 10 seconds. If you specified a non default root matcher, it may be picking a root that never takes focus

@MTersigni
Copy link

Just tried and it is actually not working, it is blocked on this step:

$ android-sdk-linux/emulator/emulator -avd test -no-window -no-audio &
$ ./android-wait-for-emulator

@illuzor
Copy link
Author

illuzor commented Dec 10, 2019

@MTersigni I know about this problem. It`s caused by latest android emulator update. I am looking for a solution.

@illuzor
Copy link
Author

illuzor commented Feb 8, 2020

Gitlab ci is really strange. Instrumented tests may work good or not work at all depends on The Moon phase.
So my advice - use Robolectric. It s simple and much more faster than tests on an emulator or device.

@mddrill
Copy link

mddrill commented Jun 25, 2020

This does not seem to work. It fails at android-sdk-linux/tools/bin/sdkmanager --update > update.log with

Warning: Could not create settings
 java.lang.IllegalArgumentException
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings.<init>(SdkManagerCliSettings.java:428)
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings.createSettings(SdkManagerCliSettings.java:152)
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings.createSettings(SdkManagerCliSettings.java:134)
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:57)
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:48)

@paulonteri
Copy link

This does not seem to work. It fails at android-sdk-linux/tools/bin/sdkmanager --update > update.log with

Warning: Could not create settings
 java.lang.IllegalArgumentException
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings.<init>(SdkManagerCliSettings.java:428)
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings.createSettings(SdkManagerCliSettings.java:152)
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings.createSettings(SdkManagerCliSettings.java:134)
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:57)
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:48)

Any solution?

@illuzor
Copy link
Author

illuzor commented Feb 10, 2021

@paulonteri something like this

@edmooliveira29
Copy link

This does not seem to work. It fails at android-sdk-linux/tools/bin/sdkmanager --update > update.log with

Warning: Could not create settings
 java.lang.IllegalArgumentException
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings.<init>(SdkManagerCliSettings.java:428)
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings.createSettings(SdkManagerCliSettings.java:152)
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCliSettings.createSettings(SdkManagerCliSettings.java:134)
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:57)
 	at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:48)

Hi. DO you solved this bug?

@illuzor
Copy link
Author

illuzor commented Jun 11, 2021

@edmooliveira29 the config is updated. Try this new version. Unit tests should work, but not sure about instrumented tests.
Also you can see another config for example.

@adriano-silva
Copy link

@illuzor Are you still able to run it at the current moment?

@illuzor
Copy link
Author

illuzor commented Oct 27, 2021

@adriano-silva no. I only have a config without instrumented tests.

@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