0

I'm trying to migrate a script from Groovy's gradle to Kotlin's gradle.

The function:

def Group(Closure closure) {
    closure.delegate = dependencies
    return closure
}

ext {

    aGroup = Group {
        implementation xyz
        kapt xpto
        ...
    }

    ...

}

What I manage to do:

object Group {

    operator fun <T> invoke(project: Project, closure: Closure<T>):Closure<T> {
        closure.delegate = project.dependencies
        return closure
    }

}

Notice that I've added the extra arg Project.

Groovy access the project property directly, I would like to know how can I get this directly from kotlin too, so I'll be able to remove that extra arg and keep the previous syntax.

ademar111190
  • 14,215
  • 14
  • 85
  • 114

1 Answers1

0

Short answer: You can't

Long answer:

You can't due to the strong/statically typed nature of Kotlin (and Java). Groovy is a dynamic language and lets you get away with many things, to an extent. In addition to the dynamic nature of Groovy, Gradle implements a lot of the metaprogramming that Groovy offers.

So even though you do not explicitly pass in a Project in your Groovy script, behind the scenes it is being looked up dynamically.

You could make Group an extension of Project to have a reference of project.

Cisco
  • 20,972
  • 5
  • 38
  • 60
  • I see. I did an extension but I face a new problem the `implementation` is also dynamic, I did another extension to have the `implementation` but I do not have the `kapt` and the `androidTestImplementation`… At the end the mess was too much, so it is preferable to keep the groovy. – ademar111190 Jan 20 '20 at 20:56