BuildConfig.DEBUG always return false

2020-06-30 13:55发布

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

9条回答
Fickle 薄情
2楼-- · 2020-06-30 14:30

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.

查看更多
老娘就宠你
3楼-- · 2020-06-30 14:32

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.

查看更多
Rolldiameter
4楼-- · 2020-06-30 14:32

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

After this change, all was working properly :

enter image description here

查看更多
▲ chillily
5楼-- · 2020-06-30 14:33

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

查看更多
smile是对你的礼貌
6楼-- · 2020-06-30 14:36

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

查看更多
手持菜刀,她持情操
7楼-- · 2020-06-30 14:40

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
}
查看更多
登录 后发表回答