Configuring Android Project on Jenkins

Abhishek Jain
Tech Insider

--

`

Part 1: Jenkins — Local Installation and Setup From Scratch On Linux

In Part 2: we will focus on configuring Jenkins for Android, Build Android Project. This Article will be in Three points —

  • Installing plugins (if req.)
  • Set Android SDK, JDK, git, and Gradle executable paths and adding environment variable.
  • Running Android Project.

1) Installing Plugins:

For configuring android we will need a couple of things in our Jenkins System

  • GIT
  • JDK
  • GRADLE

If you have chosen Default plugins, then git and Gradle plugins will be already installed .

To add these plugins, Go to Manage Jenkins -> Manage plugins

Encircled items in the image

Search git plugin and install without restart

Git plugin

Similarly, add Gradle plugin

Gradle Plugin

2) Set Android SDK, JDK, git and Gradle executable paths

Let’s first Set the Android SDK root path. for that go to Manage Jenkins > Configuration. Look for Android SDK root, write down the path of your android SDK path, apply, and save.

Android SDK Root Path

The next one is JDK, for JDK got to Manage Jenkins > Global Tool Configuration. lookout for JDK, press add JDK. I have JDK installed on my system (done in the previous blog)So I will uncheck install automatically option. Choose your copy of your Variable path and your version in the name. Apply and save.

echo $JAVA_HOME
JDK Path

The next one is git, same place in Global Tool Configuration page, find git installations.add your local git exe path. you can take the help of which command for the path. Apply and save.

which git
Git path

The last thing is Gradle, look for Gradle installation on the same Global Tool Configuration page. check the Gradle version in gradle-wrapper.properties of your android project. Now select the version from the list and don't worry you can add multiple versions if something else required. apply and save.

Gradle Version required for the project

Adding Environment Variable (to save ourselves from SDK location not found error). Go to manage jenkins > Configurations , check environment variable and add new.

Set Environment Variable

3) Running Android Project.

Let’s create a New project “Android project”, keeping it as a freestyle project.

Create a New Project

After that, you will see this screen

Source Code Management

Let’s start with Source Code Management, It provides us two options Git and Subversion. We will go with git as we have our repository at GitHub. Add repository URL, branch name.

For Credentials of git, click on add then below screen will open, add username and password (even if you have git generated token choose kind value as username with password, not as a secret value, otherwise it doesn’t show up). Press the add button and choose your added value in the previous screen.

Credentials

So far we set up how to get source code. Now we need to tell Jenkins how to build our project. Navigate to Build section, click on the Add build step button. There are two ways we can configure the build process for Android.

  1. Invoke Gradle Script: By selecting this option, Jenkins will use Gradle installed on the host machine to build our project. For this, we need to tell Jenkins which Gradle version to use to build a project. Make sure Gradle is installed on your computer and require paths that have been set in environment variables.
  2. Use Gradle Wrapper: As we are using the same Gradle version that was used to build a project locally in Android studio, it is less error-prone as there might be some compatibility issues with other versions.

We will go with the first way, project root is referred by ${workspace} variable in Jenkins.

Set Build Information

After that, we need to tell Jenkins which Gradle task we want to execute. Here, we want to build our project and generate APK so we will first clean the project and then build it. Therefore, clean build command is added. The option — stacktrace is added so that we can see the stack trace of the build process if something goes wrong and it fails to build.

Now we are Good to Go, press build now option

If something goes wrong then the dot color will turn to red and if everything is okay and build is successful then the dot color will turn blue. For the first time, it will take time to build as it might need to download required Gradle wrapper, platform tools, build tools, etc required files. You can see the output in the Console output option.

For installation Issues: See Part 1

References :

  1. https://jenkins.io/
  2. https://jenkins.io/doc/
  3. https://medium.com/nonstopio/set-up-jenkins-for-android-continuous-integration-9ffcc70315da

--

--

Abhishek Jain
Tech Insider

Android dev | Software developer at InMobi, Ex- MMT, Tokopedia