3

I am trying to translate all groovy-scripts in a gradle project to kotlin. The only problem I currently still have is translating the file settings.gradle in the root directory of my project. In the groovy-script there is an if-condition if(project.hasProperty("someProperty")). Unfortunately, when trying to translate it to kotlin, the project keyword is not recognized anymore. Furthermore, in the same folder I have a build.gradle.kts, which is written in kotlin and in which I can use the projectkeyword in an if-condition as described before without any problem. It just doesn't seem to work, when I want to translate the groovy-script in settings.gradle to kotlin in settings.gradle.kts.

The project is a gradle project and the settings.gradle looks as follows:

include ':subproject-a',
        ':subproject-b'

if (project.hasProperty("someProperty")) {
    include ':subproject-c'
}

I would like to translate this to kotlin like so:

include(":subproject-a",
        ":subproject-b")

if (project.hasProperty("someProperty")) {
    include(":subproject-c")
}

However, when I do that, project is not recognized. Even though, as mentioned before, it worked just like that in build.gradle.kts (which is written in kotlin, translated from groovy), which is in the same folder.

Chandler
  • 31
  • 1
  • 2
  • Have you tried `rootProject` instead of `project`? – tim_yates Sep 16 '19 at 15:36
  • 2
    What you have I do not think works or has ever worked. According to the documentation for `Settings` instance: https://docs.gradle.org/current/dsl/org.gradle.api.initialization.Settings.html There is no such property or method named `project`. – Cisco Sep 16 '19 at 17:05
  • or you can access your property by involving reflection workaround like [this](https://github.com/gradle/kotlin-dsl-samples/issues/635#issue-283219430) or you can use `val myVar: String by project` delegation as stated [here](https://github.com/gradle/kotlin-dsl-samples/commit/4e8a53b47c81af2996efdfecceb6a18b83ee65d5) & [here for stackoverflow answer in details (recommended to read)](https://stackoverflow.com/a/56535952/3763032) – mochadwi Mar 18 '20 at 06:23

0 Answers0