Ven Shine
Ven Shine

Reputation: 9326

Why print release and debug message in build.gradle?

build.gradle
buildTypes {
  release {
    println('release')
  }
  debug {
    println('debug')
  }
}

When I exec ./gradlew assembleDebug, print message

release
debug

Why print release message? I not understand it.

I want to execute some code in release mode, but it also can be executed in debug mode, how can I do?

buildTypes {
        release {
            println('release')
            packagingOptions {
                println('release packaging options')
                exclude 'lib/armeabi/*.so'
            }
            ndk {
                abiFilters 'armeabi-v7a'
            }
            shrinkResources true
            minifyEnabled true
            zipAlignEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField "String", "BUILDTIME", "\"${buildTime()}\""
            buildConfigField "boolean", "RELEASE", "true"
        }
        debug {
            println('debug')
            ndk {
                abiFilters 'armeabi', 'armeabi-v7a'
            }
            minifyEnabled false
            buildConfigField "String", "BUILDTIME", "\"${buildTime()}\""
            buildConfigField "boolean", "RELEASE", "false"
        }

    }

When I run ./gradlew assembleDebug, print message

release
release packaging options
debug

How can I do ? I want not execute release statement in debug mode.

Upvotes: 0

Views: 522

Answers (2)

shizhen
shizhen

Reputation: 12583

To understand why the messages are printed even though you didn't execute the any tasks, you need to understand the build phases of Gradle build lifecycle, i.e. initialisation, configuration and execution.

Most of the scripts will be run during configuration phase. The settings under file settings.gradle will be executed during initialisation phase. And the statements inside doFirst{} and doLast{} will be run during execution phase.

For your case

buildTypes {
  release {
     println('release')
  }
  debug {
     println('debug')
  }
}

Above code will actually be run during the project configuration phase and the message will be seen inside console.

How can I do ? I want not execute release statement in debug mode.

When you run

./gradlew assembleDebug

Even though you see the release message, it does not affect your debug output of your debug command.

See the official documents: https://docs.gradle.org/current/userguide/build_lifecycle.html .

Upvotes: 1

injecteer
injecteer

Reputation: 20699

The messages are printed because the println statements are executed during task declaration / initialization stage. That does NOT mean that the tasks are executed.

Upvotes: 1

Related Questions