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?
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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With