JavaExec x = task(type: JavaExec, name: "someTask")
Error:(38, 0) Cannot cast object ‘task ‘:{type=class org.gradle.api.tasks.JavaExec, name=android.Android}’’ with class ‘org.gradle.api.DefaultTask_Decorated’ to class ‘org.gradle.api.tasks.JavaExec’
Shouldn’t the cast work since a task of type JavaExec is created?
Gradle is light-years better then most build tools.
Still I despise compiler magic when it comes to creating tasks. You can avoid it completely by using:
JavaExec x = tasks.create(name: "someTask", type: JavaExec)
Any build tool that goes beyond the very basics needs to be understood or easily analyzed by the user. This includes every line in the script. Sadly Groovy already offers - not one - but many ways to do the very same thing and Gradle introduces even more syntax sugar in order to type a few less characters. This is all confusing to beginners.
Maybe the Gradle team removes all the bad decisions for the 3.0+ release
‘task(“someTask”, type: JavaExec)’ is the regular ‘Project#task’ API for creating tasks, but the OP omitted a mandatory argument, and it’s possible that the compiler magic got in the way. Task declaration is the only case where Gradle uses compiler magic to enable special syntax (‘task someTask(type: JavaExec)’), and perhaps it will get deprecated at some point.
Actually I had to dig down to find the culprit DefinitionScriptTransformer in order to fully understand it
In my opinion there should be ONE way to do this:
task.create(name: "someTask", type: JavaExec) {}
Adding a task to the collection of tasks should be explicit.
I know it would increase the size of build files a little but also make them much nicer to read. In the end it’s probably a question of taste and Gradle to a big extend is Groovy which has a very different philosophy then let’s say Python: http://legacy.python.org/dev/peps/pep-0020