Android Continuous Integration with ANT and Jenkins: Part 2

Share This

In my recent post I wrote about how to build and run unit tests on Android project from command line. These were first steps to setup Continuous Integration for your project, and now I’ll show you how to automate a build with Jenkins.

First of all, you’ll need Jenkins CI server installed on your build server (in BlackRiver, we are using Windows Server virtual box for Jenkins main server).

Since we are going to build Android project on CI server, we’ll need Android SDK (don’t forget to install appropriate SDK version using SDK Manager!)  and  Android Emulator Plugin for Jenkins. Also, you’ll probably need to integrate Jenkins with your version control system, we are using a Git Plugin for that.

Let’s start configuring Jenkins.

Step 1. System configuration.

Go to Jenkins system configuration screen: Manage Jenkins -> Configure System and set all required paths and variables (e.g. path to git, path to Android SDK, path to ANT and so on)

Step 2.  New Job.

Create new Jenkins Job (New Job -> Build a free-style software project)

Step 3. Checkout code from Version Control. 

Open newly created Job and go to its Configure screen, locate Source Code Management section and enter required paths and credentials. I’m using Git and github, so I’ve checked Git radio button and entered URL from github: http://username:[email protected]/PATH_TO_YOUR_REPO.git. Note: It is a good idea not to paste your own username and password in Jekniks, just create new user, which has access only to this project repository.

Now, you can apply changes and Jenkins will have access to your repository: just click on Build Now link, you’ll see that your project is being build (at the bottom of left sidebar you’ll notice the link to current Build). Open Console output to review the detailed log.

Alright, now we have the source code checked out from our version control repository, it is time to build the project!

Step 4. Configure Jenkins to build project with ANT. 

Go to configure screen in your Jenkins job and locate section called Build. Click on “Add build step” dropdown located at the bottom of the section and pick the item called Invoke Ant : new build step will be added to the Build section.

Make sure you’ve provided correct Ant settings in Jenkins system configuration and select Ant installation from dropdown in Invoke Ant build step.

For now, we’ll just want to build the project and see if configuration is correct, so just type:

clean debug

This will build debug version of you app.

Time to check if we made everything right: hit Build now link and wait while your project is being built by Jenkins.

Note: If you get an error regarding your build.xml file location, there is a trick: by default, Jenkins will look into workspace root for build.xml. You might have this file in your project folder, so Ant task will fail with message:

FATAL: Unable to find build script at _PATH_TO_YOUR_WORKSPACE_workspacebuild.xml
Build step 'Invoke Ant' marked build as failure
Finished: FAILURE

to fix that and point Jenkins to your build.xml, press Advanced button on Invoke Ant task and you’ll see a text field named Build file, type relative (from project/workspace root) path to build.xml. For example:

./projectname/build.xml

and now Ant will find build file.

 

Step 5. Run android emulator to run tests on. 

Jenkins is able to manage AVDs (Android Virtual Devices) which you can use to run unit test, functional tests or other kind of tasks.

Android Emulator Plugin for Jenkins provides easy to use interface which can be used in Jenkins build job.

I’ll show you how to run emulator during Jenkins build, how to run unit tests (JUnit) and how to calculate code coverage.

Let’s start with simple ACD setup: locate the Build Environment section in the Job config screen, check “Run an Android emulator during build”, pick an option preferable for you: “Run an existing emulator” or “Run emulator with properties” like I did:

 

Note: if you don’t have any downloaded Android SDKs, Jenkins will download and install them for you, just check Console Output while running the Job for details. It may take a while for Android SDK to be downloaded and installed, though, but it is the SDK manager issue.

If you are using some kind of functional testing tool (monkeyrunner, robotium or any other) which requires the app to be installed on the emulator, don’t forget to add build step, which will install APK onto the emulator:

 

 

 

 

 

This time I’ll skip functional testing, but I’ll cover that topic in my future articles.

