Cannot add task 'wrapper' as a task with that name already exists
Asked Answered
C

7

148

When installing 'react-native init AwesomeProject' I get this error when I run react-native run-android:

Could not determine java version from '11.0.1'.

A quick google suggests I need to update the distributionUrl in the Gradle-wrapper. Having done this I am faced with a new error:

Cannot add task 'wrapper' as a task with that name already exists.

It suggests the issue is in the file:

/AwesomeProject/android/build.gradle' line: 36

which looks like this

task wrapper(type: Wrapper) {
    gradleVersion = '4.4'
    distributionUrl = distributionUrl.replace("bin", "all")
}

I've been back and forth trying to figure out what this does. It seems odd that something wouldn't work straight out of the box. Is anybody facing a similar issue?

Cotidal answered 10/12, 2018 at 16:0 Comment(1)
is there any update or which is a correct-worked answer? If not then how did you solved it? else tick-right icon on an answer.>> :-)Lomond
I
199

You can also update

task wrapper(type: Wrapper) {
    gradleVersion = '4.4'    
    distributionUrl = distributionUrl.replace("bin", "all")
}

to

wrapper {
    gradleVersion = '4.4'
    distributionUrl = distributionUrl.replace("bin", "all")
}

As

Overriding built-in tasks deprecated in 4.8 now produces an error.

Attempting to replace a built-in task will produce an error similar to the following:

Cannot add task 'wrapper' as a task with that name already exists.

see the last paragraph of Tasks & properties: https://docs.gradle.org/5.2.1/userguide/upgrading_version_4.html

and Customizing the Wrapper task: https://docs.gradle.org/5.2.1/userguide/gradle_wrapper.html#customizing_wrapper

Ixia answered 18/2, 2019 at 6:37 Comment(2)
This answer doesn’t answer OP’s actual question, but it’s very helpful to Googlers like me, so I upvoted.Shantishantung
It does solve the OP’s problem. He was using an ancient Gradle version and then tied to update using an outdated solution, and this answer provides the correct solution. (Of course, @FranklinYu, if you take “is anybody facing a similar issue?” as the OP’s question, then a simple “yes” would suffice.)Unexceptional
F
113

which version of gradle do you use in your environment?

If you use gradle version 5.x you have to modify 「task wrapper」 as below.

task wrapper(type: Wrapper) {
    gradleVersion = '4.4'
    distributionUrl = distributionUrl.replace("bin", "all")
}

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

wrapper {
    gradleVersion = '4.4'
    distributionUrl = distributionUrl.replace("bin", "all")
}
Figment answered 26/3, 2019 at 0:24 Comment(1)
Then this error came. Can't figure out what to do. ERROR: Could not find method wrapper() for arguments [{type=class org.gradle.api.tasks.wrapper.Wrapper}, build_81gq92nveejavsi3t9efznmk8$_run_closure2@55f5bac8] on root project 'myproject' of type org.gradle.api.Project.Flagon
F
30

if "gradle-wrapper.properties" file already have these codes

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

then you can remove

task wrapper(type: Wrapper) {
    gradleVersion = '4.4'
    distributionUrl = distributionUrl.replace("bin", "all")
}

from build.gradle

Fransen answered 17/9, 2019 at 7:8 Comment(1)
but in this case you can't use ./gradle task from terminalEulogize
S
23

As mentioned in the comments follow - https://mcmap.net/q/44905/-react-native-error-quot-could-not-determine-java-version-from-39-9-0-1-39-quot:

  1. Open and edit the file app-folder/android/gradle/wrapper/gradle-wrapper.properties; update the line

    distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-all.zip
    
  2. In file AwesomeProject/android/build.gradle try commenting out

    task wrapper(type: Wrapper) {
        gradleVersion = '4.4'
        distributionUrl = distributionUrl.replace("bin", "all")
    }
    
Subtorrid answered 12/12, 2018 at 6:44 Comment(0)
V
2

I have the same issue. the problem in my code was a double declaration on another Gradle file in build.gradle

build.gradle

apply from: otherFile.gradle
... build. gradle code...
apply from: otherFile.gradle //Again 
Variegation answered 15/9, 2019 at 11:33 Comment(0)
E
1

There's a high chance you have a "task" to run in your build.gradle file, go and remove the line related to wrapper (including its bracket and so on). Then, if you are using IntelliJ, it will automatically bring the new one to your working directory.

Eyla answered 22/7, 2019 at 5:6 Comment(0)
B
-1

I ran into the issue because I was running my task using the global gradle, and not my local gradle wrapper.

i.e. in my current directory I had a gradlew executable.

But I was running this command:

gradle <task>

When I should have been running:

./gradlew <task>

Presumably because running the global gradle tries to create a local wrapper -- and one already existed.

Bedrock answered 1/4, 2020 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.