Android Tips: Generating a coverage report for your unit tests

The Android testing framework has the built-in ability to generate a code coverage report for your unit tests. Unfortunately this functionality is not available within the Eclipse IDE, so you have to resort to some command line and Ant fu.

First off, you need to create a test project for your Android app and some unit tests.

Next, run the following command:

android update test-project -m <path to app project> -p <path to test project>

This will create a Ant build.xml file in your test project’s top-level directory. The android tool can be found in the tools folder of your SDK installation.

Now, from your test projects top-level directory, run the following command:

ant coverage

This will build, instrument, deploy and run your unit tests. It will also generate a HTML coverage report.

The location of the report will be displayed in the last few lines of the Ant script’s output. Point a web browser at the coverage report and weep :)

UPDATE: if you are using SDK 14 or later then the command to run is now:

ant emma debug install test

See the SDK Ant command reference for more details

Android Tips: The emulator

In the last two months I have written two Android apps, in doing so I have uncovered a number of tricks and quirks which I thought I’d share. To start with, the Android emulator…

Keyboard shortcuts

Surprisingly, the GUI for the emulator does not provide controls for a number of functions such as emulating a change in orientation, instead you need to use keyboard short-cuts.

Two particularly useful ones are:

  • 7 on your keypad (or CTRL+F11) which cycles through portrait and landscape layouts.
  • F8 which toggles cell networking on and off

Connecting to the emulator console

To connect to the emulator console you need to telnet to the emulator’s console port on localhost. The console port is displayed in the title bar of the emulator window and is normally 5554.

telnet localhost 5554

You can also use the  adb command (in the tools folder of the SDK install) to list out all the running emulators and their ports:

adb devices

Emulating power state

Through the emulator console you can easily manipulate the power state of the emulator. To see the current state type:

power display

To emulate unplugging the device from a power source, type:

power ac off

Note: for some reason this does not seem to change the charging state of the emulated device.

To “plug” the device back in:

power ac on

To set the battery level to 10% use:

power capacity 10

Note: This is incorrectly documented in the SDK documentation.

There are a bunch of other commands that can be run through the emulator which allow simulation of  network connectivity & latency, telephony, SMS services etc.

Accessing the shell

Using adb you can get access to the shell of the OS running within the emulator. This allows you to execute some of the low level apps installed on the  device. To access the shell type:

adb shell

Since Android is running on a flavour of Linux you can move around the using ls and cd commands. Type the following to see where the native executables are installed:

echo $PATH$

Most of the useful applications can be found in /system/bin. Run this to see what you have access to:

ls /system/bin

Going monkey on your app

Within the shell you can run the Monkey stress tool. This tool emulates random button mashing, phone rotations and screen touches to stress out your app. Unleash monkey with the following command:

monkey -p -v 500

Turning Japanese

Sometimes when you run Monkey it will change your IME, often to the Japanese one. To fix this long press on an input field, then select Input Method and then Android Keyboard from the pop-up options.

Setting the emulator’s date & time

Via the shell you can set the emulator’s date & time which is particularly useful for testing time based logic. Unfortunately the date command only accepts the number of seconds since 1970-01-01 00:00:00 UTC as input, which is a little tedious to use. However you can use the following command (which I found on Diego Torres Milano’s blog) to set the date from your command prompt (if you are running a unixy operating system):

adb shell date $(date --date="2008-01-31 17:46:59" +%s)

Note: This spits out an error message saying “settimeofday failed Invalid argument” but seems to work anyway.

Wrap up

Hopefully this has been useful, my next post will cover Preferences and how to use them in your apps. Be sure to check out the Android SDK tools documentation for more info.

MeterRec released

Icon for MeterRecI’ve just released my first purchasable app on the Android market.

The app is called MeterRec and it allows you to easily record readings for gas, water and power or other similar meters.

These readings can then be exported as a .csv file for manipulation and graphing in applications such as Excel.

This app is ideal for property managers, flat mates or those (like me) who just want to keep tabs on what they are using.

If you have an Android phone then point your barcode scanner at the QR code above, to get a link to the app in the market. For those of you without an Android device, here are a couple of screenshots: