Setting up Android Package Kit [.apk] generator using Jenkins job [Gradle|Git]

Jenkins-Android | Productive life source

Being an android developer I can understand how much time consuming task is to generate android package [.apk] on android studio according to other teams requirement based on some testing or development. While generating [.apk] our system memory [mine is Mac Pro 13inch, 8GB Ram] get completely taken over by android studio. To make android developer’s life more convient and productive I have setup a jenkins job on which any developer can generate .apk according to their requirements.

What is Jenkins?

Jenkins is an open-source continuous integration software tool written in the Java programming language. Jenkins is a self-contained, open source automation server which can be used to automate all sorts of tasks such as building, testing, and deploying software. Jenkins can be installed through native system packages, Docker, or even run standalone by any machine with the Java Runtime Environment installed. source

Generally, you would connect each Jenkins project to a SCM tool like Git. This allows you to trigger builds manually, based on a cron schedule, or whenever a source change is detected.

Jenkins supports over 1,000 plugins allowing you to work with many third party services. The best thing is that if you can’t find or use a plugin, you’re only limited by your own scripting ability as it’s able to run any arbitrary shell script

What is continuous integration (CI)?

Continuous Integration (CI) is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early. source

Setup Jenkins on your machine:

  1. Go to https://jenkins.io/download/ and Download Generic Java Package
  2. Open the terminal and go to package where is your jenkins.jar downloaded
  3. run command $ java -jar jenkins.war
  4. Now Jenkins is up and running on your localhost. Open the browser and type localhost:8080 and do initial setup, Now you are home page on jenkins

Pre-Request for android jenkins job

Android SDK

if you have Android SDK on local machine then ignore this step otherwise You need to install the Android SDK from the command line. You also need to configure the location for the Android SKD for Jenkins.

# download the Android SDK via wget
# example
# wget http://dl.google.com/android/android-sdk_r24.2-linux.tgz
# unpack it
tar zxvf filename
# Add the new directory to your patch
# assumes you exported it to /opt/
# this must be set for the Jenkins user
export ANDROID_HOME="/opt/android-sdk-linux"
export PATH="$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH"
// run this as Jenkins user
android update sdk --no-ui

Now you have Android SDK on your local machine then select Manage Jenkins, select Configure System and setup Environment variables ANDROID_HOME

You need the following plug-ins to build Android application with Jenkins:

  1. Gradle Plugin
  2. Git Plugin — if Git is used for the project repository
  3. Android Emulator Plugin — supports a new job with the Build multi-configuration project option. This option allows you to test multiple emulator configurations at the same time. You can, for example, test different languages, densities, screen resolutions, etc.
  4. Git Parameter plugin — if you have more one build branch on project

To install the plugin on jenkins follow below steps

Go to home page -> Manage Jenkins -> Manage Plugins -> select Available -> select all plugin one by one and hit ‘install without restart

Now we are going to Configure build tool (Gradle, ant , maven etc])

[in my case Gradle] To configure build tool, select manage jenkins, select Global Tool Configuration and configure Gradle and setup gradle version on which your application currently getting build in my case I am setting up gradle2.14.1

Global Tool Configuration

Configure JDK

To configure build tool, select manage Jenkins, select Global Tool Configuration and configure JDK and setup JAVA_HOME

JDK Configuration

Creating a job for android APK

To create an Android build job on Jenkins, select New Item, and enter a job name. Select the Build a free-style software project option.

create a build job

Now time to configure your build job, select Configure
if your project have more then one build branch on your git repository, So you need selector while making APK , select This project is parameterized -> Git Parameter

if your project have more then one application to build from one branch, So you need Gradle path selector to specify which application to build. select Choice Parameter

Go to Source Code Management and select Git, Fill your Git repository URL, if your git repository is private then provide Credentials.

setup branch to build?, Here we are going to use Git Parameter variable name which we given few steps earlier

Now go to Build section and select invoke Gradle script and select Gradle version which we setup in build tool configuration

Note: Here GRADLE_PATH is path of gradle which we wants to build. if you remember we have setup choice parameter for gradle path if your project have multiple application/build.gradle.

if your project have one build.gradle then you can put gradle path directly.

save and apply!!!

That is it, you should be able to run your Android build. Go to build with parameter and select branch and select gradle/application which you wants to build.

Bonus :D

1. Archive [.apk] on build console

Configure job and scroll down to post-build Actions

Apply the change and make build. On build console you can see after successful builds artifacts [.apk] are getting archive.

2. Running tests on the device
To run your instrumentation tests on your device you also need to configure a virtual device.
Ensure that you do not select the Show emulator window option because your job should not depend on the availability of a display server.

To run your tests and your instrumentation tests use the test connectedAndroidTesttarget. source

Thanks for reading! Be sure to One clap, two clap, three clap, forty? below to recommend this article if you found it helpful. It means a lot to me.