简体   繁体   中英

BuildConfig.DEBUG always return false

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

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.

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.

如果该代码在库中,那么它总是错误的,这要归功于gradle 中的一个3 年前的错误

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.

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 :

在此处输入图片说明

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.

在此处输入图片说明

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.

在此处输入图片说明

在此处输入图片说明

Ignore the intellisense and run the project

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM