0

I have a variable that I would like overridden with src/debug/java version when I'm in debug mode:

public static final boolean DEBUG = true;

I tried putting the variable in src/debug/java to be true, and src/main/java to be false, but it does not get overridden. Is there a way to do this or a workaround?

I tried the following in my build.gradle, but regardless of release or debug mode, it

    debug {
        buildConfigField "boolean", "REPORT_CRASHES", "false"
    }

    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField "boolean", "REPORT_CRASHES", "true"
    }

And my java:

    Log.d(TAG, "BuildConfig.REPORT_CRASHES: " + BuildConfig.REPORT_CRASHES);
    // Both debug and release versions display 'true'
Kamilski81
  • 14,409
  • 33
  • 108
  • 161
  • The way you describe it, it should work. Perhaps post a screenshot of your folder structure. – fweigl Apr 02 '15 at 18:40

2 Answers2

3

Per the example on this blog, you can use buildConfigField in your gradle files to add additional variables to your BuildConfig class:

defaultConfig {
    ....
    buildConfigField "boolean", "DEBUG_FLAG", "false"
}

buildTypes {
    debug {
        buildConfigField "boolean", "DEBUG_FLAG", "true"
    }
    ...
}  

Then reference it in code by using BuildConfig.DEBUG_FLAG. Note that BuildConfig already contains a DEBUG flag which is true in all except release builds.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Great recommendation. See my edits above. I"m not sure why but everything returns true. – Kamilski81 Apr 02 '15 at 19:40
  • Are you switching between your debug/release builds via the build variants window? One thing to note is that `BuildConfig.DEBUG` already does what you have written in your update. – ianhanniballake Apr 02 '15 at 19:57
  • I am encountering this error with variants, and this is probably why I can't resolve the issue at large: http://stackoverflow.com/questions/29434962/how-do-i-change-the-build-variant-for-a-library-dependency-module – Kamilski81 Apr 03 '15 at 15:20
0

You should use your com.main.module.BuildConfig VS your com.dependency.module.BuildConfig...this will solve all problems.

Kamilski81
  • 14,409
  • 33
  • 108
  • 161