Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused with the use of assignment "=" and brackets { }

Tags:

gradle

groovy

I am new to gradle (and so also to groovy) and I am hardly getting behind some concept and don't really know if it is groovy or gradle related and what keywords to google, to get some help.

I very often stumble over stuff like:

android {
    [some configuration]
}

So what is android? A class? A namespace? A collection of properties?

But what is more confusing:

Sometimes I see configurations that look like:

minSdkVersion 19

This seems to be an assignment, but without an equal sign. And sometimes there are assignments with equal signs like

source = "folder/file.java"

So this is very confusing. Equal sign vs. no equal sign. All these bracket stuff.

All the groovy introductions I saw don't cover exactly these topics. So is it some gradle convention or is it real groovy syntax?

like image 767
Subby Avatar asked Mar 31 '14 10:03

Subby


3 Answers

In Groovy, parentheses are sometimes optional.

The first android is passing a closure to a Method. ie:

void android( Closure config ) {
    println "In Android : ${config()}"
}

android {
    'tim'
}

prints : In Android : tim it is the same as calling:

android( {
    'tim'
} )

If you put the parens back.

The second example with minSdkVersion is the same, but it is passing an Integer to a method.

void minSdkVersion( Integer version ) {
    println "In MinSdkVersion : $version"
}

minSdkVersion 19
// same as minSdkVersion( 19 )

So that prints In MinSdkVersion : 19

The last example is setting a property to a String (as you'd expect)

like image 91
tim_yates Avatar answered Nov 16 '22 02:11

tim_yates


Strictly speaking android is a dynamic method that accepts a closure (a code block), that is given access to some internal representation of android plugin/task configuration. So, inside that closure you eventually either call other methods or do assignments to the properties that are available. If there is no equal sign then it is a method call, if there is then it is a pure property. You can get the idea by looking at plugin documentation or at the objects that represent it.

I think you can safely refer to this block as android plugin configuration. Since it is what the code affects.

like image 36
Andrey Adamovich Avatar answered Nov 16 '22 01:11

Andrey Adamovich


In gradle, you have plugins which each includes properties and methods for extension.

Properties are groovy properties and their getters/setters are auto-generated. And when you want to change a property what happens behind the scenes is calling it's generated setter. So, basically, it's a method call.

Method call can be done in various ways (just syntactic sugar). For example, you can omit the paranthesis or commas. I think the link should explain the look you've mentioned.

How those methods and properties resolved is entirely different thing though. If you're interested in where those words come from, check delegates.

like image 1
stdout Avatar answered Nov 16 '22 01:11

stdout