Now, let’s add the build step which will run JUnit tests and calculate code coverage with Emma tool:

 

 

 

 

 

 

 

and if Jenkins won’t find build.xml, don’t forget point to it on the Advanced section of the build step, just like we did while we were building the main app.

Save and build your project, it will take much longer than simple compilation – open the Console output screen to see what is happening.

I hope that you build went well, compilation succeeded and all the tests passed. But it is rather uncomfortable to scan through the Console output screen to see if tests passed or build succeeded, isn’t it?

Step 6. Publish JUnit and Emma code coverage reports.

Before we started with Jenkins configuration, I should show you a little trick. As you may already know, Android JUnit tests are executed on the device or emulator and the sad thing is that JUnit stores its reports on the device, which means Jenkins is not able to grab them and build a report for you.

So, we’ll just add a simple ANT task which will download JUnit report files from device/emulator and store them in the workspace folder. I won’t bother you with the details – here is the code of the ANT task which will download JUnit report from device using adb tool:

<target name=”fetch-test-report”>
<echo>Downloading XML test report…</echo>
<mkdir dir=”junitreports”/>
<exec executable=”${adb}” failonerror=”true”>
<arg line=”${adb.device.arg}”/>
<arg value=”pull” />
<arg value=”/data/data/${tested.manifest.package}/files/junit-report.xml” />
<arg value=”junitreports/junit-report.xml” />
</exec>
</target>

Now we have all required reports to publish.

Locate this Post-build Actions section and add two actions: Publish JUnit result report and Record Emma coverage report, provide path to reports relative to the workspace folder:

 

Now you can build your project  and on the build details page you’ll have pretty graphs with info about unit tests and code coverage:

 

One tiny detail left: you probably want to be notified when the build is ready, Jenkins can handle this job pretty well, just add post build action called E-mail Notification. This action will be triggered when the build is finished (regardless of result) and recipients listed in corresponding field will receive an email about recent build.

You can set up more complex notification system with the Email-ext plugin for Jenkins.

Time to finish this pretty long article, thank you for reading!

Next time I’ll show you how do we maintain development, QA and production processes with Jenkins.


