Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle kotlin how to call function defined in parent?

When working with gradle multimodule project, is it possible to define functions in parent project but use them in submodules build.gradle.kts?

Note i do not need untyped tasks registered and called with strings... I want actual typesafe code to be shared to submodules.

Ideally without creating a plugin or using buildSrc.

Whats the most to the point way to share a class from parents build.gradle.kts to all submodules?

NOTE : this is not the same as sharing closure trough ext... you loose type safety, what i ask for is Type safety on submodule side.

like image 674
vach Avatar asked Aug 28 '19 10:08

vach


Video Answer


1 Answers

I'm thinking that there is no way. When a Kotlin build script gets compiled, it will end up as a class called Build_gradle in the default package (i.e. empty package) that extends CompiledKotlinBuildScript. Any class that is defined within a script becomes a public nested class in its corresponding Build_gradle. When the subproject build script gets compiled, it has no access to the classpath that contains the parent projects build script. This makes sense, as there would be multiple Build_gradle files in the same (default) package.

I'd go for buildSrc, to solve the problem, but I'm speculating that what you also wanted was some sort of nice separation of concerns in a multimodule project, not having unrelated projects knowing about what others need to communicate. What you could do to minimize the exposure is to only keep the API (interfaces, data classes) in buildSrc and have a script plugin in a parent project provide the implementation.

like image 164
David Burström Avatar answered Sep 29 '22 10:09

David Burström