Hello team, I encountered an issue while migratin...
# gradle
s
Hello team, I encountered an issue while migrating my Gradle build scripts from Groovy to Kotlin DSL. I've successfully migrated the default Gradle files (project-level, app-level module, and settings) to Kotlin DSL. However, when I manually create a new file and try to migrate it to Kotlin DSL, I encounter errors. Interestingly, these manually created files work without errors in Groovy. I'm using Kotlin version 1.9.0, Gradle 8.4, Gradle Plugin 8.3.2, and JDK 17. Could someone please help me troubleshoot this issue? I'm unsure why this is happening or if I've missed something in the migration process. Thank you in advance for any assistance or insights you can provide!
g
It's because the name of thje file is test.gradle.kts, as result it doesn't have any build.gradle.kts context
what are you trying to do? remember, that script include is not working well with Kotlin, because it type-safe
So you can partially solve it by renaming to test.build.gradle.kts, so Gradle will know that it project context (also same for *.settings.glradle.kts, to explicitly mark that script will be used in settings context)
s
I understand, @gildor. I followed the same process with the custom Groovy file, and it seems to be working fine with the same build script codes. Please see the attached screenshots below for reference. Thank you for your understanding.
g
Yes, it works in Groovy, because for IDE this code is just as a simple text
you can write any block
Copy code
bogus {
    foo {
         iDoNotExist()
    }
}
And it will look the same For Groovy everything is possible, for Kotlin it showed as error
as I said, if you rename to test.build.gradle.kts it will work
but I wouldn't recommend this, you very soon will find that you limited by plugins {} block and type-safe accessors generated by it
s
I'd like to share something @gildor. I created these build script codes for testing purposes, particularly to manage unit test coverage using Jacoco with a custom file named "jacoco.gradle". However, I'm facing the same issue where it works in the Groovy file format but encounters problems in the "jacoco.gradle.kts" format. Do you have any insights or suggestions regarding this matter? Thank you for your help.
g
Exactly, for Kotlin DSL you need absolutely different approach, you cannot just move things to files, they will not be type safe
I highly recommend to read https://docs.gradle.org/current/userguide/kotlin_dsl.html before you approach it
s
Of course @gildor. I'll look into it. Thank you for your time and assistance.
👍 1
v
as I said, if you rename to test.build.gradle.kts it will work
That's not correct.
whatever.gradle.kts
is fine as "build script context". The problem is, that you use legacy script plugins (the things you "apply from"). Even with Groovy they are highly discouraged and have many quirks. With Kotlin DSL it is even worse as you do only get type-safe accessors for things you applied using the
plugins { ... }
block and that block is not available in legacy script plugins. You should instead use convention plugins in
buildSrc
or an included build, for example implemented as precompiled script plugins. Those are almost like normal build scripts.
g
.
whatever.gradle.kts
is fine as "build script context".
Ah, yeah, you right, I probably mixed it with settings.gradle.kts case