I am writing my first Android application and I am liberally using asserts() from junit.framework.Assert
I would like to find a way to ensure that the asserts are on
I think the Java language's assert
keyword is likely what you want. Under the covers, the assert
keyword essentially compiles into Dalvik byte code that does two things:
assertionsDisabled
(set in the class' static constructor via a call to java.lang.Class.desiredAssertionStatus()
) is != 0 and if so, does nothingjava.lang.AssertionError
if the expression resolves to false
, effectively terminating your application.The Dalvik runtime by default has assertions turned off, and therefore desiredAssertionStatus
always returns 1 (or more precisely, some non-zero value). This is akin to running in "retail" mode. In order to turn on "debug" mode, you can run the following command against the emulator or the device:
adb shell setprop debug.assert 1
and this should do the trick (should work on the emulator or any rooted debugging-ready device).
Note however that the aforementioned Dalvik code that checks the value of assertionsDisabled
and throws an AssertionError
if the expression is false is always included in your byte code and liberal sprinkling of assert
s in your code may lead to byte code bloat.
Please see this for a bit more detail: Can I use assert on Android devices?