Search code examples
androidgradlegradle-task

Gradle: Differences when define task with and without << operator


I'm using Gradle and I try to configure for my Android project. I read document and I see that there are two ways of defining a task.

Without << Operator

task SampleTask {
    methodA param1 param2
}

With << Operator:

Task SampleTask <<{
   methodA param1 param2
}

My question is: what is real differences between above 2 ways?

Thanks :)


Solution

  • you can define tasks like this :

    task hello {
        doLast {
            println 'Hello world!'
        }
    }
    

    here, the last thing that hello task does, is to print 'Hello World!' I can use another syntax for defining my task like this :

    task hello << {
        println 'Hello world!'
    }
    

    these two tasks are same. another example is :

    task hello << {
        println 'Hello Earth'
    }
    hello.doFirst {
        println 'Hello Venus'
    }
    hello.doLast {
        println 'Hello Mars'
    }
    hello << {
        println 'Hello Jupiter'
    }
    

    now the output will be :

    Hello Venus
    Hello Earth
    Hello Mars
    Hello Jupiter
    

    read documentation for more details.