Ilya Gazman
Ilya Gazman

Reputation: 32211

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.

Upvotes: 26

Views: 6306

Answers (1)

Florian Barth
Florian Barth

Reputation: 1392

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.

Upvotes: 11

Related Questions