36

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);
    }
}
bruno
  • 32,421
  • 7
  • 25
  • 37
cocoa
  • 378
  • 1
  • 3
  • 8
  • 1
    Possible duplicate of [BuildConfig.DEBUG always false when building library projects with gradle](http://stackoverflow.com/questions/20176284/buildconfig-debug-always-false-when-building-library-projects-with-gradle) – Oleksandr Jan 25 '17 at 14:34

9 Answers9

109

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.

DmitryBorodin
  • 4,584
  • 4
  • 17
  • 29
13

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.

Abhinav Saxena
  • 1,990
  • 2
  • 24
  • 55
marshallino16
  • 2,645
  • 15
  • 29
  • How does this work if I have a dev variant next to debug, and I want them both to be debuggable? Adding debuggable=true to dev variant did not help. – Chapz Aug 25 '22 at 13:09
9

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.

jeet.chanchawat
  • 5,842
  • 5
  • 38
  • 59
7

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

Lawrence Kesteloot
  • 4,149
  • 2
  • 31
  • 28
7

Do not import BuildConfig. This is an auto-generated class and importing it is unnecessary, despite what Android Studio may tell you.

If Android Studio is prompting you to import BuildConfig it may be because you need to do an initial Gradle build to create the auto-generated class which ends up being created at com.yourdomain.yourapp.BuildConfig. This can happen when you upgrade Android Studio and Gradle, or when you run Build -> Clean project.

If you import another package's BuildConfig, then of course it'll always be false because they are only releasing their release flavours and not their debug flavours.

Regarding the other answers recommending modifying your build.gradle, I found that specifying buildType conflicted with the default behaviour of Android Studio and its generation of BuildConfig, stating I had a duplicate entry.

So essentially:

  • Do not import any package's BuildConfig (so let it stay red)
  • Do not add buildType to your build.gradle (this may conflict with the default build behaviour of auto-generating the class)
  • Ignore the lint error
  • Run build

The error should go away.

I experience this when I upgrade Android Studio and Gradle and when I clean the project.

Ignore import prompts

Do not import another package's BuildConfig—it'll always be false because they are not releasing their debug versions.

enter image description here

Importing will cause the error you're experiencing

In my project, if I import one of the suggested libraries, it'll show the error you're getting, because no one releases a debug build so of course it'll always be false if you're pointing to someone else's.

enter image description here

enter image description here

Ignore the intellisense and run the project

Just run a build. The class will be auto-generated and the warning will go away.

enter image description here

Tina
  • 1,186
  • 10
  • 11
4

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.

ssawchenko
  • 1,198
  • 1
  • 13
  • 24
  • This is a manual setting. Instead, use the usual one because the value of `BuildConfig.DEBUG` changes, when you switch from `debug` mode to `release`. But thanks, you have given the idea of creating constants at BuildConfig level. – Abhinav Saxena Feb 09 '19 at 06:49
  • In order to make your `BuildConfig` based constants semi-dynamic, you should use flavors. But this is beyond the scope of this question. – Abhinav Saxena Feb 09 '19 at 06:50
4

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

After this change (simply remove it), all was working properly :

enter image description here

hannes ach
  • 16,247
  • 7
  • 61
  • 84
3

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
}
ND_
  • 63
  • 6
  • This is the correct answer for me, at least if you have that problem with a module / library. And this is not a workaround, you overwrite the default Android behavior for libraries, because you own this library. – botzek13 Jun 18 '18 at 12:50
  • One should use flavors to manage the release time and debug time settings. – Abhinav Saxena Feb 09 '19 at 06:53
3

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

César N.
  • 449
  • 4
  • 7