How to declare gradle version 5.0 in build.gradle?

2019-03-02 06:19发布

问题:

I used task wrapper when Gradle was 4.x but when I change gradleVersion to 5.0 , gradle wrapper states that it can't add a task with the same name. This didn't happen when it was 4.x when I could just change from 4.8 to 4.9 without issues. Does Gradle changed how task wrapper works?

回答1:

Defining a custom wrapper task in your build script has been deprecated since Gradle 4.8 version, see Gradle 4.8 depreciations (section Overwriting Gradle's built-in tasks" section)

Since version 4.8 (and before 5.0) you should have a warning message as below if you still define a custom wrapper task:

$ ./gradlew clean --warning-mode all

> Configure project :

Creating a custom task named 'wrapper' has been deprecated and is scheduled to be removed in Gradle 5.0.

You can configure the existing task using the 'wrapper { }' syntax or create your custom task under a different name.'.

As announced, the support for custom wrapper task has been removed in Gradle 5.0, so you need to use the new way for configuring the Wrapper:

// Configuring the wrapper, the old way (gradle < 4.8 )
// see https://docs.gradle.org/4.4/userguide/gradle_wrapper.html#sec:wrapper_generation
task wrapper(type: Wrapper) {
    gradleVersion = '4.4'
    distributionType = Wrapper.DistributionType.BIN
}

// Configuring the wrapper, the new way (since Gradle 4.8) 
// see https://docs.gradle.org/current/userguide/gradle_wrapper.html#customizing_wrapper
wrapper{
    gradleVersion = '5.1'
    distributionType = Wrapper.DistributionType.BIN
}