ChatGPT解决这个技术问题 Extra ChatGPT

When does ADT set BuildConfig.DEBUG to false?

In the newest version of ADT (r17) a generated constant was added BuildConfig.DEBUG that is set according to the build type. The problem I have is that it is never set to false, I expected it to change when doing "Android Tools -> Export Signed Application Package" but it hasn't for me.

So how do I change the build type?

Added a feature that allows you to run some code only in debug mode. Builds now generate a class called BuildConfig containing a DEBUG constant that is automatically set according to your build type. You can check the (BuildConfig.DEBUG) constant in your code to run debug-only functions

BuildConfig.java is generated automatically by Android build tools, and is placed into the gen folder. The signed APK should have BuildConfig.DEBUG = false. It shouldn't be a problem for you. You shouldn't have to manually touch that file...
If you use gradle to release this flag is 100% reliable. So when you do a ./gradlew assembleDebug its true and when doing assembleRelease its false.

s
smith324

Currently you can get the correct behavior by disabling "Build Automatically", cleaning the project and then export via "Android Tools -> Export Signed Application Package". When you run the application BuildConfig.DEBUG should be false.


broken too. Which has the consequence of displaying all Log.d messages which should be omitted by this flag. ps. where to file bug report?
mine is always false, even when debugging
A
Arnaud SmartFun

With Eclipse, I always disable "Build Automatically" option before Exporting the app in release. Then I clean the project and export. Otherwise it starts compiling in debug mode, and then the value of BuildConfig.DEBUG may be wrong.

With Android Studio, I simply add my own custom variable in the build.gradle:

buildTypes {
    debug {
        buildConfigField "Boolean", "DEBUG_MODE", "true"
    }
    release {
        buildConfigField "Boolean", "DEBUG_MODE", "false"
    }
}

When I build the project, the BuildConfig.java is generated as follows:

public final class BuildConfig {
  // Fields from build type: debug
  public static final Boolean DEBUG_MODE = true;
}

Then in my code I can use:

if (BuildConfig.DEBUG_MODE) {
    // do something
}

I recommand to clean after switching debug/release build.


This solution is the best if you use proguard because it will generate a constant with a literal value, so your debug code will be completely removed from the binary in release mode.
R
Randy Sugianto 'Yuku'

It doesn't work properly:

Issue 27940: BuildConfig.DEBUG is "true" for exported application package

It's disappointing that they sometimes release buggy features.


Please go to the link to the issue mentioned above and 'star' it if you want this to be fixed.
C
Community

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.

p.s I just copy this one from my answer here:BuildConfig.DEBUG always false when building library projects with gradle


Yeah, for me it was accidentally imported from android.support.compat. I guess that's another reason to just define your own field with a different name.
My project is not allowing me to import from my package name. Artic Fox IDE is buggy as hell
p
pbhowmick

It does work, but note that the code file never changes, even when exporting the signed file. The export process changes the value of this variable to false, which might give you the false impression that it is not working. I tested this with logging statements like

if (com.mypackage.BuildConfig.DEBUG)
            Log.d(TAG, location.getProvider() + " location changed");

When testing, my Log statements no longer produce any output.


I changed the instances of BuildConfig.DEBUG to com.mypackage.BuildConfig.DEBUG, then reran the app... and it still returned true all the time. Maybe I misunderstood your suggestion.
What I am saying is that the code will NOT change. However, com.mypackage.BuildConfig.DEBUG will be set to False post compilation. Try a test logging statement as above (choose an arbitrary string to log), do the export and then run it. See if adb displays the logging statement. I am will to take a bet that adb will not report that logging statement, signifying that DEUBUG has been set to false.
I'm not sure I know what you mean about "the code"... however, I will say that doing a clean before exporting the APK (as suggested in the accepted answer) made both BuildConfig.DEBUG and com.mypackage.BuildConfig.DEBUG report false as expected.
You got it. That's the expected behavior.
if you literally used "com.mypackage.BuildConfig.DEBUG" the it would not work. The package is relative to your app.
p
peter

