Sometime Gradle shows error like this :
* What went wrong:
A problem occurred evaluating script.
> Cannot add task ‘:helloFromBuild1’ as a task with that name already exists.
This may result even when you haven’t created two tasks with the same name in build files.So, what’s the reason behind Gradle giving this error.Let’s try to understand this with an example.
Consider below sample files :
build_1.gradle
task helloFromBuild1
build_2.gradle
apply from : 'build_1.gradle' task helloFromBuild1
build_3.gradle
apply from : 'build_1.gradle' apply from : 'build_2.gradle' task helloFromBuild1
Now , when we import build_1.gradle in build_2.gradle, it effectively imports all tasks of build_1.gradle into build_2.gradle which is evident from terminal output :
yogeshwardancharan@yogeshwardancharan-Lenovo-G570:~/android_learning_resources/gradle_resources/ud867/1.01-Exercise-RunYourFirstTask$ gradle -b build_2.gradle tasks
:tasks————————————————————
All tasks runnable from root project
————————————————————Build Setup tasks
—————–
init – Initializes a new Gradle build. [incubating]
wrapper – Generates Gradle wrapper files. [incubating]Help tasks
———-
components – Displays the components produced by root project ‘1.01-Exercise-RunYourFirstTask’. [incubating]
dependencies – Displays all dependencies declared in root project ‘1.01-Exercise-RunYourFirstTask’.
dependencyInsight – Displays the insight into a specific dependency in root project ‘1.01-Exercise-RunYourFirstTask’.
help – Displays a help message.
model – Displays the configuration model of root project ‘1.01-Exercise-RunYourFirstTask’. [incubating]
projects – Displays the sub-projects of root project ‘1.01-Exercise-RunYourFirstTask’.
properties – Displays the properties of root project ‘1.01-Exercise-RunYourFirstTask’.
tasks – Displays the tasks runnable from root project ‘1.01-Exercise-RunYourFirstTask’.Other tasks
———–
helloFromBuild1
helloFromBuild2To see all tasks and more detail, run gradle tasks –all
To see more detail about a task, run gradle help –task
BUILD SUCCESSFUL
Total time: 1.377 secs
Now , when we import both build_1.gradle and build_2.gradle into build_3.gradle it results in task name collision because task helloFromBuild1 is available in both build_1.gradle and build_2.gradle and hence this error.
Solution :
If you want to import task of both build_1.gradle and build_2.gradle in build_3.gradle then in a case like above , you can do it only by importing build_2.gradle in build_3.gradle, since tasks of build_1.gradle are already included in build_2.gradle.