Building an Android app and test project

Skip to end of metadata
Go to start of metadata

Introduction

If you have an Android app project along with its test project in Eclipse and you would like to use Jenkins for Continuous Integration and have FindBugs and Emma coverage trends - this is how it should work.

Assumptions

You have a "standard" Android app and test project in Eclipse. For this document, let's assume the app project is called "android-app" and the test project "android-test", and that your Android SDK is installed in "/data/android-sdk".
This document also assumes that you are checking in your projects to a version control system like Subversion, and that you are checking them out in Jenkins from there.
The procedure described here should work with SDK 14 or above (last tested with SDK 22).

The directory tree should look like

/home/joe/workspace
/home/joe/workspace/android-app
/home/joe/workspace/android-test

Assuming this directory structure then the commands below would be executed from /home/joe/workspace

Set up Ant

  • Install Ant if you don't have it already
  • In your Eclipse workspace, run the following command to set up Ant for the app project:
    android update project -p android-app
    
  • Then run this command to set up Ant for the test project. Note that the path given to -m must be the relative path to the project under test, seen from the test project, not the workspace:
    android update test-project -m ../android-app -p android-test
    
  • If you have any library projects that need to be set up for and use this
    android update lib-project --target "Google Inc.:Google APIs:13" --path JakeWharton-ActionBarSherlock/plugins/maps
    
  • Refresh the projects in Eclipse
  • Note: The file "local.properties" contains local configuration, like the location where your Android SDK is installed. Since this location can be different on i.e. Jenkins build machines, this file should not be checked in to version control. So it is recommended to add "local.properties" to the "Ignored Resources" in Eclipse right away.
  • The files "project.properties" and "ant.properties" (test project) are required and must be checked in to version control.
  • Test if you can build the app project with
    ant clean debug
    
  • Make sure that you have a (virtual) device (only one at a time!) running, and see if you can build the test project and run the tests:
    ant clean emma debug install test
    
    If you receive an error like "Failed to generate emma coverage. Is emma jar on classpath?", try to build the test project with "ant all clean emma debug install test", this might fix it.
  • This will take a bit longer. Eventually, you should find a "coverage.html", "coverage.xml" and "coverage.txt" in the "bin" folder of your test project, containing the test coverage.
    If you can only find a "coverage.html" in your project (probably in a "coverage" folder), follow the steps in the "Emma" section below, otherwise skip it.

Emma

This section can be skipped if the build creates a "coverage.html", "coverage.xml" and "coverage.txt" in the "bin" folder of your test project, which should be the case with at least SDK 22 or later.

If you want to build your projects in Jenkins and use the Emma plugin to see the test coverage and trend, you cannot use the "coverage.html" file as it currently is generated by the "coverage" Ant task and you have to change it to create a "coverage.xml" instead:

  • Go to your Android SDK installation, and open the file "tools/ant/build.xml", copy the complete "test" target and paste it in the "build.xml" of the test project, before the import of build.xml, so before the comment starting with "Import the actual build file". There is more information on how to customize targets in that comment.
  • Change the version-tag from "1" to "custom" to avoid the file being overwritten by future runs of "android update project":
    <!-- version-tag: custom -->
    
  • Note: Eclipse might complain about your build file. Since I didn't know how to get rid of these errors and warnings, I just set "Ignore all buildfile problems" in the "Ant Editor settings" in Eclipse's preferences.
  • In the copied "test" target in your build.xml, make the following changes:
  • Change
    <html outfile="coverage.html" />
    
  • to
    <html outfile="coverage.html" />
    <xml outfile="coverage/coverage.xml" />
    
  • Adjust the last "echo" message accordingly (just for the sake of correctness):
    <echo>Saving the report file in ${basedir}/coverage/coverage.html and ${basedir}/coverage/coverage.xml</echo>
    
  • When you now run "ant clean emma debug install test" again, you should have a coverage.xml file, ready to be used by Jenkins!
  • Note: The Emma XML report does not provide "highlighting individual source line coverage states" as the HTML report does. So we make both reports. See the ANT section in the emma manual http://emma.sourceforge.net/userguide_single/userguide.html

