GitLab comes with a great tool called GitLab CI/CD. It provides a way to solve many problems and issues that developers face in day-to-day life, such as manually running unit tests, lint check and generating builds with different variants, and verifying code changes that may break your unit test or your entire build. Fortunately, GitLab CI/CD helps us to do all these manual tasks without worrying too much.
This is an ongoing series. Please have a look at the next stories related to gitlab and CI for more information.
Part 2:
Part 3:
We are what we repeatedly do. Excellence, then, is not an act, but a habit. Try out Justly and start building your habits today!
Roll up your sleeves, we’ll get our hands dirty!. We’ll need to configure the environment and download and install a few packages to run SDK.
Let’s have quick look at Docker Image
// Dockerfile
FROM openjdk:8-jdk
RUN apt-get --quiet update --yes && \
apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1 curl && \
# download Android SDK tools
wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip && \
unzip android-sdk.zip -d /android_sdk && \
export ANDROID_HOME=/android_sdk && \
export PATH=$PATH:$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin && \
# install Android SDK & dependencies
echo y | sdkmanager "platform-tools" "platforms;android-26" "build-tools;26.0.2" && \
echo y | sdkmanager "extras;android;m2repository" && \
echo y | sdkmanager "extras;google;google_play_services" && \
echo y | sdkmanager "extras;google;m2repository" && \
# install pip for grip export README.md to html
echo y | apt-get update && \
echo y | apt-get install python-pip && \
pip install grip
FROM indicates the base image. To build an Android application, we’re going to use openjdk:8-jdk.
Then download Android SDK tools and use the sdkmanagaer to download the required SDK. The version of SDK or library depends on the need of the application.
Now, Let’s add a Gitlab CI configuration file .gitlab-ci.yml
inside our Android project root to execute all unit tests.
Android project root to execute all unit tests.
image: your-docker-image-name
stages:
- test
cache:
paths:
- .gradle/wrapper
- .gradle/caches
testDebug:
stage: test
before_script:
- chmod +x ./gradlew
- export GRADLE_USER_HOME=$PWD/.gradle
script:
- ./gradlew test
Image: Docker image to run CI jobs.
Cache: Add cache to our builds, each cached job will download and extract the cache at the beginning and upload the cache at the end.
Stages: Define the order of our jobs. The job of the same stage run in parallel.
before_script: Used to set up stuff before the actual work is started. In our case, we set GRADLE_USER_HOME
to avoid using the complete path to Gradle and make the gradlew executable.
Script: The shell script executed by the Gitlab Runner. Here we run our all unit tests.
stages:
- test
cache:
paths:
- .gradle/wrapper
- .gradle/caches
lintDebug:
stage: test
before_script:
- chmod +x ./gradlew
- export GRADLE_USER_HOME=$PWD/.gradle
script:
- ./gradlew lintDebug
Let’s add one more stage to build debug apk for manual testing.
stages:
- build
build_assembleDebug:
stage: build
before_script:
- chmod +x ./gradlew
- export GRADLE_USER_HOME=$PWD/.gradle
script:
- ./gradlew assembleDebug
- mv app/build/outputs/apk/debug/*.apk . //It will move //generated apk to the root directory
artifacts:
expire_in: 1 month
paths:
- MyApplicatoion*.apk
artifacts: defines the jobs file and directories attached to job after execution complete.
expire_in: Indicates the lifetime of artifacts.
Let’s put it all together
image: jangrewe/gitlab-ci-android
stages:
- test
- build
cache:
paths:
- .gradle/wrapper
- .gradle/caches
before_script:
- chmod +x ./gradlew
- export GRADLE_USER_HOME=$PWD/.gradle
testDebug:
stage: test
script:
- ./gradlew test
lintDebug:
stage: test
script:
- ./gradlew lintDebug
build_assembleDebug:
stage: build
script:
- ./gradlew assembleDebug
- mv app/build/outputs/apk/debug/*.apk . //It will move //generated apk to the root directory
artifacts:
expire_in: 1 month
paths:
- MyApplicatoion*.apk
Now, it's time to push some code and check your pipelines. It looks something like this.
We completed a short introduction to set up CI to run unit test, lint check and building debug apk. Many more things we can do by using CI such as slack integration, auto-deploy on play store, upload on dropbox, etc…
Thanks for your support!
Happy coding!!
Whether you need...