2

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?

4
  • It prints both values because they are in the scope of the configuration phase. See the gradle lifecycle documentaiton for more details. Also for libraries the build is always release. Commented Jun 9, 2016 at 8: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. Commented Jun 9, 2016 at 9: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' Commented Jun 9, 2016 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. Commented Jun 10, 2016 at 5:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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'
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.