BuildConfig.DEBUG is not working (= logically set to false) when I run my app in debug mode. I use gradle to build.I have a library project where I do this check. BuildConfig.java looks like this in the build debug folder:
/** Automatically generated file. DO NOT MODIFY */
package common.myProject;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
}
and in the release folder:
public static final boolean DEBUG = false;
both in the library project and in the application project.
I tried to get around this by checking a variable which is set a class of my project. This class inherits from the library and starts on startup.
<application
android:name=".MyPrj" ...
This led to another problem: is use my DEBUG variable in a DataBaseProvider which runs before the application class.
With Android Studio 1.1 and having also the gradle version at 1.1 it is possible:
Library
App
Complete documentation can be found here http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication
EDIT:
The issue has just been marked as fixed for the Android Studio Gradle Version 3.0. There you can just use
implementation project(path: ':library')
and it'll select the correct configuration automatically.Check for
imports
, sometimes BuildConfig is imported from any class of library unintentionally. For example:In this case BuildConfig.DEBUG will always return false;
In this case BuildConfig.DEBUG will return your real build variant.
As a workaround, you can use this method, which uses reflection to get the field value from the app (not the library):
To get the
DEBUG
field, for example, just call this from yourActivity
:I have also shared this solution on the AOSP Issue Tracker.
You can create your own BuildConfig class for each build type using gradle
for /src/debug/.../MyBuildConfig.java and...
for /src/release/.../MyBuildConfig.java
Then use:
This is my workaround: reflect BuildConfig of app module:
`public static boolean debug = isDebug();
In my case I was importing the wrong
BuildConfig
as my project has many library modules. The fix was to import the correctBuildConfig
for myapp
module.