Checking DEBUG build the risky way!

Do you know that using BuildConfig.DEBUG to check for Debug Build is risky? Although it works, could be wrong sometimes?

Why is that not best?

Imagine if you partition your App into multiple modules, whereby App is dependent on CommonLibrary.

In your CommonLibrary you build your common function, that could be used by other Apps in the future. e.g.

public static void LOGDEBUG(final String tag, String message) {
if (BuildConfig.DEBUG) {
Log.d(tag, message);
}
}

The above code seems good, as it suppose to prevent your debug message from showing when in Debug Build. Little did you know that,

BuildConfig.DEBUG in App is not the same as BuildConfig.DEBUG in CommonLibrary. And this would risk all your Debug Message shown in actual release build!!

What’s the remedy?

Android has provided an internal flag that is globally available indicating if a build is in Debug or non-Debug mode, regardless of which module you are in. It is ApplicationInfo.FLAG_DEBUGGABLE.

You could use it as below:-

boolean isDebug = ((getContext().getApplicationInfo().flags &  
ApplicationInfo.FLAG_DEBUGGABLE) != 0);

--

--