blueberrycarrot
09/25/2024, 11:05 AMbuild.gradle.kts wants the setX(value) syntax, in others x = value, for example in:
sourceSets {
main {
java {
setSrcDirs(listOf("somedir"))
destinationDirectory = File("otherdir")
}
}
}
I.e., setDestinationDirectory(...) doesn't compile, and neither does srcDirs = .... Am I doing this wrong?Vampire
09/25/2024, 11:13 AMdestinationDirectory is Kotlin sugar for calling getDestinationDirectory() which returns a DirectoryProperty, so the "natural" way is to do destinationDirectory.set(...) but there is some syntax sugar that you can do destinationDirectory = ... instead.
With the srcDirs it is a bit different, getSrcDirs() returns a copy of the src dirs a new Set<File>, so even if you could call add and so on on it, it would not have any effect. You can call srcDir(...) to add a src dir, or you can use setSrcDirs(listOf(...)) to overwrite all already defined src dirs.Vampire
09/25/2024, 11:17 AMFile(...) with a relative path.
In practically all JVM code this is a very bad idea, because the File constructor resolves relative paths relative to the current working directory, so is always depending on the users current working directory. The only valid use-case I'm aware is, if you code some commandline utility and the relative path is coming from the user calling the utility in which case you can usually safely assume the given path is relative to the current user's working directory.
In Gradle logic this is even more problematic, as the current working directory of the Gradle daemon is the on that is used to resolve the relative path. This working directory can be the root project's project directory, but this is not guaranteed and sometimes not the case.
So in your case you most probably would have wanted to do destinationDirectory = layout.projectDirectory.dir("otherdir")blueberrycarrot
09/25/2024, 11:18 AMsrcDirs.set(...) doesn't work either. I'm just trying to find a universal way to set values, but maybe there isn't one?blueberrycarrot
09/25/2024, 11:20 AMVampire
09/25/2024, 11:22 AMYes, why should it work?doesn't work either.srcDirs.set(...)
srcDirs is giving you a Set<File> as I just explained and that does not have a method called set and even if it would have, it would not have any effect as it is just a copy as I just explained.
I'm just trying to find a universal way to set values, but maybe there isn't one?No, there isn't in this case.
I mean, from your explanation it sounds like you've got to know and understand the exact class you're working with while setting values.Like with any API you use. 😄
blueberrycarrot
09/25/2024, 11:27 AMbuild.gradle.kts is better edited as an arbitrary Kotlin file, not the project build fileblueberrycarrot
09/25/2024, 11:28 AMblueberrycarrot
09/25/2024, 11:31 AMVampire
09/25/2024, 11:35 AMsounds like it needs a full IDEWell, like with any coding, it greatly helps if you have a good IDE like IntelliJ IDEA or Android Studio, especially when using the Kotlin DSL which has much better IDE support than Groovy DSL due to the dynamic nature of Groovy. And if you don't use an IDE to edit any code, you just have to know the API and how to use it as you just a text editor. But usually the Userguide should provide you with appropriate examples how to do it, so it should usually be sufficient to edit the build script, even if not supported by IDE, it's just not as convenient.
king mistakes while editing kind of screws up the entire project/IDE stateIndeed, but usually not while editing. By default IntelliJ for example only reloads the project when either an external change is detected, or if you trigger the reload manually, so that you can do changes without breaking the reload and then trigger the reload when finished. If you broke it an lost IDE support, what I usually do is commenting out the problematic code, then reload the project so that IDE assistence is there again, then I comment back in the code and use IDE assistence to do what I want to do before I then reload again.
Maybe theNot really, that would then only work much worse if at all, as the context is different then.is better edited as an arbitrary Kotlin file, not the project build filebuild.gradle.kts
I wonder if there's a declarative Gradle build syntax that hides all the type complexity away,There is the "Declarative Gradle" project that should provide exactly what you are after, but this is in a very early stage. You can have a look at #C06JG95HREY and at the Gradle Blog posts relating to it.
blueberrycarrot
09/25/2024, 11:36 AM