ANDROID CI WITH JENKINS, DOCKER and KITEMATIC STEP BY STEP.

Denys Mokhrin
TEMY
Published in
10 min readDec 13, 2018

This guide is for complete beginners and for those who wants to get acquainted with the CI process setup from the scratch.

For those who don’t want to dive into details please get already baked container by xmartlabs.

TABLE OF CONTENTS

INTRODUCTION

“Continuous integration systems let you automatically build and test your app every time you check in updates to your source control system. You can use any continuous integration tool that can initiate a Gradle build to build your Android Studio projects.” — Google docs

Why Docker?

  • Easy installation and setup of a complete Docker development environment for the macOS/Windows.
  • Integrated Docker platform and tools Docker command line, Docker Compose, and Docker Notary command line.
  • Automatic updates with channels for monthly Edge and quarterly Stable versions of Docker.
  • Fast and reliable performance with native macOS/Windows virtualization running a custom minimal Linux distro.
  • Seamless volume mounting for code and data, including file change notifications that unlock fast edit-test cycles.
  • Supports Native macOS/Windows security with sandbox security model.
  • Enterprise network support allows Docker for macOS/Windows to with VPNs and proxies.
  • In container development and debugging with supported IDEs.

Docker docs

Why Jenkins?

  • Open source tool with great community support.
  • It has 1000+ plugins to ease your work. If a plugin does not exist, you can code it and share with the community.
  • It is built with Java and hence, it is portable to all the major platforms.
  • With Jenkins you can build snapshots and release artifacts of your application.
  • It is also used for deployment of released artifact with custom scripts.
  • It provides continuous integration pipeline support for establishing software development life cycle work flow for your application.
  • It also provides support for scheduled builds & automation test execution.
  • You can configure Jenkins to pull code from a version control server like GitHub, BitBucket etc. whenever a commit is made.
  • It can execute bash scripts, shell scripts, ANT and Maven Targets.
  • It can be used to Publish results and send email notifications.

Quora

Why Kitematic?

Kitematic is an open source project built to simplify and streamline using Docker on a Mac or Windows PC. Kitematic automates the Docker installation and setup process and provides an intuitive graphical user interface (GUI) for running Docker containers. Kitematic integrates with Docker Machine to provision a VirtualBox VM and install the Docker Engine locally on your machine.

Kitematic guide

PART1. DOCKER. KITEMATIC

PART1_1. Docker Installation

  1. Download Docker instance from the official site(registration required):

I work in macOS so I will proceed with it. Also macOS is great if you want to setup CI for Android and iOS as well.

2. Run the downloaded file and follow the installation instructions to complete the installation process:

3. Launch the docker app and authorize:

4. If everything is ok you can find Docker icon on the AppPanel:

That’s it! Pretty simple!)

PART1_2. Kitematic Installation

  1. Click on Docker icon on the panel and using context menu download Kitematic installation file. It will provide you a link to the Kitematic download page:

2. Run the downloaded file and follow the installation instructions to complete the installation process. At the end you will find auth window to connect to the Docker Hub. Go for registration and further auth:

3. If everything has been installed properly, finally you will find the window with the Docker containers:

PART2. JENKINS CONTAINER

PART2_1. Deploying Jenkins container

1. Search for “jenkins” official docker container using kitematic search panel or pick it up from the suggested list and press “CREATE” button. It will automatically download and deploy container using Docker:

2. This window should appear if everything was done right:

3. After that click on the WEB PREVIEW window (top-right corner) and it will automatically redirect you to the web browser with the Jenkins page

PART2_2. Jenkins setup

  1. To unlock Jenkins we need to provide InitialAdminPassword. Go back to Kitematic and click on the path that is located at the VOLUMES section:

2. Enable Volumes to edit files:

3. Jenkins working directory will be opened:

4. Go through the jerkins_home -> secrets -> initialAdminPassword and open it. Then copy the content and paste it to the Jenkins Getting started page to complete Jenkins installation.

5. Jenkins will suggest you to choose plugins. Use default setup:

6. This what you will find at the end. Some plugins are not installed, but no worries just press the “Continue” button and move forward

