Kotlin DSL¶
Code completion and type correctness depends on an up to date IDE sync¶
I run an IDE sync after almost every change to a plugin or build script if I am making subsequent changes that rely on it. This is because IDE auto-complete and type checking depend on having an updated classpath, which changes whenever you apply a new plugin or change some code in an applied plugin. It is annoying that it doesn't stay up-to-date automatically, as it does in application code. There is probably a reason for this that I don't understand.
I have a custom shortcut in my Intellij to trigger an IDE sync (I use cmd-shift-7).
The kotlin plugins all come from Maven Central¶
In order to use any kotlin plugin, you need this:
repositories {
mavenCentral()
}
You can't use plugin accessors outside of project build script¶
If you try to do this inside a kotlin-dsl plugin (i.e. in buildSrc or in an included build plugin), you might get this incorrect, unhelpful suggestion from Intellij:
If you are offered an import of the form gradle.kotlin.dsl.accessors._<some hash>
- don't take it. Instead what you need to do is use the<T>()
or extensions.getByName<T>(String extensionName)
to get the Gradle object you're looking for. The extension name is the same as the name that you would use in a project build script at the top level. For example if in a project build script you would write dockerApplications { ... }
, then you are configuring an extension called "dockerApplications". To get the type, I use IDEA's quick documentation shortcut (ctrl-J).
So in the image above where I am trying to access an extension called "dockerApplications" in my plugin, what I need to do is first define it within the plugin:
val dockerApplications = the<DockerApplicationExtension>()
Sometimes, the type will be more convoluted. For example, it could also have been this:
val dockerApplications = the<NamedDomainObjectContainer<DockerApplication>>()
Created on 2024-05-24
Updated on 2024-05-24