Running Android unit tests in Intellij

It turns out that running Android unit tests in Intellij is not as easy as creating a unit test in the [ANDROID_MODULE_HOME]/src folder.

You instead need to create a new “Android test module” inside your main module and run your tests from there:

If you choose the default options, your new test module (“myAndroidTestProject”) will be created inside the main module (“myAndroidProject”):

This test module has a dependency on the main module:

And the nice thing is that Intellij automatically creates an Android unit test configuration that automatically executes all the unit tests in this new test module:

From there, you can create your unit tests in this new module. Here is a simple one:

public class MyAndroidTestCase extends AndroidTestCase {

public void test1() throws IOException, JSONException {
 Log.d(this.getClass().toString(), (">> test1"));
 Context ctx = getContext();
 FileOutputStream fos = ctx.openFileOutput("test.dat", Context.MODE_PRIVATE);
 fos.write("hello the world".getBytes());
 assertTrue(Arrays.equals(new String[]{"test.dat"}, ctx.fileList()));
 Log.d(this.getClass().toString(), ("<< test1"));

Note that Android unit tests cannot be directly ran from Intellij: you need to run them from the emulator (or from a real device):


Debugging an Android x86 application using Intellij

OK, you decided to drop the incredibly slow Android emulator that comes with the official SDK and you are now happily using Android x86… but how do you debug your Android application using Intellij ?

That’s a good question ! (By the way, if you may want to check my last post if you need help running Android x86 in VMWare player).

First, you need to know the IP of your Android x86 VM. To do this, hit ALT-F1 in the VM and use the ‘netcfg’ command to know the IP address given to your eth0 device:

You can then hit ALT-F7 to go back to the Android UI.

Then, in your host PC (the one that is running Intellij), execute the ‘adb connect [ANDROID_X86_IP]” to connect the SDK debugger to your Android x86 VM:

Now go back to Intellij, display your run configuration and make sure that the “show chooser dialog” option is selected:

You are almost done: add a breakpoint in Intellij, launch the application in debug mode and then select your Android x86 device in the “choose device” dialog:

Any question ?