7

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.

vach
  • 10,571
  • 12
  • 68
  • 106
  • 1
    Possible duplicate of [Extract common methods from Gradle build script](https://stackoverflow.com/questions/18715137/extract-common-methods-from-gradle-build-script) – Alexey Soshin Aug 28 '19 at 15:30
  • Not really, i'm asking this for kotlin and most importantly i want the types to be exported, not guessed on the other side... In one of the answers they basically get the method from ext by assuming what the method signature looks like... the whole point is to have type safety and easily use this stuff otherwise we could stick to groovy – vach Aug 29 '19 at 09:46
  • 1
    If you find another way to achieve that, feel free to share it. – Alexey Soshin Aug 29 '19 at 11:53
  • I absolutely will :) – vach Aug 29 '19 at 12:02

1 Answers1

2

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.

David Burström
  • 1,592
  • 14
  • 14