android – BuildConfig.DEBUG always return false-ThrowExceptions

Exception or error:

Why does BuildConfig.DEBUG return false, when I run the application?

I use it to control the log like the following:

public static void d(String LOG_TAG, String msg){
    if(BuildConfig.DEBUG){
        Log.d(LOG_TAG,msg);
    }
}
How to solve:

In your Android Studio build variant are you on debug variant?

That is applied when you use flavors, either for debug or release.

in the debug mode, BuildConfig.BUILD is true, and in the release mode, it is false.

###

Check imports in the class, make sure you are using correct BuildConfig path. You may use BuildConfig not from your app, but from some library.

###

If that code is in a library, then it’ll always be false, thanks to a 3-year-old bug in gradle.

###

There is a workaround for the problem:

App

dependencies {
    releaseCompile project(path: ':library', configuration: 'release')
    debugCompile project(path: ':library', configuration: 'debug')
}

Library

android {
    publishNonDefault true
}

###

Perhaps not ideal, but I ended up creating my own

    buildTypes {
    debug {
        buildConfigField "boolean", "IS_DEBUG", "true" // Had issues with BuildConfig.DEBUG, created IS_DEBUG to ensure functionality behaved as expected.
    }
    release {
        signingConfig signingConfigs.release
        buildConfigField "boolean", "IS_DEBUG", "false"
    }
}

And then address it like BuildConfig.IS_DEBUG programatically.

###

Maybe you are importing the wrong package, check that. (some Android libraries also have the BuildConfig class)

###

I specified debuggable true in build.config, but this was always false

After this change, all was working properly :

enter image description here

###

Ensure the auto import statement of build config on the top of your class belongs to your project.

com.your.package.name.BuildConfig

the BuildConfig import might belong to a released library there DEBUG is false.

Leave a Reply

Your email address will not be published. Required fields are marked *