Using type-safe accesors on a settings plugin

Is it possible to use type-safe accesors on a custom settings plugin in the settings.gradle.kts?

I have no success trying with a composite build and gradle version 7.5.1

Here is a sample code GitHub - evantill/gradle-testing-settings-plugin: Reprocase for using type-safe accessors in Settings

And where I can not use the type-safe accesor :

No, that’s a missing feature requested at Settings plugin extension usable from Groovy but not Kotlin · Issue #11210 · gradle/gradle · GitHub, so thumbs-up and follow it.

As a work-around you can provide the missing accessor yourself.
Either in your own package, for example adding in SomePluginPlugin.kt after the class

fun Settings.something(configuration: Action<SomeExtension>) =
    extensions.configure(SomeExtension::class.java, configuration)

val Settings.something
    get() = extensions.findByType(SomeExtension::class.java)!!

and then in your settings script you can

import some.plugin.something

and then use it like you expected, or you can go the dirty way and create the accessors within the org.gradle.kotlin.dsl package, then you can use them without import.

I personally would probably go the slightly cleaner way of not doing anything in the Kotlin namespace but require an import on usage.

The drawback on both approaches is, that the accessors are always available if the plugin is in the classpath, even if the plugin is not applied, but that is probably not a major problem.

1 Like

Thank you for the detailed explanation.