FindBugs

  • If you don't already have FindBugs installed, download the .zip or .tar.gz from http://findbugs.sourceforge.net/downloads.html. Let's assume that you install it to "/data/findbugs".
  • Copy "lib/findbugs-ant.jar" from your FindBugs installation to the "lib" folder of your Ant installation.
  • Add the location of your FindBugs installation to the "local.properties" file in you app project:
    findbugs.home=/data/findbugs
    
  • If you want to exclude FindBugs warnings for the R.java generated source file, create a file "findbugs-exclude.xml" in your app project folder with the following content:
    <?xml version="1.0" encoding="UTF-8"?>
    <FindBugsFilter>
    	<Match>
    		<Class name="~.*\.R\$.*"/>
    		<Bug code="Nm"/>
    	</Match>
    </FindBugsFilter>
    
  • Add the following taskdef and target to the "build.xml" of your app project:
    <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask"/>
    
    <target name="findbugs">
    	<mkdir dir="reports" />
    	<findbugs home="${findbugs.home}" output="xml" outputFile="reports/findbugs.xml" excludeFilter="findbugs-exclude.xml">
    		<auxClasspath path="${android.jar}" />
    		<class location="${out.dir}" />
    	</findbugs>
    </target>
    
  • Change the version-tag from "1" to "custom" to avoid the file being overwritten by future runs of "android update project":
    <!-- version-tag: custom -->
    
  • To create a FindBugs report, run your app project like this:
    ant clean debug findbugs
    

    You should get a folder "reports" containing a "findbugs.xml" file.

  • Note, it may be necessary to add JARs your code depends on with additional "auxClasspath" elements, but I am not really sure.
  • If you don't want to use the "findbugs-exclude.xml" described above, you have to remove the "excludeFilter" attribute.
  • You might want to add the "reports" folder to the version control "Ignored Resources" as well.

Jenkins

Create a "free-style software project" for each the app project and the test project.

Configure the app project

  • Add an "Invoke Ant" build step and set "Targets" to
    clean debug findbugs
    
  • Under "Advanced", add the following properties and adjust the paths according to the Android SDK and FindBugs installation location of the machine where Jenkins is running on:
    sdk.dir=/data/android-sdk
    findbugs.home=/data/findbugs
    
  • Under "Post-build Actions" check "Publish FindBugs analysis results". It should not be necessary to make any settings there.
  • You should now be able to build the project, and the FindBugs trend should be visible after at least 2 successful builds.

Configure the test project

  • Check out both the app and the test project, so that they are besides each other in the Jenkins job's workspace, similar to the Eclipse workspace
  • Add an "Invoke Ant" build step and set "Targets" to
    clean emma debug install test
    
    For the SDK 14 use "clean emma debug" and a second task "ant emma installt test" http://code.google.com/p/android/issues/detail?id=20997
    Also fix your build.xml as described here: http://code.google.com/p/android/issues/detail?id=20979&can=1
  • Under "Advanced", add the following properties and adjust the paths according to the Android SDK installation location of the machine where Jenkins is running on and adjust "tested.project.dir" if necessary:
    sdk.dir=/data/android-sdk
    
  • Also under "Advanced", it may be necessary to specify the relative path to the "Build File" of the test project:
    android-test/build.xml
    
  • Under "Post-build Actions", check "Record Emma coverage report". In "Folders or files containing Emma XML reports", I filled in:
    **/bin/coverage*.xml
    
    If coverage files are placed in a "coverage" folder in your project, the path should be "**/coverage/coverage*.xml"
  • You should now be able to build the project and the Emma coverage trend should be visible after at least 2 successful builds.

Happy building!

Labels

Edit
android android Delete
emma emma Delete
findbugs findbugs Delete
ant ant Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Add Comment