Android library project with tests, step by step

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:

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

  1. Create a new library project as follows:
    android create lib-project -n ExampleLib -t android-8 
    -p examplelib -k com.example.lib
  2. In that project, create src/com/example/lib/ containing:
    package com.example.lib;
    public class Widget {
        public String getColour() {
            return "blue";
        public String getDisposition() {
            return "awesome";
  3. In the examplelib directory, create a test project with:
    android create test-project -m .. -p test -n ExampleTest
  4. Edit the AndroidManifest.xml in the test project and change the line:


  5. Delete the following line from the in the test project:

    and add the following:

  6. Delete the src/com/example/lib/ file.
  7. Create src/com/example/lib/ containing:
    package com.example.lib;
    import junit.framework.TestCase;
    public class WidgetTest extends TestCase {
        private Widget widget;
        protected void setUp() {
            widget = new Widget();
        public void testColour() {
            assertEquals("blue", widget.getColour());
        public void testDisposition() {
            assertEquals("awesome", widget.getDisposition());
  8. Start an emulator and install with ant install.
  9. Run the tests with:
    adb shell am instrument 
    -w com.example.lib.tests/android.test.InstrumentationTestRunner


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 file).

  1. In step 4, we changed the targetPackage. This is necessary because if we leave it as com.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 is com.example.lib.tests
  2. 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 the tested.project.dir line. 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.dir to the current directory (.). This enables ant 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!


Updated to simplify WidgetTest to use JUnit directly instead of InstrumentationTestCase.

11 Responses to “Android library project with tests, step by step”

  1. 1 gfeng November 17, 2010 at 5:28 pm

    Good example.

  2. 2 Santu February 28, 2011 at 6:12 pm

    Very good example…!!!

  3. 3 Neil Traft April 17, 2011 at 4:18 am

    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… πŸ™‚

    • 4 paul April 18, 2011 at 3:15 pm

      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 πŸ™‚

  4. 5 Neil Traft April 19, 2011 at 1:12 am

    I found a better way! You gotta check out Robolectric: Very happy with my workflow now.

  5. 6 paul April 19, 2011 at 10:24 am

    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.

  6. 7 Joe Bowbeer October 21, 2011 at 12:48 am


    Have the details changed in SDK Tools r14?

    Does the run-tests target still exist in r14?

  7. 8 paul October 21, 2011 at 12:53 am

    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.

  8. 10 paul October 28, 2011 at 9:03 am

    r14 and r15 seem to be completely broken 😦 Sigh.

  1. 1 Tools/Frameworks & Recommendation for Android Automation Testing | guanleiwang Trackback on April 10, 2015 at 1:26 pm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: