BuildConfig.DEBUG always false when building libra

2019-01-04 17:38发布

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.

13条回答
Animai°情兽
2楼-- · 2019-01-04 17:59

With Android Studio 1.1 and having also the gradle version at 1.1 it is possible:

Library

android {
    publishNonDefault true
}

App

dependencies {
    releaseCompile project(path: ':library', configuration: 'release')
    debugCompile project(path: ':library', configuration: 'debug')
}

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.

查看更多
家丑人穷心不美
3楼-- · 2019-01-04 18:00

Check for imports, sometimes BuildConfig is imported from any class of library unintentionally. For example:

import io.fabric.sdk.android.BuildConfig;

In this case BuildConfig.DEBUG will always return false;

import com.yourpackagename.BuildConfig;

In this case BuildConfig.DEBUG will return your real build variant.

查看更多
ら.Afraid
4楼-- · 2019-01-04 18:00

As a workaround, you can use this method, which uses reflection to get the field value from the app (not the library):

/**
 * Gets a field from the project's BuildConfig. This is useful when, for example, flavors
 * are used at the project level to set custom fields.
 * @param context       Used to find the correct file
 * @param fieldName     The name of the field-to-access
 * @return              The value of the field, or {@code null} if the field is not found.
 */
public static Object getBuildConfigValue(Context context, String fieldName) {
    try {
        Class<?> clazz = Class.forName(context.getPackageName() + ".BuildConfig");
        Field field = clazz.getField(fieldName);
        return field.get(null);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}

To get the DEBUG field, for example, just call this from your Activity:

boolean debug = (Boolean) getBuildConfigValue(this, "DEBUG");

I have also shared this solution on the AOSP Issue Tracker.

查看更多
三岁会撩人
5楼-- · 2019-01-04 18:02

You can create your own BuildConfig class for each build type using gradle

public class MyBuildConfig
{
    public static final boolean DEBUG = true;
}

for /src/debug/.../MyBuildConfig.java and...

public class MyBuildConfig
{
    public static final boolean DEBUG = false;
}

for /src/release/.../MyBuildConfig.java

Then use:

if (MyBuildConfig.DEBUG)
    Log.d(TAG, "Hey! This is debug version!");
查看更多
霸刀☆藐视天下
6楼-- · 2019-01-04 18:02

This is my workaround: reflect BuildConfig of app module:

`public static boolean debug = isDebug();

private static boolean isDebug() {
    boolean result = false;
    try {
        Class c = Class.forName("com.example.app.BuildConfig");
        Field f = c.getField("DEBUG");
        f.setAccessible(true);
        result = f.getBoolean(c);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return result;
}`
查看更多
看我几分像从前
7楼-- · 2019-01-04 18:04

In my case I was importing the wrong BuildConfig as my project has many library modules. The fix was to import the correct BuildConfig for my app module.

查看更多
登录 后发表回答