kaun jovi
kaun jovi

Reputation: 615

Why are the configuration styles inconsistent in gradle?

I am new to gradle and a few things of gradle confuses me. Some things appear like inconsistent coding / configuration style.

For example, when we configure the repository to be jcenter or mavencentral we call a function / method e.g. jcenter.

repositories {  
   jcenter()  
}

However, in the same file, when we try to configure a dependency we do not call functions / methods anymore.

dependencies {
   classpath 'com.android.tools.build:gradle:2.3.1'
}

And then there are clearly variables getting values

productFlavors {
    prod {
        versionName = "1.0-paid"
    }
    mock {
        versionName = "1.0-free"
    }
}

I am sure there is a reason behind this perceived inconcistency but could not find anything when I read through the documentation. Could anybody explain the reason?

Upvotes: 1

Views: 228

Answers (2)

Marcin Krasowski
Marcin Krasowski

Reputation: 686

This is the flexibility (I prefer this to inconsistency) that is delivered by Groovy the language that Gradle is using. In Groovy you can call a function/method with or without the parenthesis if its name is followed by matching arguments but if there are no arguments you must add parenthesis to make it a call to a function and make it distinct from the closure it represents. Here is an example using groovysh

groovy:000> def a(){println "a"}
===> true
groovy:000> a
===> org.codehaus.groovy.runtime.MethodClosure@95e33cc
groovy:000> a()
a
===> null
groovy:000> def b(arg){println arg}
===> true
groovy:000> b
===> org.codehaus.groovy.runtime.MethodClosure@d771cc9
groovy:000> b "argument"
argument
===> null
groovy:000> b("argument")
argument
===> null
groovy:000>

Upvotes: 3

Henry
Henry

Reputation: 43798

Actually these examples are not so different.

classpath 'com.android.tools.build:gradle:2.3.1'

is a function call as well. Groovy (the language in which gradle build scripts are written) allows you to leave out the parenthesis around the arguments in many cases.

Upvotes: 3

Related Questions