How to use assert in android?

JavaAndroidAssert

Java Problem Overview


I want to use assert obj != null : "object cannot be null" on Android device. The assert doesn't seem to work, so I searched online and I found this local solution:

> adb shell setprop debug.assert 1

it does work on my local machine.

I want to run this command using my Eclipse project(so it would be in the source control). How can I do it?

Java Solutions


Solution 1 - Java

Assert won't work in Android because most of the time a person isn't running in debug mode, but rather some optimized code. Thus, the proper solution is to manually throw an exception, with code like this:

if (obj==null) throw new AssertionError("Object cannot be null");

It should be noted that by design, Asserts are intended for debug code, and not for release time code. So this might not be the best use of throwing an Assert. But this is how you can do it still, so...

Solution 2 - Java

Tested on Android 4.x device, it is possible to use Java assert on Android device:

  • Edit /system/build.prop (for example by X-plore), add line at end of file: debug.assert=1
  • Reboot phone

Now your Android device is sensible to assert checks, and will throw AssertionError when assert check fails.

EDIT:

Another easy approach, enabling asserts from PC until device is restarted:

platform-tools\adb shell setprop debug.assert 1

You may for example create a .bat file (on Windows) and run it when device is attached.

Solution 3 - Java

Create your own assert method:

public static <T> T assertNotNull(T object) {
    if (object == null)
        throw new AssertionError("Object cannot be null");
    return object;
}

Returning same object allows for using this in assignments for brevity.

Solution 4 - Java

if (somevar == null) throw new RuntimeException();

Replace RuntimeException() with an appropriate exception subtype.

Solution 5 - Java

Sharing my class which I use for assertions on Android, its simpler, has nice naming and very elegant because it allows you to write asserts like this:

Assert.that(obj!=null, "Object should not be null");

Here's the code for the class:

public class Assert {
    public static void that(boolean condition, String message) {
        if (!condition) {
            throw new AssertionError(message);
        }
    }
}

Hope it helps!

Solution 6 - Java

To make assertion available in Java Virtual Machine, using -enableassertions or -ea command-line options

  • on Window's Command prompt, >java –ea YourApp
  • on Android Studio 3.0, on Tools/Edit Configurations/VM options, input -enableassertions or -ea

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdibe7View Question on Stackoverflow
Solution 1 - JavaPearsonArtPhotoView Answer on Stackoverflow
Solution 2 - JavaPointer NullView Answer on Stackoverflow
Solution 3 - JavaPaweł NadolskiView Answer on Stackoverflow
Solution 4 - JavamahView Answer on Stackoverflow
Solution 5 - JavapragmanView Answer on Stackoverflow
Solution 6 - JavaShuyan JiView Answer on Stackoverflow