Us­ing Jenk­ins to Create a Pipe­line for An­droid Ap­pli­ca­tions

This ar­ti­cle is a tu­to­rial on how to create a pipe­line to per­form code anal­y­sis us­ing lint and create a APK file for An­droid ap­pli­ca­tions.

OpenSource For You - - Contents - By: Bhagyashri Jain The au­thor is a sys­tems en­gi­neer and loves An­droid de­vel­op­ment. She likes to read and share daily news on her blog at http://bjlit­tlethings.word­

Con­tin­u­ous in­te­gra­tion is a prac­tice that re­quires de­vel­op­ers to in­te­grate code into a shared repos­i­tory such as GitHub, GitLab, SVN, etc, at reg­u­lar in­ter­vals. This con­cept was meant to avoid the has­sle of later find­ing prob­lems in the build life cy­cle. Con­tin­u­ous in­te­gra­tion re­quires de­vel­op­ers to have fre­quent builds. The common prac­tice is that when­ever a code com­mit oc­curs, a build should be trig­gered. How­ever, some­times the build process is also sched­uled in a way that too many builds are avoided. Jenk­ins is one of the most pop­u­lar con­tin­u­ous in­te­gra­tion tools.

Jenk­ins was known as a con­tin­u­ous in­te­gra­tion server ear­lier. How­ever, the Jenk­ins 2.0 an­nounce­ment made it clear that, go­ing forward, the fo­cus would not only be on con­tin­u­ous in­te­gra­tion but on con­tin­u­ous de­liv­ery too. Hence, ‘au­to­ma­tion server’ is the term used more of­ten af­ter Jenk­ins 2.0 was re­leased. It was ini­tially de­vel­oped by Kohsuke Kawaguchi in 2004, and is an au­to­ma­tion server that helps to speed up dif­fer­ent DevOps im­ple­men­ta­tion prac­tices such as con­tin­u­ous in­te­gra­tion, con­tin­u­ous test­ing, con­tin­u­ous de­liv­ery, con­tin­u­ous de­ploy­ment, con­tin­u­ous no­ti­fi­ca­tions, or­ches­tra­tion us­ing a build pipe­line or Pipe­line as a Code.

Jenk­ins helps to man­age dif­fer­ent ap­pli­ca­tion life­cy­cle man­age­ment ac­tiv­i­ties. Users can map con­tin­u­ous in­te­gra­tion with build, unit test ex­e­cu­tion and static code anal­y­sis; con­tin­u­ous test­ing with func­tional test­ing, load test­ing and se­cu­rity test­ing; con­tin­u­ous de­liv­ery and de­ploy­ment with au­to­mated de­ploy­ment into dif­fer­ent en­vi­ron­ments, and so on. Jenk­ins pro­vides eas­ier ways to con­fig­ure DevOps prac­tices. The Jenk­ins pack­age has two re­lease lines:

LTS (long term sup­port): Re­leases are se­lected ev­ery 12 weeks from the stream of reg­u­lar re­leases, en­sur­ing a sta­ble re­lease.

Weekly: A new re­lease is avail­able ev­ery week to fix bugs and pro­vide fea­tures to the com­mu­nity.

LTS and weekly re­leases are avail­able in dif­fer­ent flavours such as .war files (Jenk­ins is writ­ten in Java), na­tive pack­ages for the op­er­at­ing sys­tems, in­stall­ers and Docker con­tain­ers.

The cur­rent LTS ver­sion is Jenk­ins 2.73.3. This ver­sion comes with a very use­ful op­tion, called De­ploy to Azure. Yes, we can de­ploy Jenk­ins to the Mi­crosoft Azure pub­lic cloud within min­utes. Of course, you need a Mi­crosoft Azure sub­scrip­tion to utilise this op­tion. Jenk­ins can be in­stalled and used in Docker, FreeBSD, Gen­too, Mac OS X, OpenBSD, openSUSE, Red Hat/Fe­dora/Cen­tOS, Ubuntu/ De­bian and Win­dows.

The fea­tures of Jenk­ins are:

Sup­port for SCM tools such as Git, Sub­ver­sion, Star Team, CVS, Ac­cuRev, etc.

Ex­ten­si­ble ar­chi­tec­ture us­ing plug­ins: The plug­ins avail­able are for An­droid de­vel­op­ment, iOS de­vel­op­ment, .NET de­vel­op­ment, Ruby de­vel­op­ment, li­brary plug­ins, source code man­age­ment, build tools, build trig­gers, build no­ti­fiers, build re­ports, UI plug­ins, au­then­ti­ca­tion and user man­age­ment, etc.

It has the ‘Pipe­lines as a Code’ fea­ture, which uses a do­main-spe­cific lan­guage (DSL) to create a pipe­line to man­age the ap­pli­ca­tion’s life­cy­cle.

