1

I have a gradle build i need to have different configurations based whether it is a release or debug build. The problem is that the gradle build does not distinguish between those two.

for example :

apply plugin: 'com.android.library'

android {
  compileSdkVersion 22
  buildToolsVersion "22.0.1"

    buildTypes {

        debug {
          println 'debug'

        }
        release {
          println 'release'

        }

    }
  }

When i build using 'gradle assembleRelsease' or using 'gradle assembleDebug', it prints both 'release' and 'debug' in both cases or even when i build using debug (from Build Variants) in android studio. It simply does not distinguish it. What i would excpect is when i build release it only prints 'release' and when i build degub it only prints 'debug'. Does any body have a solution to this problem ? am i do doing something wrong?

fadi
  • 11
  • 1
  • It prints both values because they are in the scope of the configuration phase. See [the gradle lifecycle documentaiton](https://docs.gradle.org/current/userguide/build_lifecycle.html) for more details. Also for libraries the build is [always release](http://stackoverflow.com/questions/20176284/buildconfig-debug-always-false-when-building-library-projects-with-gradle). – GPuschka Jun 09 '16 at 08:23
  • 1
    Libraries are always built as release only if they're included as a module inside an application project in Android Studio. A separate library project will build debug or release as instructed. – mijiturka Jun 09 '16 at 09:47
  • Thank you for the answer but the problem it always enter in both. so i always get the configuration variables written from the second block whether it is 'release' or 'debug' – fadi Jun 09 '16 at 10:12
  • Please state what your expected behavior is, and why your situation demands that behavior. My guess is that your expectations do not match the reality of the build system. Please bear in mind that the lines you add in build.grade are *actual code* that get executed *universally* at every build, not just code the gets executed conditionally based on a condition that you imagine. If you have code to execute conditionally, then write that into yoru build.gradle as such, based on some condition you determine. – Doug Stevenson Jun 10 '16 at 05:03

1 Answers1

1

Your script will produce the expected results (different .aar's) for debug and release library variants: it just prints everything all the time.

What you're encountering is the Gradle lifecycle, which goes through an Initialization, Configuration and Execution phase.

Since many build.gradle files (Android projects usually have at least 2) are used in the build process for different directories and subprojects, and you can define tasks after you use them in the ordering of the build file, I am guessing the first two phases perform scanning and analysis on the build files before actually building anything.

Configuration goes through all commands, including your println statement. (see this example) Maybe they've chosen to have println actually print during configuration to ease debugging.

mijiturka
  • 434
  • 6
  • 18
  • Thank you but it is not only about the print because i set some configuration variables based on this, but the problem it always enter in both. so i always get the configuration from the second block whether it is 'release' or 'debug' – fadi Jun 09 '16 at 10:04
  • 1
    If the configuration phase goes through all commands, it will go through your variable assignment too. If you're relying on their values you should probably use debug << { ... } or debug { doFirst { .. }} then, as per the example, to get them to only be assigned during execution. – mijiturka Jun 09 '16 at 10:12