-1

I'm trying to figure out what kind of code element the apply from is:

buildscript {
    apply from: rootProject.file("gradle/versions.gradle")
    repositories {
        google()
        ...
    }
    dependencies {
        classpath plugin.android_gradle_plugin
        ...
    }
}

From this post i understand that the buildscript gradle function is called with a closure, so we can add the omitted brackets:

buildscript ({
    apply from: rootProject.file("gradle/versions.gradle")
    repositories ({
        google()
        ...
    })
    dependencies ({
        classpath plugin.android_gradle_plugin
        ...
    })
})

I first thougt the apply from is a label but then when could that label actually be used? Not to mention that groovy doesn't mention that labels are allowed to have spaces.

Then i thought it could be a property initialization (new Coordinates(latitude: 43.23, longitude: 3.67)), but apply from is in a closure.

At last i read about "curry", like this:

def nCopies = { int n, String str -> str*n }    
def twice = nCopies.curry(2)                    
assert twice('bla') == 'blabla'

So the apply from part could be evaluated first when using the closure, but even then i am still unaware if the apply from is a label or some kind of assignment and what purpose it serves.

So, what kind of code element is the apply from part?

goulashsoup
  • 2,639
  • 2
  • 34
  • 60

1 Answers1

1

TL;DR: It's a function call with a map as argument

apply from: rootProject.file("gradle/versions.gradle")

Is short for:

apply(from: rootProject.file("gradle/versions.gradle"))

("Parens are optional, if unambiguous")

Is short for:

apply([from: rootProject.file("gradle/versions.gradle"]))

(If maps are passed, you can leave out the [] around the map literal)

cfrick
  • 35,203
  • 6
  • 56
  • 68