Warning! Android Tools r14 and (sadly, still) r15 seem to have completely broken the Ant build system with regard to both testing and library support. If you haven’t already upgraded, I strongly suggest that you don’t until these problems are addressed. Here are links to some of the problems:
http://code.google.com/p/android/issues/detail?id=21720 http://code.google.com/p/android/issues/detail?id=21304 http://code.google.com/p/android/issues/detail?id=21299 http://code.google.com/p/android/issues/detail?id=21276 http://code.google.com/p/android/issues/detail?id=21194 http://code.google.com/p/android/issues/detail?id=21108 http://code.google.com/p/android/issues/detail?id=20997
In Testing a library project the Android documentation says:
There are two recommended ways of setting up testing on code and resources in a library project:
- You can set up a test project that instruments an application project that depends on the library project. You can then add tests to the project for library-specific features.
- You can set up a set up a standard application project that depends on the library and put the instrumentation in that project. This lets you create a self-contained project that contains both the tests/instrumentations and the code to test.
How to achieve the first of these is pretty obvious, but the second (to me, at least) rather less so. I wasn’t able to find an example, so I thought that I’d post how I managed to get it working.
Although it does work, I’m not 100% happy with it, and there may well be a nicer way to achieve this (see the end of this post for a discussion about my concerns). I would be very grateful for any suggestions for improvements.
Note, we don’t build with Eclipse (partly because we’ve found its reliability leaves a lot to be desired, but mainly because we need a command line build in order to be able to integrate with the rest of our build system and continuous integration server). So all of the following uses the Android command line tools.
The steps
- Create a new library project as follows:
android create lib-project -n ExampleLib -t android-8 \ -p examplelib -k com.example.lib
- In that project, create
src/com/example/lib/Widget.javacontaining:package com.example.lib; public class Widget { public String getColour() { return "blue"; } public String getDisposition() { return "awesome"; } }
- In the
examplelibdirectory, create a test project with:android create test-project -m .. -p test -n ExampleTest
- Edit the
AndroidManifest.xmlin the test project and change the line:android:targetPackage="com.example.lib"
to:
android:targetPackage="com.example.lib.tests"
- Delete the following line from the
build.propertiesin the test project:tested.project.dir=..
and add the following:
android.library.reference.1=..
- Delete the
src/com/example/lib/ACTIVITY_ENTRY_NAMETest.javafile. - Create
src/com/example/lib/WidgetTest.javacontaining:package com.example.lib; import junit.framework.TestCase; public class WidgetTest extends TestCase { private Widget widget; @Override protected void setUp() { widget = new Widget(); } public void testColour() { assertEquals("blue", widget.getColour()); } public void testDisposition() { assertEquals("awesome", widget.getDisposition()); } }
- Start an emulator and install with
ant install. - Run the tests with:
adb shell am instrument \ -w com.example.lib.tests/android.test.InstrumentationTestRunner
Reservations
It’s clear from the above that the Android tools (the command line tools, at least—perhaps the Eclipse plugin is better?) don’t really fully support this way of working. Not least the fact that they assume that we’re always testing an activity in another application (hence the strangely named ACTIVITY_ENTRY_NAMETest.java file).
- In step 4, we changed the
targetPackage. This is necessary because if we leave it ascom.example.lib, Android will try to launch an application with that package name, which doesn’t exist. However, it’s something of a lie to suggest that the targetPackage iscom.example.lib.tests - In a normal Android test project, we can run the tests by typing:
ant run-tests
We have to run the tests explicitly with
adb, however, because in step 5, we deleted thetested.project.dirline. If we don’t, the ant build system will try to rebuild the library project (and library projects can’t be build independently).An alternative to the solution presented above is to set
tested.project.dirto the current directory (.). This enablesant run-tests, but at the expense of building the project twice.
So, it all works, but it’s a bit messy. Suggestions for improvements very welcome!
Update
Updated to simplify WidgetTest to use JUnit directly instead of InstrumentationTestCase.

Good example.
Very good example…!!!
This post was a lifesaver. I’ve been trying to figure this out all day; I can’t believe how painful it was. Now to see if I can find an Eclipse-friendly way…
Happy I was able to help, Neil.
Please do let us know if you find out how to do this in an Eclipse-friendly way.
Android is comfortably the most test-hostile environment I’ve ever worked in
But we’re gradually beating it into submission
I found a better way! You gotta check out Robolectric: http://pivotal.github.com/robolectric/index.html Very happy with my workflow now.
I looked at Robolectric a while ago, but it was pretty immature. Looks like it’s come on quite a bit since then though
Note, however, that it’s solving a different problem – it’s great if you are confident that you don’t need to run on a real device. For our purposes (developing SwiftKey) a lot of our tests depend upon the intricacies of specific Android versions, so we need to find a way to run our tests there too.
Thanks!
Have the details changed in SDK Tools r14?
Does the run-tests target still exist in r14?
I haven’t had a chance to look at the latest SDK in detail yet, I’m afraid – but from a cursory glance things have certainly changed somewhat. I don’t yet know how it affects the steps in this post.
For update on r14 see comments:
http://code.google.com/p/android/issues/detail?id=21108
r14 and r15 seem to be completely broken
Sigh.