Automating the Continuous Integration of Android Projects With Gradle Using Jenkins on Windows
Join the DZone community and get the full member experience.Join For Free
this post will show how to automate the deployment process of a android application using jenkins continuous integration – to build the project, run the unit tests (if any), archive the built artifacts and run the android lint reports.
1. install jenkins as a windows service
navigate to jenkins-ci.org website using an internet browser and download the windows native package (the link is underlined for easy identification) as shown from the right side pane of the download jenkins tab.
once the download is complete, uncompress the zip file and click on the jenkins-1.xxx.msi file. proceed through the configuration steps to install the jenkins as a windows service.
2. modify default jenkins port
by default jenkins runs on the port 8080. in order to avoid conflict with other applications, the default port can be modified by editing the jenkins.xml found under c:\program files (x86)\jenkins location. as shown below, modify the httpport to 8082.
<service> <id>jenkins</id> <name>jenkins</name> <description>this service runs jenkins continuous integration system.</description> <env name="jenkins_home" value="%base%"/> <!-- if you'd like to run jenkins with a specific version of java, specify a full path to java.exe. the following value assumes that you have java in your path. --> <executable>%base%\jre\bin\java</executable> <arguments>-xrs -xmx256m -dhudson.lifecycle=hudson.lifecycle.windowsservicelifecycle -jar "%base%\jenkins.war" --httpport=8082</arguments> <!-- interactive flag causes the empty black java window to be displayed. i'm still debugging this. <interactive /> --> <logmode>rotate</logmode> <onfailure action="restart" /> </service>
once the modification is saved in jenkins.xml file, restart the jenkins service from the windows task manager->services and right clicking on the jenkins service and choose stop service to stop the service as shown below.
once the status of the service changes to stopped, restart the service by right clicking on the jenkins service and choose start service to start the service again.
navigate to localhost:8082 to verify if the jenkins restart was successful as shown below – jenkins dashboard will be displayed. note that it takes a while before the jenkins service becomes available.
3. install plugins
on the jenkins dashboard, navigate to manage jenkins –> manage plugins as shown in the snapshot below.
install the following plugins and restart jenkins for the changes to take effect.
- git plugin (for integrating git with jenkins)
- gradle plugin (for integrating gradle with jenkins)
- android lint plugin (for integration lint with jenkins)
4. configure system
on the jenkins dashboard, navigate to manage jenkins –> configure system as shown in the snapshot below.
navigate to the global properties section and click on add to add an environment variable android_home as shown in the snapshot below. enter the name as android_home and enter the path of the location where the android sdk is stored on windows.
navigate to the jdk section and click on “add jdk” to add the jdk installation as shown in the snapshot below. specify a jdk name, choose the jdk version to install and follow the on-screen instructions to save the oracle login credentials. save the changes.
next, proceed to the git section and click on “add git” to add the git installation as shown in the snapshot below. specify git name, specify the path to git executable and save the changes.
next, proceed to the gradle section and click on “add gradle” to add the gradle installation as shown in the snapshot below. specify gradle name, choose the appropriate version (at the time of writing, i used gradle 1.10) and save the changes.
next, proceed to the email notification section and enter the smtp server details as shown below. click on the advanced button to add the further details required and save the changes. click on “test configuration by sending test e-mail”, enter the test e-mail recipient and click on “test configuration” to see if the email is successfully sent.
5. create a new jenkins job
from the jenkins dashboard, click on “new job” to create a new job. enter a name for the job and choose “build a free-style software project” as option and click on ok as shown below.
from the new job configuration screen, proceed to the source code management section. save the git credentials by clicking on “add” as shown below and entering the details in the following dialog. save the changes by clicking on “add” as shown below.
specify the git repository url for the project, choose the saved credentials from the drop-down list as shown in the snapshot below. save the changes.
next, from the build triggers section, select the options desired as shown below and save the changes.
proceed to the build section, choose “invoke gradle script” from the drop-down list of choices for “add build step”. choose the appropriate gradle version which is configured, enter the tasks to be built and select the options as desired. save the changes.
proceed to the post-build actions section, click on “publish android lint results” from the drop-down list of choices for “add post-build action” and specify the location where the lint results should be stored in the jenkins workspace for the job.
similarly, click on “archive the artifacts” from the drop-down list of choices for “add post-build action” and the specify the format of apk files to be archived after every build. additionally, options from advanced section such as “discard all but the last successful/stable artifact to save disk space” could be enabled for saving disk space.
click on “e-mail notification” from the drop-down list of choices for “add post-build action” and enter the values for the email recipients as shown below. save the changes.
6. build now
once the above configuration steps are complete, click on “build now” under the jenkins –> build android application (or the respective job name) to build the project based on the configuration.
the console output has the detailed logs of what steps were initiated by the configuration and the outcome of the entire build. clicking on any successful build outcome shows the artifacts that were archived as part of the build, the change that started the build and the lint results as shown below.
thus the entire process of building the project an android application project whenever a scm change is triggered or under another condition, running lint reports, archiving the artifacts built, publishing lint reports and triggering emails to the recipients can be automated with a click of a button through jenkins.
Published at DZone with permission of Elizabeth Thomas, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.