Are there any public Kotlin extensions for the Gra...
# gradle
e
Are there any public Kotlin extensions for the Gradle API? I find myself writing things like this when writing a plugin:
Copy code
inline fun <reified T : Any> PluginManager.apply() = apply(T::class.java)
inline fun <reified T : Plugin<*>> PluginContainer.getPlugin(): T = getPlugin(T::class.java)
inline fun <reified T : Task> TaskContainer.withType(): TaskCollection<T> = withType(T::class.java)
inline fun <reified T : Any> ExtensionContainer.findByType(): T? = findByType(T::class.java)
inline fun <reified T : Any> ExtensionContainer.configure(action: Action<T>) = configure(T::class.java, action)
Searching is difficult because because I just get hits on “how to write plugins” etc. Then I can write more “Kotlin-esque” code like this:
Copy code
plugin.pluginManager.apply<DetektPlugin>()
        plugin.extensions.configure<DetektExtension> { detekt ->
            // ...
        }
        val detekt = target.tasks.withType<Detekt>().first()
s
Have you added the
kotlin-dsl
plugin to your plugin project?
That should let your plugin source code access all the regular Kotlin extensions that are accessible from
build.gradle.kts
scripts
Couldn't find many docs, but here's a small mention of it: https://docs.gradle.org/7.4/userguide/kotlin_dsl.html#sec:kotlin-dsl_plugin
The key point to note is that the plugin adds the
gradleKotlinDsl()
dependency for you, which you could also do by hand if you prefer
e
I tried adding it like this:
Copy code
dependencies {
   // others...
   implementation("org.gradle.kotlin:gradle-kotlin-dsl-plugins:2.2.0")
}
But I don’t see ext functions like those in the imported jar.
v
Because that was not what Sam said
s
You should either add
Copy code
dependencies {
    implementation(gradleKotlinDsl())
}
or
Copy code
plugins {
    `kotlin-dsl`
}
☝️ 1
There's no need to specify a version number in either case, since the version is set automatically according to the version of Gradle
v
If you want to know the difference, the
kotlin-dsl
plugin does more than just adding that dependency.
kotlin-dsl
 applies 
java-gradle-plugin
kotlin-dsl.base
, and 
kotlin-dsl.precompiled-script-plugins
 plugins
kotlin-dsl.base
 plugin applies 
embedded-kotlin
, adds 
gradleKotlinDsl()
 to the dependencies of 
compileOnly
 and 
testImplementation
 configurations, and configures the Kotlin DSL compiler plugins for example for proper SAM conversion for 
Action
 and similar. So if you are just after having the Gradle Kotlin DSL classes in your dependencies, you can use 
gradleKotlinDsl()
 🙂
e
I think I want all of that, actually.
👌 1
🐕 1
Ok, cool. Thanks. Able to remove my custom ext functions now. 👍
🎉 1
👌 1
s
Sorry if my link to plugins.gradle.org was confusing, I realise now that the code examples there don't really match either of the solutions that I actually recommended
e
I seem have lost part of the std lib, though?
Copy code
inline fun <reified T : Any> KClass<T>.simpleClassName() = java.simpleName.replaceFirstChar { it.lowercase() }
It can’t resolve
replaceFirstChar
now.
s
The
kotlin-dsl
plugin will make sure you're using the version of Kotlin that corresponds to the version embedded in your Gradle version. It's possible that's an older version than what you were using before.
I think
replaceFirstChar
was added in 1.5
e
yeah, and I think this is still using 1.5. Maybe IntelliJ is confused. I’ll restart it.
🤞 1
e
Gradle 7.x uses Kotlin language/api targets 1.4 for backwards compatibility. We can only change that on a major version. In Gradle 8.0 we’ll move to the latest.
👍 1