Search code examples
gradlekotlingroovygradle-kotlin-dsl

How can I call a function from a separate .gradle file from a gradle script using the Gradle Kotlin DSL?


Basically I want to do exactly this:

Use Gradle function from other gradle file

which is to say, call one function in one gradle script from another. The wrinkle is that my build.gradle is in Kotlin (build.gradle.kts) and the script my function is in is still in groovy.

I followed the above link for groovy-to-groovy, but I can't get this to work using the Kotlin DSL.

In my groovy file, functions.gradle, I have:

def buildVersionName() {
        //Do some stuff
}

And

ext {
    buildVersionName = this.&buildVersionName
}

Then, in my build.gradle.kts script, I have:

apply(from = "functions.gradle")
project.ext.buildVersionName()

When I sync, I get the error:

Unresolved reference: buildVersionName

Solution

  • In the build.gradle.kts do the following:

    import groovy.lang.Closure
    apply(from="functions.gradle")
    val buildVersionName: Closure<Any> by ext
    buildVersionName()