7. Add new credentials for Admin user and then click on Save and Finish button.

8. Press “Start using Jenkins” button

9. Finally, We are ready to start working with Jenkins!

PART2_3. Jenkins update

1. First of all we need to update Jenkins to the latest version, because it is outdated. Click on the red area and it will expand the notifications.

2. Click on the download link and get the latest version:

3. Open Kitematic app and click on the Volumes path:

4. Delete all files inside the “war” folder:

5. Extract all the files from the downloaded “jenkins.war” with any archive tool (its a simple archive) and copy the content into the jenkins “war” folder

6. Restart “jenkins” container:

7. After it starts click on the Kitematic WEB PREVIEW section and go to the Jenkins web page to check that the latest version was applied:

8. The last step is to actualize plugins versions. Go to package manager and update all the plugins to the latest one:

If everything has passed successful no warnings should be on top

PART3. ANDROID SDK SETUP

PART3_1. Setup Android SDK

Let’s check that Jenkins container is running using command line.

  1. Open “terminal” and type the following command:

docker container ls

As a result you will see the following output

2. Next command will provide us access to the jenkins container with the root permissions:

docker exec — user root -it jenkins bash

3. Lets set up OS environment. By default Linux distributive is “Debian”. To work with the file system effeciantly we need to install “Midnight Commander”:

4. Update debian repositories:

apt-get update

5. Install MC:

apt-get install mc

Now we can easily navigate through the file system

6. Navigate to “/var/jenkins_home”:

cd /var/jenkins_home

7. Create folder android-sdk:

mkdir android-sdk

8. Go inside the folder

cd /var/jenkins_home/android-sdk

9. Download SDK tools(for Linux) using wget utility

wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip

10. Let’s extract the content from the archive:

unzip sdk-tools-linux-4333796.zip

PART3_2. SDK Tools Setup

  1. Accept licenses. Go to SDK tools utility folder:

cd /var/jenkins_home/android-sdk/bin

2. Run SDK update:

./sdkmanager — include_obsolete — update

3. Type the following command :

./sdkmanager — licenses

Accept 5 of them by typing “y”

4. This command will show the list of available platforms and tools for download:

./sdkmanager — list — verbose

5. Please pick the version number from the list and replace it below. My project uses build tools version 25.0.2 and platform android 25. You can find such specifics in the project build.gradle file:

./sdkmanager “platforms;android-25” “build-tools;25.0.2” “extras;google;m2repository” “extras;android;m2repository”

PART4. JENKINS JOB

Part4_1. Global Variable

In this final part we will create a simple job for nightly builds

  1. Go to Manage Jenkins > Configure System and add a new “Environment variables”

2. Add new global variable ANDROID_HOME with the path /var/jenkins_home/android-sdk and save it:

Part4_2. Nightly Build Job Configuration

3. Let’s create our first job to assemble nightly builds! Click on “New item”

4. Choose “Freestyle project” and give a name “Android Nightly Build” and click OK button:

5. Now we are ready to setup job specifics:

6. Give description “The job is used to assemble nightly builds”:

7. GIT setup. Just copy the git link and paste it into the Repository URL. For instance, I use the following project for nightly builds “https://github.com/temyco/security-workshop-sample.git”. Also provide the branch name to assemble the new build from. In my case it is “*/master”:

8. Go to build section and put a task name to build a specific variant of the app. Gradle will use it for assembling. I use “build”:

9. Now we have to schedule the build time. On my side Jenkins do that every night at 2 am. The pattern for that “H 2 * * *”

10. And the last step is to publish “apk” file. Just put the following pattern “**/*.apk” and don’t forget to press “Apply” and “Save” buttons to save the job:

11. And that’s it! We have completed all the steps. Now let’s run it! Go back to main window and you will the following. Just press the “Run” button and wait until the assembling will be finished

Build execution status you can find here:

If you need more details just press on the link and you will find a lot useful information with the console output logs:

BUILD SUCCESSFUL u should find at the end of the assembling process:

OMG! Where is my apk file??? Just click on the job link:

And here it is:

CONCLUSION

--

--