DEV Community

Dmitrii Leonov
Dmitrii Leonov

Posted on

Gradle and Groovy Magic

Gradle can be pretty confusing at times, especially for Android developers working on small and medium projects. In Android project Gradle is set up by default and we rarely feel a need to explore how and why things work.

Here are some questions and answers that you might've had regarding Gradle.

1. Why do we use gradlew taskName instead of gradle taskName to execute a task?
It is because in our projects we use Gradle wrapper to have 1 version of Gradle among all the developers of the project.

2. Android Studio's Modules and Gradle's Projects
An Android project is also a Gradle project. Gradle has a 1-to-1 relationship between its projects and build.gralde files. At the same what Android studio calls a "module", in Gradle is referred as sub-project.

3. Should I use ' or " for string literals?
Groovy, which is one of the languages used along with Kotlin to describe Gradle script, recognises both double and single quotation marks as a string.
String types in Groovy:

  • 'Hello World 1' - String
  • "Hello World 2" - String
  • "Hello World $count" - GString

4. Function calls, properties, and when to use round brackets?
In Groovy:

  • If a function has at least one parameter you can ignore curly brackets. Fields are properties, so each class field generates setter and getter for a declared field.

Look at the code below and try to guess, how many times will the Foo.name() function will be triggered when executing ./gradlew showMagic?

class Foo {
    def name = ""

    void name(String newString) {
        name = newString
        println("Foo.name() triggered")
    }
}

tasks.create("showMagic") {
    doFirst {
        group "Whole new group"
        description "Check setter and getter capabilities"

        def foo = new Foo()

        foo.name = "string 1"
        println("showMagic() ${foo.name}")
        foo.name("string 2")
        println("showMagic() ${foo.name}")
        foo.name "string 3"
        println("showMagic() ${foo.name}")
        foo.setName("string 4")
        println("showMagic() ${foo.name}")
        foo.setName "string 5"
        println("showMagic() ${foo.name}")
        String propertyOrFunctionName = "name"
        foo."$propertyOrFunctionName" = "string 6"
        println("showMagic() ${foo.name}")
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

showMagic() string 1
Foo.name() triggered
showMagic() string 2
Foo.name() triggered
showMagic() string 3
showMagic() string 4
showMagic() string 5
showMagic() string 6
Enter fullscreen mode Exit fullscreen mode

5. Functions with multiple parameters
As you might have guessed from the code above, we can ignore round brackets and pass multiple parameters separated by commas:

void foo(String url, String parameter) {
    println url
    println parameter
}

tasks.create("callFoo") {
    doFirst {
        foo "google.com", 'consistency'
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)