android - better way to do Debug only assert code -
i writing first android application , liberally using asserts() junit.framework.assert
i find way ensure asserts compiled debug build, not in release build.
i know how query android:debuggable attribute manifest create variable , accomplish in following fashon:
static final boolean mdebug = ...
if (mdebug) assert.assertnotnull(view);
is there better way this? i.e. prefer not use if() each assert.
thanks
i think java language's assert keyword want. under covers, assert keyword compiles dalvik byte code 2 things:
- checks whether static variable
assertionsdisabled(set in class' static constructor via calljava.lang.class.desiredassertionstatus()) != 0 , if so, nothing - if is 0, checks assertion expression , throws
java.lang.assertionerrorif expression resolvesfalse, terminating application.
the dalvik runtime default has assertions turned off, , therefore desiredassertionstatus returns 1 (or more precisely, non-zero value). akin running in "retail" mode. in order turn on "debug" mode, can run following command against emulator or device:
adb shell setprop debug.assert 1 and should trick (should work on emulator or rooted debugging-ready device).
note aforementioned dalvik code checks value of assertionsdisabled , throws assertionerror if expression false included in byte code , liberal sprinkling of asserts in code may lead byte code bloat.
please see bit more detail: can use assert on android devices?
Comments
Post a Comment