From Preparing for Release:

Turn off logging and debugging Make sure you deactivate logging and disable the debugging option before you build your application for release. You can deactivate logging by removing calls to Log methods in your source files. You can disable debugging by removing the android:debuggable attribute from the tag in your manifest file, or by setting the android:debuggable attribute to false in your manifest file. Also, remove any log files or static test files that were created in your project. Also, you should remove all Debug tracing calls that you added to your code, such as startMethodTracing() and stopMethodTracing() method calls.

More information is following the link.


I thought that this process now happens automatically at build time: developer.android.com/tools/sdk/tools-notes.html
Causes compile-time error: «Avoid hardcoding the debug mode; leaving it out allows debug and release builds to automatically assign one»
e
e.shishkin

The solution for me:

Project -> Build Automatically Project -> Clean Project -> Build Project Export Android application

It's work in r20


This worked for me just now (using the latest ADT I guess). Maybe the cleaning fixed it, not sure.
n
neokim

I would want to propose a simple workaround if you use proguard during APK export.

Proguard provides a way to remove calls to specific functions in release mode. Any calls for debugging logs can be removed with following setting in proguard-project.txt.

# Remove debug logs
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

And optimization setting in project.properties.

proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt

With this, you don't need to concern any unnecessary String computation passing to debug log to which @Jeremyfa pointed. The computations are just removed in release build.

So the workaround for BuildConfig.DEBUG uses the same feature of proguard like following.

public class DebugConfig {

    private static boolean debug = false;

    static {
        setDebug(); // This line will be removed by proguard in release.
    }

    private static void setDebug() {
        debug = true;
    }

    public static boolean isDebug() {
        return debug;
    }
}

And following setting in proguard-project.txt.

-assumenosideeffects class com.neofect.rapael.client.DebugConfig {
    private static *** setDebug();
}

I would prefer using this to disabling the Build Automatically option, because this doesn't depend on the builder's individual IDE setting but is maintained as committed file which are shared among developers.


V
Vincent Mimoun-Prat

Does not work properly as far as I understood (Android issue 22241)

I had some trouble on a project (working with Eclipse), that constant was not set to true when exporting a signed APK of my project :(

Would love to hear it works though


It should've been fixed in r17, its marked as such in the bug tracker.
Actually libs are not compiled in release mode in ADT when exporting (works in Ant). I updated code.google.com/p/android/issues/detail?id=27940
@Xav thanks for looking into it, I'll stop spamming you now promise. It was actually the main project I was having issues with (didn't look at the dependent library). If I can create a concrete test case I'll post it to the bug tracker under the same issue.
B
Baby Groot

a good way is creating your own class :

public class Log {

public static void d(String message) {
    if (BuildConfig.DEBUG)
        android.util.Log.d(
            "[" + (new Exception().getStackTrace()[1].getClassName()) + "]",
            "{" + (new Exception().getStackTrace()[1].getMethodName()) + "} "
            + message
        );
}

}

The problem with this method is that, event when DEBUG is false, java will still compute each String to pass it to your custom class. The if (DEBUG) Log.d(...) is less elegant but more efficient.
T
TALE

I've seen some strange behavior that has to do with when the values in BuildConfig are set to their final values. This may have something to do with your issue.

The simple explanation is that default values are set initially before Proguard is run, then after Proguard runs, the BuildConfig file is regenerated with the proper values. However, Proguard has already optimized your code by this point and you have issues.

Here is a bug I created against Gradle. https://code.google.com/p/android/issues/detail?id=182449


M
Mohd Qasim

will you check your app level build.gradle enable debuggable true for release

buildTypes {
    release {
        debuggable true
        }
     }

instead you keep false or comment that line

buildTypes {
    release {
        //debuggable true
        }
     }

now you will get BuildConfig.DEBUG false for release build