The master agent ar­chi­tec­ture sup­ports dis­trib­uted builds. To in­stall Jenk­ins, the min­i­mum hard­ware re­quire­ments are 256MB of RAM and 1GB of drive space. The rec­om­mended hard­ware con­fig­u­ra­tion for a small team is 1GB+ of RAM and 50 GB+ of drive space. You need to have Java 8 - Java Run­time En­vi­ron­ment (JRE) or a Java De­vel­op­ment Kit (JDK).

The eas­i­est way to run Jenk­ins is to down­load and run its lat­est sta­ble WAR file ver­sion. Down­load the jenk­ins.war file, go to that di­rec­tory and ex­e­cute the fol­low­ing com­mand: java -jar jenk­ins.war.

Next, go to http://<lo­cal­host|IP ad­dress>:8080 and wait un­til the ‘Un­lock Jenk­ins’ page ap­pears. Fol­low the wiz­ard instructions and in­stall the plug­ins af­ter pro­vid­ing proxy de­tails (if you are con­fig­ur­ing Jenk­ins be­hind a proxy).


To in­stall plug­ins, go to Jenk­ins Dash­board > Man­age Jenk­ins > Man­age Plug­ins. Ver­ify the up­dates as well as the avail­able and the in­stalled tabs. For the HTTP proxy con­fig­u­ra­tion, go to the Ad­vanced tab.

To man­u­ally upload plug­ins, go to Jenk­ins Dash­board > Man­age Jenk­ins > Man­age Plug­ins > Ad­vanced > Upload Plugin.

To con­fig­ure se­cu­rity, go to Jenk­ins Dash­board > Man­age Jenk­ins > Con­fig­ure Global Se­cu­rity. You can con­fig­ure au­then­ti­ca­tion us­ing Ac­tive Di­rec­tory, Jenk­ins’ own user data­base, and LDAP. You can also con­fig­ure au­tho­ri­sa­tion us­ing Ma­trix-based se­cu­rity or the pro­ject­based Ma­trix au­tho­ri­sa­tion strat­egy.

To con­fig­ure en­vi­ron­ment vari­ables (such as ANDROID_ HOME), tool lo­ca­tions, SonarQube servers, Jenk­ins lo­ca­tion, Qual­ity Gates - Sonarqube, e-mail no­ti­fi­ca­tion, and so on, go to Jenk­ins Dash­board > Man­age Jenk­ins > Con­fig­ure Sys­tem.

To con­fig­ure Git, JDK, Gra­dle, and so on, go to

Jenk­ins Dash­board > Man­age Jenk­ins > Global Tool Con­fig­u­ra­tion.

Cre­at­ing a pipe­line for An­droid ap­pli­ca­tions

We have the fol­low­ing pre­req­ui­sites:

Sam­ple the An­droid ap­pli­ca­tion on GitHub, GitLab, SVN or file sys­tems.

Down­load the Gra­dle in­stal­la­tion pack­age or con­fig­ure it to in­stall au­to­mat­i­cally from Jenk­ins Dash­board. Down­load the An­droid SDK.

In­stall plug­ins in Jenk­ins such as the Gra­dle plugin, the An­droid Lint plugin, the Build Pipe­line plugin, etc.

Now, let’s look at how to create a pipe­line us­ing the Build Pipe­line plugin so we can achieve the fol­low­ing tasks: Per­form code anal­y­sis for An­droid ap­pli­ca­tion code us­ing An­droid Lint.

Create an APK file.

Fig­ure 1: Global tool con­fig­u­ra­tion Fig­ure 2: Gra­dle in­stal­la­tion

Fig­ure 3: ANDROID_HOME en­vi­ron­ment vari­able Fig­ure 4: Source code man­age­ment Fig­ure 5: Lint con­fig­u­ra­tion

Create a pipe­line so that the first code anal­y­sis is per­formed and on its suc­cess­ful im­ple­men­ta­tion, ex­e­cute an­other build job to create an APK file.

Now, let’s per­form each step in se­quence.

Con­fig­ur­ing Git, Java, and Gra­dle: To ex­e­cute the build pipe­line, it is nec­es­sary to take code from a shared repos­i­tory. As shown be­low, Git is con­fig­ured to go fur­ther. The same con­fig­u­ra­tion is ap­plied to all ver­sion con­trol that is to be set up. The path in Jenk­ins is Home > Global tool con­fig­u­ra­tion > Ver­sion con­trol / Git.

Fig­ure 6: Pub­lish An­droid Lint re­sults

