Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use assert in Android Studio

I been looking at this question, and I thought it would be a good idea of using assert only in debug build.

Is there any thing special that I need to configure in Android Studio in order to allow asserts? I also want to guarantee that they will not be presented in release build.

like image 573
Ilya Gazman Avatar asked Sep 09 '25 22:09

Ilya Gazman


1 Answers

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.

like image 154
Florian Barth Avatar answered Sep 12 '25 10:09

Florian Barth