DEV Community

Dmitrii Leonov
Dmitrii Leonov

Posted on

2 1

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay