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 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);

There’s an article about this flag (though it didn’t post the warning of using BuildConfig.DEBUG)

Android Debug Vs Release Build Check in Running Code -

Note: Android will warn if one explicitly hardcode android:debuggable as proposed in the article.

Avoid hardcoding the debug mode; leaving it out allows debug and release builds to automatically assign one less...

 It's best to leave out the android:debuggable attribute from the manifest. If you do, then the tools will automatically insert android:debuggable=true when building an APK to debug on an emulator or device. And when you perform a release build, such as Exporting APK, it will automatically set it to false.  
 If on the other hand you specify a specific value in the manifest file, then the tools will always use it. This can lead to accidentally publishing your app with debug information.

Click “❤” below to share. Thanks! ~Twitter:elye; Facebook:elye

More by Elye

Topics of interest

More Related Stories