Deploying CI server on Linux: Personal android test machine

Let’s see how to use and deploy Jenkins CI flow for Android apps. Specifically, setting up automated unit testing and building projects step-by-step.
 
Diving into development for Android OS, you’ll probably hear about a thing called unit testing. In fact, development for any platform has a thing for unit testing. And that’s because of a reason.

README: All examples are on Android Studio, Android SDK and Linux (so if you’re not into these, some examples may be hard to follow. Or not ;) We also covered CI server on Mac OS for iOS using GitLab and Fastlane for iOS devs.

Why care about unit testing?

Unit testing is definitely a great thing to introduce to your project, because you can make sure that:

  • New modules don’t affect the existing unit-tested parts of the project.
  • A specific code part works as we expect it to.

In case they don’t, failed tests immediately show you where the problem is.

Unit tests are executed on a virtual Java machine, and of course you can run them directly in Android Studio. However, if your team is more than just you, using a continuous integration tool that automatically launches unit tests and generates informative report is a better idea.

You can then export this file as .apk and share via email with your QAs, for example. And here’s a solution for such purposes: Jenkins CI server https://jenkins.io/

How to install for Jenkins  CI server

So, to install Jenkins:

  1. Download the latest stable version at http://mirrors.jenkins.io/war-stable/latest/jenkins.war
  2. Open up a terminal and go to the download folder (or a folder to where you moved the jenkins.var file) with a command:
    $ cd home/anyfolder/Downloads/
  3. Run a command:
    $ java -jar jenkins.war --httpPort=8080

This will launch a script that’ll create a hidden .Jenkins subfolder in the home folder. All server configurations will be stored in that subfolder. The server will run as long as the terminal with script is open.

To stop the server, either close the terminal window, or press Ctrl + C.

Setting Jenkins server up

After you run the script, the building of a local Jenkins server starts (using 8080 port).

On the Getting Started screen at http://localhost:8080/, you’ll need to enter  

Initial Administrator Password. For this purpose, copy the provided path to your clipboard.

Therefore, to locate this password:

  1.     Launch a separate terminal instance and paste that path. For example:
    $ cd /home/ruslanstosyk/.jenkins/secrets
    This will open the secrets folder.
  2. Use the cat command that allows reading content of a specific file. In this case, run cat command with initialAdminPassword filename:
    $ cat initialAdminPassword
    This will display your password on the console.
  3. Copy and paste the password to the Administrator Password field and then click Continue.
  4. At the next step, select Install suggested plugins.

    After you familiarise with Jenkins or require some specific tools only, you may use Select plugins to install. However, at first and for basic needs, Install suggested plugins option is better.
  5. Wait a few minutes while the installation is in progress.
     
  6. Create a user for signing into the system and configuring the server and click Save and Continue.

  7. Sign in to Jenkins using your username and password.

Configuring Jenkins integration for your needs

So, after you’re all set up with the server inself, you’ll need to arm it with plugins that bring most benefits of continuous integration for building Android apps. First, from the start screen, select Manage Jenkins.

From the menu that appears, select Manage Plugins to go to the plugin management page. Here you can add, update, and select plugins you need. We’ll add plugins to customize the Jenkins deployment and  adjust it to the required types of tasks. 

Go to the available inset and find and download the following plugins (or use the links):

After you install all plugins you need and to properly launch them, restart Jenkins tool.

Set up the path to folders that contain Java and Android SDK:

  1. From the start page, to to Manage Jenkins, and select Configure System option.
  2. Scroll down to the Global Properties and select the Environment Variables checkbox.
  3. In the Name field, paste JAVA_HOME and specify the path to you Java folder in the Value field.
  4. Click Add.
  5. In the name-value pair that appears, paste ANDROID_HOME as Name and specify the path to your Android folder in the Value field.
  6. Click Save.

Creating a Jenkins CI project for Android

So, finally, we’re all set up for creating a new project.

Bind Jenkins to your project on Git

  1. On the start page click New Item.

  2. Assign a name for your project. This name must not contain spaces.
    For example, testproject, test-project, Test_Project.

  3. Select Freestyle project option.
    This will launch the project configuration page.

  4. Go to Source Code Management.

  5. Select Git checkbox and paste full path to your project.

  6. Enter your credentials to let Jenkins clone and launch your project.

Add rules for building the project

Next tab we’ll need for Jenkins continuous deployment is Build Triggers. Here we can add rules for launching the project’s build. There’s a variety of options we can set up for starting the build. For example, detecting a specific keyphrase in a commit name such as [ci start] or any other you set up. Yet, to make use of this functionality, you’ll have to deploy Jenkins CI on a web server (instead of a local one), because it’s implemented using web hooks.

In our case, we used an option that starts build every 15 minutes. This option uses cron  syntax.

In the Build Environment tab, select Abort the build if it's stuck checkbox. This option is quite handy for cases when an unexpected error occurs and you need to free queue and deallocate memory.

So, in the Build tab, you have to specify the options to run:

  1. From the drop-down menu, select the Invoke Gradle Script option.

  2. In the Invoke Gradle Script dialog, select Use Gradle Wrapper checkbox.

  3. Fill the Tasks field with the script that defines the order for running the commands.
    We used:
    clean
    assembleDebug
    test
    connectedAndroidTest

  4. Then configure Post-Build Actions by selecting the following options from the drop-down:

  • Publish JUnit test result report

Using this option, you’ll configure Jenkins to save the detailed report on each test run. You’ll also get a nice visualization of the report.

  • Archive the artifacts

This option builds the project after all tests are run. So you’ll get an .apk  file ready to be downloaded and installed on a device.

5. Click Save.

To launch the project, either run an emulator, or connect some Android device to your computer and then click Build Now.

While the project is being cloned from the repo and the build is starting, you can explore the build status and info about success/failure of each stage in the Console Output.

Review the build

After the project is successfully built, you’ll see the following logs in the Console Output:
 

From the main page of the build, you can download the .apk file and build logs, review the information about running the tests (success and failures), and more.

Started and up!

So we’ve made quite a lot of configurations. As a result, we’ve got a continuous integration using Jenkins for automating a part of build creation and launching tests when developing an Android app.

This article covers only the tip of the iceberg of what you can set up and customize via Jenkins CI for your project/requirements.

Thanks for reading and hope it helps you automate mundane tasks ;) Subscribe for more nifty stuff about continuous integration tools, IoT, Android development, and more!

Share via:
Recommended posts:
Ray tracing implementation using a Metal framework
9/2/15

Ray tracing implementation using a Metal framework

Connecting Raspberry PI via Azure IoT: Step-By-Step Tutorial
1/10/19

Connecting Raspberry PI via Azure IoT: Step-By-Step Tutorial

Node.js back-end sucks, doesn’t it?
2/24/16

Node.js back-end sucks, doesn’t it?

Interested in latest news in IT sphere?

Subscribe to start receiving notifications about new posts
17 Sep 2019

5 IoT Trends to Watch in 2020

4 Sep 2019

Why Node.js Is the Best Solution for Internet of Things

27 Aug 2019

How we created IoT system for managing solar energy usage

12 Aug 2019

Deploying CI server on Linux: Personal android test machine