Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are "BuildConfig.DEBUG conditional checks"?

Tags:

Android Lint complain about assert() being used, and recommend using BuildConfig.DEBUG conditional checks instead.

I understood perfectly why assert are not safe to use on android, but what exactly are "BuildConfig.DEBUG conditional checks" ?

How would the following example code be amended ?

Context ctx = getContext(); assert (ctx instanceof FragmentActivity); fragment_manager = ((FragmentActivity) ctx).getSupportFragmentManager(); 
like image 670
Rémi Avatar asked Apr 16 '14 15:04

Rémi


1 Answers

I think what lint is trying to say is that add a check of BuildConfig.DEBUG for assert statement

if(BuildConfig.DEBUG)

assert (ctx instanceof FragmentActivity);

so that assert works only when you are testing the app, but on release versions assert will not be called

BuildConfig.DEBUG will be false when you export a release build.

Edit: Looks like you should do something like below rather than using assert

 if(BuildConfig.DEBUG && !(ctx instanceof FragmentActivity))       throw new RuntimeException(); 

instead of assert.

source: http://tools.android.com/recent/androidstudio045released

Some new lint checks, and in particular one which flags uses of the assertion keyword. This does not work reliably on devices and you should use BuildConfig.DEBUG to do conditional checks instead.

like image 81
nandeesh Avatar answered Sep 28 '22 11:09

nandeesh