DEV Community

Javad Arjmandi
Javad Arjmandi

Posted on

Gitlab-CI for Android 2021 update

TL;DR at the bottom

I spent a few hours trying to set up Gitlab CI for our new project, only to be faced with a few errors when updating the version of SDK tools. Turns out the official documentation for Gitlab CI for android is outdated and still thinks it's 2018. So after doing a few trial and error and reading different StackOverflow threads, I tried updating the Gitlab CI documentation. Here's the outdated version from 2018:

image: openjdk:8-jdk

variables:
  ANDROID_COMPILE_SDK: "28"
  ANDROID_BUILD_TOOLS: "28.0.2"
  ANDROID_SDK_TOOLS:   "4333796"

before_script:
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
  - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-${ANDROID_SDK_TOOLS}.zip
  - unzip -d android-sdk-linux android-sdk.zip
  - echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null
  - echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" >/dev/null
  - echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
  - export ANDROID_HOME=$PWD/android-sdk-linux
  - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
  - chmod +x ./gradlew
  # temporarily disable checking for EPIPE error and use yes to accept all licenses
  - set +o pipefail
  - yes | android-sdk-linux/tools/bin/sdkmanager --licenses
  - set -o pipefail

stages:
  - build
  - test

lintDebug:
  stage: build
  script:
    - ./gradlew -Pci --console=plain :app:lintDebug -PbuildDir=lint

assembleDebug:
  stage: build
  script:
    - ./gradlew assembleDebug
  artifacts:
    paths:
    - app/build/outputs/

debugTests:
  stage: test
  script:
    - ./gradlew -Pci --console=plain :app:testDebug

Enter fullscreen mode Exit fullscreen mode

So what's changed?

First off, ANDROID_BUILD_TOOLS and ANDROID_COMPILE_SDK should be updated to "30.0.3" and "30" respectively until the new version is released.
The tricky part however is that sdk-tools-linux is deprecated so the number from 2018 is as high as it gets you and it should be replaced with commandlinetools. So on our before_script, we gotta change the URL to this:

https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}.zip
Enter fullscreen mode Exit fullscreen mode

And then change the ANDROID_SDK_TOOLS to 6858069_latest until a new version is out. you can find the number for future versions here and navigating to Command line tools only section.

What comes next is that since the folder hierarchy for the new commandlinetools is different from the old one, we need to add a flag to each sdkmanager command, and also change every path from android-sdk-linux/tools/bin to android-sdk-linux/cmdline-tools/bin so it should be like this:

- echo y | android-sdk-linux/cmdline-tools/bin/sdkmanager --sdk_root=. "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null
- echo y | android-sdk-linux/cmdline-tools/bin/sdkmanager --sdk_root=. "platform-tools" >/dev/null
- echo y | android-sdk-linux/cmdline-tools/bin/sdkmanager --sdk_root=. "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
...
 - yes | android-sdk-linux/cmdline-tools/bin/sdkmanager --sdk_root=. --licenses
Enter fullscreen mode Exit fullscreen mode

Next is another deprecation regarding Environmental Variables hence we should change ANDROID_HOME to ANDROID_SDK_ROOT

TL;DR

the updated version would be this:

image: openjdk:8-jdk


variables:
ANDROID_COMPILE_SDK: "30"
ANDROID_BUILD_TOOLS: "30.0.3"
ANDROID_SDK_TOOLS: "6858069_latest"


before_script:
- apt-get --quiet update --yes
- apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
- wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}.zip
- unzip -d android-sdk-linux android-sdk.zip
- echo y | android-sdk-linux/cmdline-tools/bin/sdkmanager --sdk_root=. "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null
- echo y | android-sdk-linux/cmdline-tools/bin/sdkmanager --sdk_root=. "platform-tools" >/dev/null
- echo y | android-sdk-linux/cmdline-tools/bin/sdkmanager --sdk_root=. "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
- export ANDROID_SDK_ROOT=$PWD
- export PATH=$PATH:$PWD/platform-tools/
- chmod +x ./gradlew
# temporarily disable checking for EPIPE error and use yes to accept all licenses
- set +o pipefail
- yes | android-sdk-linux/cmdline-tools/bin/sdkmanager --sdk_root=. --licenses
- set -o pipefail


stages:
- build
- test


lintDebug:
stage: build
script:
- ./gradlew -Pci --console=plain :app:lintDebug -PbuildDir=lint


assembleDebug:
stage: build
script:
- ./gradlew assembleDebug
artifacts:
paths:
- app/build/outputs/


debugTests:
stage: test
script:
- ./gradlew -Pci --console=plain :app:testDebug
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
seanghay profile image
Seanghay

Thanks for sharing

Collapse
 
johnjacobkenny profile image
Kenny John Jacob

Saved a couple hours for me today, thank you so much!

Collapse
 
vivekweb2013 profile image
Vivek

Thanks for writing this article. It really helped me upgrade the old pipeline to comply with new android version