Assertions on Android

If you program in Java on Android, you know that Android has its own virtual machine called DalvikVM which is not compatible with JVM from Oracle. In fact, the difference is so big that you cannot run plain Java application on Android and vice-versa. Moreover, because you cannot run the JVM directly, it is impossible to enable assertions by passing ‘-enableassertions’ or ‘-ea’ parameters. In this post I would like to describe several ways how to go around this issue.

The first method is to verify precondition using if and throw exception (e.g. IllegalArgumentException, NullPointerException or AssertionError) if the condition is not met:

import java.util.ArrayList;

public class Chain {

	private final ArrayList<ChainCommand> chainCommands = new ArrayList<ChainCommand>();

	public void addChainCommand(ChainCommand cmd) {
		if (cmd == null)
			throw new AssertionError("Null command");
		chainCommands.add(cmd);
	}
}

It is less convenient and longer to write than plain assert but it just works out of the box. Of course, you can write your own method which encapsulates it.

Another popular method is to use various assert methods from JUnit Assert class. It is shorter than previous one and also just works.

import java.util.ArrayList;
import junit.framework.Assert;

public class Chain {

	private final ArrayList<ChainCommand> chainCommands = new ArrayList<ChainCommand>();

	public void addChainCommand(ChainCommand cmd) {
		Assert.assertNotNull(cmd);
		chainCommands.add(cmd);
	}
}

The disadvantage of this solution is that assertions cannot be disabled when you want to release your application to public (e.g. on Google Play) unless you use ProGuard to strip them.

Third way (which is what you are probably looking for) is to use assert normally in your code:

import java.util.ArrayList;
import junit.framework.Assert;

public class Chain {

	private final ArrayList<ChainCommand> chainCommands = new ArrayList<ChainCommand>();

	public void addChainCommand(ChainCommand cmd) {
		assert cmd != null : "Command is null";
		chainCommands.add(cmd);
	}
}

and enable assertions on your device (e.g. telephone, tablet or emulator) by setting debug.assert property to 1:

[robert@epsilon ~]$ adb shell setprop debug.assert 1

The assertions will be enabled for all applications until the device is rebooted. There are also few other ways to enable assertions and some of them require the device to be rooted but these three mentioned above should be enough for daily work.

Advertisements

About Robert Piasecki

Husband and father, Java software developer, Linux and open-source fan.
This entry was posted in Defensive programming, Java, Software development practices and tagged , , , , , . Bookmark the permalink.

4 Responses to Assertions on Android

  1. What would be the use-case of using assert statements in production code?

  2. ask says:

    It is really a nice and useful piece of info. I’m happy that you simply shared this useful information with us.
    Please stay us informed like this. Thank you for sharing.

  3. ask says:

    Every weekend i used to go to see this website, for the reason that i want enjoyment, for the
    reason that this this site conations truly nice funny material too.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s