22 Responses to “Android Continuous Integration with ANT and Jenkins: Part 2”

  1. […] the next articles, I’ll show you how to set up ANT to publish code coverage and JUnit reports, run Android ANT […]

  2. singhraminder says:

    Hi,
    Great post.
    Can you please provide update about plans for next article?

    regards
    singhraminder

  3. allen says:

    great job ,hope you can keep update

  4. al says:

    I do not have the Git option under Source Code Management. Am I missing a plug-in or something? I only see CVS, none, and Subversion

  5. Ston says:

    Where do I put that Ant task code on Step 6. Publish JUnit and Emma code coverage reports.??

    • Andrey Zavarin says:

      You should put the “fetch-test-report” task described in step 6 to the same build step as “test” ant task.
      So your ANT task list will look like “all clean debug install test fetch-test-report”

      • Daniel Ochoa says:

        Do I add the “ant task” specified in Step #6 in the build.xml of the main project or the test project?

        • Max says:

          Hi.

          First of all, thank you for the great tutorial.
          I have build a simple HelloWorld-Project (https://github.com/MaxBatt/HiAll)
          and an according test-project (https://github.com/MaxBatt/HiAllTest)

          I was able to follow your tutorial until step 5: the project can be built, deployed to device or emulator and tested successfully.

          But i am stucked within step 6.
          Generally I have the same question like Ochoa. I am not sure where to put the ant-tast in the build.xml. No matter if I put it in the build.xml of the original project or the test-project, after building i get the error:
          ‘ Target “fetch-test-report” does not exist in the project “MainActivityTest”. ‘

          Could you please have a look at my build.xml, located here?
          https://github.com/MaxBatt/HiAllTest/blob/master/build.xml

          Thanks a lot!
          Max

  6. Mo says:

    Thanks for your article, I am trying to setup the jenkins for our android projects as well, when I use a device and run “all clean debug install test fetch-test-report” it works fine. but it fails when I use an emulator.

    [exec] remote object ‘/data/data/com.example/files/juni
    t-report.xml’ does not exist

    I was wondering if anyone has came across this issue.

  7. Burak says:

    Hello,

    are you sure that the default android test classes generate a test report in a XML file located at /data/data/project_name/files/junit-testreport.xml ?

    I get the same error like Mo above me

    remote object ‘/data/data/com.example.android.apis/files/junit-report.xml’ does not exist

    I also tested it manually and pulled completly /data/data and it seems that it doesn’t generate a directory for the project. So I don’t get a junit report, too.

    On this website (http://www.alittlemadness.com/2010/07/14/android-testing-xml-reports-for-continuous-integration/) I read about a custom class which generate the report file. Did you take this from there or are you sure that default android classes do this too?

    greets,
    Burak

  8. Alex Lee says:

    Thanks a lot! It’s very helpful

  9. Max says:

    Hi.

    First of all, thank you for the great tutorial.
    I have build a simple HelloWorld-Project (https://github.com/MaxBatt/HiAll)
    and an according test-project (https://github.com/MaxBatt/HiAllTest)

    I was able to follow your tutorial until step 5: the project can be built, deployed to device or emulator and tested successfully.

    But i am stucked within step 6.
    Generally I have the same question like Ochoa. I am not sure where to put the ant-tast in the build.xml. No matter if I put it in the build.xml of the original project or the test-project, after building i get the error:
    ‘ Target “fetch-test-report” does not exist in the project “MainActivityTest”. ‘

    Could you please have a look at my build.xml, located here?
    https://github.com/MaxBatt/HiAllTest/blob/master/build.xml

    Thanks a lot!
    Max

  10. Max says:

    To configure your build.xml with the usage of Emma you can follow this instruction:

    https://wiki.jenkins-ci.org/display/JENKINS/Building+an+Android+app+and+test+project

  11. MRB says:

    The project builds and runs on emulator. But all test cases are failing? Though they pass in local environment. Do you have any idea? Is emulator needs to be unlocked manually?

  12. Neil says:

    Should we have one Jenkins job which includes both the app and test projects or one ‘app’ job and one ‘test’ job which follow on from each other?

    If one job should we tell it to use the test project’s build.xml and expect that to build the app project via Android’s ant’s dependencies? I guess this also assumes you have both projects in one git repo as the job’s git SCM options don’t let you specify a target directory.

    If separate jobs how do we get the test job to reference the built output from the app job? If both projects are included in the git clone it’ll rebuild the app project again, wasting the time spent on the first build.
    Also need to reference the classes if there are junit tests.

    Great articles thanks, very helpful!

  13. Nithya says:

    if build.xml is not found in the workspace what should i do?

  14. sometimes i got same problem lke you

Leave a Reply

Recent Revive AdServer (Formerly OpenX Source) Expandable Banners

Revive AdServer (Formerly OpenX Source)  Expandable Banners The following example demonstrates a 600px by 150px banner served by Revive AdServer (Formerly OpenX Source)  and expanded to 600px by 300px on rollover. The flash creative can be either uploaded to creatives directory directly (FTP) or just as an another Revive AdServer (Formerly OpenX Source)  banner (preferred). When uploading the SWF creative, you do not need to replace any hardcoded URLs or indicate a destination URL – that would be done in the HTML banner setup. Essentially, we are just using it as a storage container for our creative, all impressions and clicks will be … read more

 Twitter  LinkedIn  Google+  Skype RSS

Get in Touch

  • r Phone:
    (416) 877 2844 / (647) 258 4847
  • h Email:
    [email protected]
  • m Address:
    1454 Dundas St. East, Suite 124
    Mississauga, Ontario
    L4X1L4, Canada

Any Questions?

    Email

    Message