How to use assert in android?
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...
How to use assert in Android Studio
The adb shell setprop debug.assert 1
from your referred question is to be executed on the device you are testing on, so you could whip up a script or even create a custom gradle task for that (gradle docu).
In general I'd recommend to also have your checks in production and handle them in a proper way. A simple solution would be to throw a RuntimeException
. With checked Exception you could even handle the recovery from this errorneous states / misuses of your api.
Furthermore, it would make sense to add proper tests that ensure, that your code/APIs only emit "valid" values that can be handled by the rest of your code.
Assertions Android Programming
Since the question was if it is possible... Yes it is possible but I go along with CommonsWare that asserions should be avoided.
Anyway see Can I use assert on Android devices? for how to use Assertions on Android
Are asserts available on Android?
Yes, they are available.
They are disabled in emulator by default. You will need to add -shell -prop debug.assert=1
command line parameters to Additional Emulator Command Line Options at the run configuration you're using to run your app.
The other thing you should be aware of is that your application installed on a device will not take into account assertions - they will be ignored.
Android Studio assert not working
Apparently, (as stated in the comments) assertions are disabled by default but encouraged by Android Studio.
In this manner, the "problem" can be classified as a misleading UX issue rather than a technical problem.
better way to do Debug only assert code
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:
- Checks whether the static variable
assertionsDisabled
(set in the class' static constructor via a call tojava.lang.Class.desiredAssertionStatus()
) is != 0 and if so, does nothing - If it is 0, then it checks the assertion expression and throws a
java.lang.AssertionError
if the expression resolves tofalse
, 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?
Related Topics
Scale Factor for Xxhdpi Android
Single Selection in Recyclerview
Org.JSON.JSON Exception:End of Input at Character 0
How to Catch Sigsegv (Segmentation Fault) and Get a Stack Trace Under Jni on Android
How to Get the Md5 Fingerprint from Java's Keytool, Not Only Sha-1
Adb Root Is Not Working on Emulator (Cannot Run as Root in Production Builds)
Android Saving Created Bitmap to Directory on Sd Card
Android - Application (Apk) Maximum Size
How to Show an Activity Before My App Is Uninstalled (Android)
How Do Android Screen Coordinates Work
Android Webview Style Background-Color:Transparent Ignored on Android 2.2
How to Fix "Unexpected Element <Queries> Found in <Manifest>" Error
How to Use Sharedpreferences in Xamarin.Android
How to Create Spinner-List Using Customadapter in Android
Background-Size: Cover Not Working in Portrait on Android Tablet
Turn on Location Services Without Navigating to Settings Page