Gitlab-CI for Android 2021 update
Tags: android, ci, gitlab, sdk
Published: 2021-04-25 05:13:11

---
title: Gitlab-CI for Android 2021 update
published: true
description: Official doc for android GitLab CI is deprecated. this article covers what you should do to use it in 2021 
tags: android, ci, gitlab,sdk
//cover_image: https://direct_url_to_image.jpg
---
##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

```
###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
```
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](https://developer.android.com/studio#downloads) 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](https://developer.android.com/studio/command-line/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
```
Next is another deprecation regarding [Environmental Variables](https://developer.android.com/studio/command-line/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
```