Android Continuous Integration with ANT and Jenkins: Part 1
Recently we’ve started a new Android project for one of our customers, and since we are using a proper development workflow, I’ve began with setting up Continuous Integration (CI) as a part of the process.
I’m going to tell you a little more than just the technical details about how to set up CI server and build an application there, what I’ll tell you is how we, at Black River, manage development and testing processes using Jenkins.
In this article, I’ll cover following topics:
- How to create an ANT build script for the Android app
- How to run unit tests from the Android test project with ANT
- How to setup Jenkins CI for your Android project
- How to integrate Jenkins with Github
- How to make a release build and send the application to QA team
Let’s start with creating ANT build script, based on your eclipse project.
I assume, since you are going to setup CI for Android, you already have Android SDK installed.
First of all – get the latest version of Apache ANT here – http://ant.apache.org/bindownload.cgi and unpack it somewhere on your computer.
See Apache ANT website for installation details, in essence, it says that you need to add a few environment variables:
- ANT_HOME – pointing to directory where you’ve unpacked ANT
- ANT_OPTS – additional ANT settings (for example, you can limit the amount of memory that ANT may consume)
- add %ANT_HOME%bin to your PATH variable
Now, if you run “ant” tool in your console (cmd.exe in Windows, terminal in *nix, etc), you’ll see something like the followin:
Buildfile: build.xml does not exist!
Build failed |
Okay, now we’re ready to setup our ANT build scripts.
Go to your project directory with console and run the following command:
android update project -p . |
Note that “.” (dot) after -p (path to project) flag, means “current folder”.
It will update and rebuild your Android project files and create build.xml file, which will be used by ANT to build your project.
Then go to your test project folder with console and run update command for the test project:
android update test-project -m _PATH_TO_ANDROID_PROJECT -p _TEST_PROJECT_PATH_ |
At this point, we have two projects with prepared and build.xml files.
Let’s try to build them!
Go to your project folder and run:
ant clean debug |
This command should start compilation.
Afterwards, cd to your test project folder, start Android emulator and run in console:
ant all clean emma debug install test |
This command will build the test project, run code coverage report with EMMA tool, install APK into emulator and run unit tests with JUnit on device
You’ll see the code coverage and unit tests results in your console window.
Okay, now we know how to build Android project and run tests from console with ANT tool.
It is a good start for build automation and as a result – continuous integration for development process.
In the next articles, I’ll show you how to set up ANT to publish code coverage and JUnit reports, run Android ANT build with Jenkins CI server, get code from Github (or any other Git server) and run a couple of other useful and clever tasks.
Nice start 🙂 Though I am really interested in the further parts of this series. Do you know when will those be published?
This looks promising, thanks. When will the future parts be published?
[…] my recent post I wrote about how to build and run unit tests on Android project from command line. These were […]
When I try to run:
ant all clean emma debug install test
I get an error that says:
“Target “all” does not exist in the project ”
Any idea what’s wrong?
Make sure that path to android sdk in local.properties file is correct. It looks like ANT cannot find main build.xml which is provided with Android SDK.
Hello,
I have excatly the same problem. The local.properties contains the correct path to sdk directory. Also the ANDROID_HOME environment variable is set. But I get always the message
“Target “all” does not exist in the project”
Seems like its connected to newer versions of the sdk tools (see https://code.google.com/p/android/issues/detail?id=35784)
Just try to run the code without “all”
Hi,
I have a question. Why build didn’t fail when tests fail?
My unit test are failing but my build is success!
Thanks,
Alex
Hm, that is weird, make sure that “test” task is not overridden with something else. Default “test” task from build.xml provided with Android SDK should fail the build, if unit tests are not successul.
Which kind of test-project did you create?
Thank you for providing the clearest instructions for using eclEmma, Android Junit Tests (Robotium, in my case), ANT and Eclipse. I did have to open an emulator first because it kept stalling and removed “all” from the instructions to the tester. All other instructions found on the interwebz were not as straightforward (an not as user friendly to Command Prompt noobs). Thanks!
when i type
android update project -p.
it is saying android : command not found
[…] Jenkins ou Windows. Outra fonte interessante é esse post do Black River, falando desta vez sobre integração contínua com ANT e Jenkins. Para finalizar Android e partir para o iOS, deixamos aqui esse vídeo do Google I/O que fala sobre […]
Your notes have a small but important error – the path setting should have a slash before the bin – “%ANT_HOME%\bin” not add %ANT_HOME%bin to your PATH variable
Thanks for good Page. How to Exclude some classes for code coverage? there any way to exclude and use “ant clean emma debug install test” ?
Hi,
I have a problem when trying to make the “android update project -p .”. It said “… is not a valid project (AndroidManifest.xml not found). It seems that there are sub-projects. If you want to update them please use the –subprojects parameter.” Am I making the calls from wrong directories? I made the call from the outmost folder of my project (C:\Users\Christian\PCDS2015\Care2Reuse), hence not where the manifest is, which is in C:\Users\Christian\PCDS2015\Care2Reuse\app\src\main.
I am using the Facebook sdk as a modul in my project, so im not sure if it is this which it sees as a subproject.
Sorry for the rant, thanks in advance!
Hi,
can you please provide an build.xml file sample for android continious integration using Ant.
[…] various use-cases. Automatic daily builds are made using the Jenkins continuous integration server (read more about it in our blog). To support testing on multiple devices, we have engineered a custom Jenkins CI cluster which can […]