Jenkins : Continuous Integration / Continous Delivery for Android & iOS mobile Applications

Learn how to setup Android/iOS Continuous Integration Jenkins in mac

Hello guys,  We will learn what is Jenkins,  how to setup and configure it in the Mac OS X for Android and iOS.

Build Automation on Android using Jenkins,

Jenkins is an open source automation tool which can be used to automate all kinds of tasks related to building, testing, and deployment softwares, Android and iOS applications.

In real life Jenkins will be installed in a centralized server, where all your core working developer can access. But to play with jenkins and it features or if we want to automate our own mobile apps, we can install it in our local server.

 

 

Setting up ANDROID_HOME

Before install jenkins setup the android home

I believe you have setup the Android home

Add the following 3 lines to  ~/.bash_profile using vi

 

In Terminal ,

vi ~/.bash_profile

export ANDROID_HOME=/Users/admin/Library/Android/sdk/
export PATH=$PATH:$ANDROID_HOME
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/platform-tools

refesh the path

source ~/.bash_profile

 

Lets install the jenkins server

 

Install Jenkins in Mac OSX

You can install jenkins in various way such as you can use docker, native system packages or run as standalone. Since i am working in my mac OSX i am going to download the Setup for Mac OSX from Jenkins official page.

By default the downloaded installer sets up Jenkins as a launch daemon, listening on port 8080. Daemon is a kind of special background program which can be operated without any user interfaces. Don’t forget you should launch your daemon via launchd. Launchd is a command line tool.

After the download…check the following preference file. it is added. Daemon will access the config details from this file.

Now access the (http://localhost:8080/securityRealm/firstUser) i think by default it installer will redirect to this url. You will create a default user.

 

Unlock Jenkins

To ensure Jenkins is securely set up by the administrator, a password has been written to the log (not sure where to find it?) and this file on the server:

/Users/Shared/Jenkins/Home/secrets/initialAdminPassword

Please copy the password from the location and paste it below.

If You get error like this

-bash: cd: Shared/Jenkins/Home/secrets: Permission denied

This is because of the user permission issue. You can manually give permission to the particular shared user folder.

  • Navigate to this folder /Users/Shared/Jenkins/Home
  • Right click on secrets/ folder and select “Get Info”
  • Scroll down to the right bottom corner of the pop up window and click on the lock image > enter your password > ok
  • Click on the “+” at the left bottom corner of the pop up window and add the user 4.5 Click on Settings icon – left bottom and Apply changes.
  • Open the “secrets” folder and find the initialAdminPassword file to get the initial admin password. If you don’t have permission to the file, you need to right click on the file and select “Get Info” then repeat step 3 and 4 above to access the file.

Or use following command to print the password

 sudo cat /Users/Shared/Jenkins/Home/secrets/initialAdminPassword 

Password will be printed in command line. paste it in browser textfield. You will be redirected to next page.

Install the Initial necessary plugins

 

Now click on the “Install Suggested plugins” button. Most useful plugins will be installed by default. Mostly what we needed for Android and iOS are

1) Git Plugin.
2) Gradle Plugin
3) Junit plugin

 

 

 

Necessary plugns will be installed automatically and it will be rediercted to another page. click to continue as an admin.

We should install these plugins manually.

1) Android Lint plugin (We will install later)
2) Google Play publisher plugin.(To publish an app to google play automatically.)

You will see the Jenkins dashboard..

 

Starting and stopping the Jenkins server using commandline

To manually start the daemon:

sudo launchctl load /Library/LaunchDaemons/org.jenkins-ci.plist

To manually stop the daemon:

sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist

Lets install missing plugins

select the plugins and press install.

 

Create an Android Github Project.

 

Before we goto next step lets create an Android Project and push it to github. So it will be easy for us to do the demo.

Add some example unit tests to the file in the   app -> src -> test folder

package com.appsgit.jenkinsexample;

import org.junit.Test;

import static org.junit.Assert.*;

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
public class ExampleUnitTest {
    @Test
    public void addition_isCorrect() throws Exception {
        assertEquals(4, 2 + 2);
    }

    @Test
    public void subscription_isCorrect() throws Exception {

        //WE intentionally made the string null
        String myName = null;

        assertNull(myName);
    }
}

 

Upload the Project to Github

Initialize the git repo and push the project to github.

git init
git add .
git commit -m “first commit”
git remote add origin https://github.com/zumrywahid/Jenkins_Example_Android_App.git
git push -u origin master

In this simple android project, we will show you how to trigger Jenkins to build the app. If everything succeed well, we would sign APK file and upload it to Play store (Maybe later).

Now you can see the project is in my github.

 

What is Jenkins Job ?

Jenkins build is the most important part in the jenkins continuous integration. We can call it as a build job where it containts build related information. Lets create a job now,

Create a new free-style project and name it as “Jenkins_Android_Example”

 

Fill the General section and type the github project name without “.git” part.

jenkins job genral section

 

Next goto “Source code management section”

Type your github project url. and credentials.

 

Goto Build Trigger section.

There are ways you can trigger jenkins to build your project. You can do them in the following ways,

1) Build the project remotely.(This is the most suitable solution)
2) Build the project in an interval. This is cron like feature. (Ex:-in minutes/hours/days).
3) Using Github hook trigger.

Goto Build Environment section.

Here you can do many things, such as setting upto clean the workspace before the building process start. You can configure more things here.

