Monthly Archives: June 2015

Gradle : Cannot add task ‘:helloFromBuild1’ as a task with that name already exists.

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
helloFromBuild2

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

Leave a comment

Filed under Gradle, Open Source

Gradle : Stackoverflow Error

One of the reasons for Stackoverflow Error is infinite loop. So when Gradle gives ouptput like this:

yogeshwardancharan@yogeshwardancharan-Lenovo-G570:~/android_learning_resources/gradle_resources/ud867/1.01-Exercise-RunYourFirstTask$ gradle tasks

FAILURE: Build failed with an exception.

* Where:
Script ‘/home/yogeshwardancharan/android_learning_resources/gradle_resources/ud867/1.01-Exercise-RunYourFirstTask/other.gradle’ line: 1

* What went wrong:
A problem occurred evaluating script.
> java.lang.StackOverflowError (no error message)

* Try:
Run with –stacktrace option to get the stack trace. Run with –info or –debug option to get more log output.

BUILD FAILED

Total time: 1.563 secs

Well , there might be many reasons of the above error , but one of them is creating cyclic dependency in files. To better understand see two build files below:-

build_1.gradle

apply from : 'build_2.gradle'

build_2.gradle

apply from : 'build_1.gradle'

So , here , build_1 is trying to import build_2 and vice-versa and since Gradle handles dependency issue during configuration phase by creating a Directed Acyclic Graph , here due to cycle it is not able to do so and gets trapped into infinite loop resulting in Stackoverflow Error.

Stackoverflow Error can also be generated if a file somehow tries to import itself directly or indirectly , because that also results in a cycle.

Leave a comment

Filed under Gradle, Open Source