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);
}
}
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);
}
}
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 atcom.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 specifyingbuildType
conflicted with the default behaviour of Android Studio and its generation ofBuildConfig
, stating I had a duplicate entry.So essentially:
BuildConfig
(so let it stay red)buildType
to yourbuild.gradle
(this may conflict with the default build behaviour of auto-generating the class)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.
Perhaps not ideal, but I ended up creating my own
And then address it like
BuildConfig.IS_DEBUG
programatically.Ensure the auto import statement of build config on the top of your class belongs to your project.
the
BuildConfig
import might belong to a released library thereDEBUG
is false.