Goto Build Section.

This is the most important part for Android Build process. Click on the (Add Build step) dropdown and select “Invoke Gradle script”.

Select “Use Gradle Wrapper” because Android project root has Gradle Wrapper by default. Lets add some Gradle tasks in the tasks text area. You can check more info about gradle tasks in https://wiki.jenkins.io/display/JENKINS/Gradle+Plugin

 

 

if the tasks field is blank the defaults tasks of the build will be invoked.

Note : Don’t Type anything in the task area for now.! By default Gradle wrapper will run all the build tasks.

 

Goto Post-build Actions Section.

 

This section will execute the actions after the build is successfully completed. You can set it up to send email notification after the build succeed or you can upload the APK to playstroe and you can many more things…!

So cool right ?

 

Setup to show Junit Result

Now lets go and print the unit test result. Unit test result will report will show you the successful and failed methods,

 

in the test report xml path field it is optional to type “app/build/test-results/debug/*.xml” by default it jenkins will search for the unit result file . The Junit plugin will create the file there for you in the Jenkins workspace.

Select “publish Junit test result report” in Post-build Actions Section and give the following path.

You should see following setting after you selected the above.

 

 

We already have installed Junit plugin.  If you have not installed that first do it.

 

 

Save everything. and goto dashboard. You can see the created Job.

 

Before execute the job please setup the environment variables.

Setting up Environment Variables

Go to Manage Jenkins > Configure System
Scroll down and at Global properties section check the Environment variables box and add ANDROID_HOME and JAVA_HOME

 

Find JAVA_HOME

exceute this commnd in the mac osx terminal

echo $(/usr/libexec/java_home)

It will print the path java is installed.

 

You can find the Android sdk path in the Android Studio preferences.

Now setup the ANDROID_SDK_HOME home and java home

 

 

Goto Global Properties -> Environment Variables section

 

 

Thats all. Save the job.  lets go and run the Jenkins job we careated.

Build the Job

Build the job manually for now. We can let Jenkins build automatically as well.

Build the project and check the console. You should see the below result.

Build is successful…

Total time: 49.873 secs
Build step 'Invoke Gradle script' changed build result to SUCCESS
Recording test results
Finished: SUCCESS

 

 

Now check the Unit Test result in the build result.

 

Learn how to setup Android iOS Continuous Integration Jenkins in mac

 

JUnit result are show here.

Learn how to setup Android Continuous Integration Jenkins in mac

Show the Android Lint Result

Now lets try the Lint,

Now lets go and print the Lint Result. Lint result will give you the overview of the code quality. Whether you have been maintaining code conventions upto the level.

Learn how to setup Android Continuous Integration Jenkins in mac

Note : This plugin does not run Lint automatically for you. You should provide Lint results in XML format, either by running lint during a build, or by copying the file(s) from somewhere else.

Setup the post build action like below.

Running ./gradlew lint will compile write the results to build/lint-results-debug.xml. Don’t forget to update task field like below

Learn how to setup Android Continuous Integration Jenkins in mac

Its better to disable the ‘abortOnError’ option, to ensure that the Gradle script reports success, even if Lint finds issues with your project that it regards fatal.

To do that update the build.gradle file in you app module.

android {
// ...

lintOptions {
// Don't abort if Lint finds an error, otherwise the Jenkins build
// will be marked as failed, and Jenkins won't analyse the Lint output
abortOnError false
}
}

 

 

BUILD SUCCESSFUL

 

 

Now both Lint result and Unit test result are generated,

Total time: 50.55 secs
Build step 'Invoke Gradle script' changed build result to SUCCESS
[android-lint] Collecting Android Lint files...
[android-lint] Searching for all files in /Users/Shared/Jenkins/Home/workspace/Jenkins_Example_Android_App that match the pattern **/lint-results-debug.xml
[android-lint] Parsing 1 file in /Users/Shared/Jenkins/Home/workspace/Jenkins_Example_Android_App
[android-lint] Successfully parsed file /Users/Shared/Jenkins/Home/workspace/Jenkins_Example_Android_App/app/build/outputs/lint-results-debug.xml with 5 unique warnings and 0 duplicates.
[android-lint] Computing warning deltas based on reference build #9
Recording test results
Finished: SUCCESS

The jenkins Job Main dashboard should be like this,

Learn how to setup Android Continuous Integration Jenkins in mac

It is updated with Lint result.

 

Thats all folks,

 

Possible error are 

 


Ran lint on variant debug: 5 issues found
Ran lint on variant release: 5 issues found
Wrote HTML report to file:///Users/Shared/Jenkins/Home/workspace/Jenkins_Example_Android_App/app/build/outputs/lint-results-debug.html
Wrote XML report to file:///Users/Shared/Jenkins/Home/workspace/Jenkins_Example_Android_App/app/build/outputs/lint-results-debug.xml

Solution :- You should type the xml file name pattern i have done above. If not go to the jenkins workspace and check the path.


 

problem
A problem occurred configuring project ‘:app’.
> SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

Solution : Set the Environment variables in the Config page. Check my post.


 

 

Let me know if you have anything in the comments.

 

 

About Zumry

Zumry Mohamed

Self Taught iOS & Android Mobile Application Developer.

Article written by zumrywahid

Self Taught iOS & Android Mobile Application Developer.

Be the first to comment

Leave a Reply

Your email address will not be published. Required fields are marked *