In an An­droid pro­ject, the main part is Gra­dle, which is used to build our source code and down­load all the nec­es­sary de­pen­den­cies re­quired for the pro­ject. In the name field, users can en­ter Gra­dle with their ver­sion for bet­ter read­abil­ity. The next field is Gra­dle Home, which is the same as the en­vi­ron­ment vari­able in our sys­tem. Copy your path to Gra­dle and paste it here. There is one more op­tion, ‘In­stall au­to­mat­i­cally’, which in­stalls Gra­dle’s lat­est ver­sion if the user does not have it.

Con­fig­ur­ing the ANDROID_HOME en­vi­ron­ment vari­able: The next step is to con­fig­ure the SDK for the An­droid pro­ject that con­tains all the platform tools and other tools also.

Here, the user has to fol­low a path to the SDK file, which is present in the sys­tem.

The path in Jenk­ins is Home> Con­fig­u­ra­tion >SDK. Cre­at­ing a Freestyle pro­ject to per­form Lint anal­y­sis for the An­droid ap­pli­ca­tion: The ba­sic setup is ready; so let’s start our pro­ject. The first step is to en­ter a proper name (An­droidApp-CA) to your pro­ject. Then se­lect ‘Freestyle pro­ject’ un­der Cat­e­gory, and click on OK. Your pro­ject file struc­ture is ready to use.

The user can cus­tomise all the con­fig­u­ra­tion steps to show a neat and clean func­tion. As shown in Fig­ure 4, in a gen­eral con­fig­u­ra­tion, the ‘Dis­card old build’ op­tion dis­cards all your old builds and keeps the num­ber of the build at what­ever the user wants. The path in Jenk­ins is Home> #your_pro­ject# > Gen­eral Set­ting.

In the last step, we con­fig­ure Git as ver­sion con­trol to pull the lat­est code for the Build Pipe­line. Se­lect the Git op­tion and pro­vide the repos­i­tory’s URL and its cre­den­tials. Users can also men­tion from which branch they want to take the code, and as shown in Fig­ure 5, the ‘Master’ branch is ap­plied to it. Then click the Ap­ply and Save but­ton to save all your con­fig­u­ra­tion steps.

The next step is to add Gra­dle to the build, as well as add Lint to do static code anal­y­sis. Lint is a tool that per­forms code anal­y­sis for An­droid ap­pli­ca­tions, just as Sonarqube does in Java ap­pli­ca­tions. To add the Lint task to the con­fig­u­ra­tion, the user has to write Lint op­tions in the

build.gra­dle file in the An­droid pro­ject.

The An­droid Lint plugin of­fers a fea­ture to ex­am­ine the XML out­put pro­duced by the An­droid Lint tool and pro­vides the re­sults on the build page for anal­y­sis. It does not run Lint, but Lint re­sults in XML for­mat must be gen­er­ated and avail­able in the workspace.

Cre­at­ing a Freestyle pro­ject to build the APK file for the An­droid ap­pli­ca­tion: Af­ter com­plet­ing the anal­y­sis of the code, the next step is to build the An­droid pro­ject and create an APK file to ex­e­cute fur­ther.

Cre­at­ing a Freestyle pro­ject with the name An­droidApp-APK: In the build ac­tions, se­lect In­voke Gra­dle script.

Archive the build arte­facts such as JAR, WAR, APK or IPA files so that they can be down­loaded later. Click on Save.

Af­ter ex­e­cut­ing all the jobs, the pipe­line can be pic­to­ri­ally rep­re­sented by us­ing the Build Pipe­line plugin. Af­ter in­stalling that plugin, users have to give the start, mid­dle and end points to show the build jobs in that se­quence. They can con­fig­ure up­stream and down­stream jobs to build the pipe­line.

To show all the build jobs, click on the ‘+’ sign on the right hand side top of the screen. Se­lect build pipe­line view on the screen that comes up af­ter click­ing on this sign. Con­fig­ur­ing a build pipe­line view can be de­cided on by the user, as per re­quire­ments. Se­lect An­droidApp-CA as the ini­tial job.

There are mul­ti­ple op­tions like the Trig­ger Op­tion, Dis­play Op­tion, Pipe­line Flow, etc.

As con­fig­ured ear­lier, the pipe­line starts by click­ing on the Run but­ton and is re­freshed pe­ri­od­i­cally. Up­stream and down­stream, the job ex­e­cu­tion will take place as per the con­fig­u­ra­tion.

Af­ter com­plet­ing all the pro­cesses, you can see the vi­su­al­i­sa­tion shown in Fig­ure 11. Green colour in­di­cates the suc­cess­ful ex­e­cu­tion of a pipe­line whereas red in­di­cates an un­suc­cess­ful build.

Fig­ure 7: Gra­dle build

Fig­ure 9: Down­stream job

Fig­ure 8: Archive Ar­ti­fact

Fig­ure 11: Suc­cess­ful ex­e­cu­tion of the Build Pipe­line

Fig­ure 10: Build Pipe­line flow

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.