80

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?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Adibe7
  • 3,469
  • 7
  • 30
  • 36
  • 1
    possible duplicate of [Can I use assert on Android devices?](http://stackoverflow.com/questions/2364910/can-i-use-assert-on-android-devices) – Flow Mar 21 '14 at 09:44
  • Define "doesn't work". Do you mean it has no apparent effect? Or it has an effect but different from what you expected? – LarsH Mar 30 '18 at 16:27

6 Answers6

93

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

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • 6
    What about http://stackoverflow.com/a/5563637/632951 and http://stackoverflow.com/a/8164195/632951 ? – Pacerier Jan 18 '12 at 05:01
  • 3
    Someone please tell me that this code should read if (obj == null) – Lo-Tan May 24 '13 at 19:05
  • 1
    warning to copy pasters: this should read "throw", not "thrown" – chrisben Dec 18 '13 at 09:59
  • 2
    I'd make this throw AssertionError instead of Exception. – Ellen Spertus May 15 '14 at 21:26
  • Good idea, changed accordingly. – PearsonArtPhoto May 15 '14 at 21:28
  • 4
    Your answer is wrong, this is not what asserts are for. Asserts are automatically removed from the release code, where your condition won't be. You can use a similar if-condition combined with BuildConfig.DEBUG flag, like so: if(BuildConfig.DEBUG && obj == null) throw ... BuildConfig.DEBUG is a compile time constant, so compiler can remove such conditions in the release code entirely. However, BuildConfig.DEBUG is never set for lib projects, so again this make the whole thing a little bit useless. – b005t3r Aug 14 '17 at 11:02
  • Yes... if only people would understand what assertions are for. – funct7 May 08 '19 at 20:40
13

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.

Pointer Null
  • 39,597
  • 13
  • 90
  • 111
  • 1
    I set "platform-tools\adb shell setprop debug.assert 1" but idk why, it's not throw any exception when reach the assert line. Do we need to use debug mode? – HendraWD Jun 09 '16 at 09:56
  • 1
    Tried with debug mode, but still the same. My android version is 5.1.1 and it is rooted, idk if that can be the problem – HendraWD Jun 09 '16 at 10:07
  • @HendraWD: Not working in non-rooted Nexus 5 either. – Daniel Feb 15 '19 at 02:40
10

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.

Paweł Nadolski
  • 8,296
  • 2
  • 42
  • 32
5

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!

pragman
  • 1,564
  • 16
  • 19
5

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

Replace RuntimeException() with an appropriate exception subtype.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
mah
  • 39,056
  • 9
  • 76
  • 93
1

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
Shuyan Ji
  • 121
  • 2
  • 3