Using Kotlin extension properties & functions from Kotlin DSL in my plugin's Kotlin code

In my plugin’s Kotlin code, I want to use Kotlin extension properties & functions that are available in the Kotlin DSL.

How can I use them?

I’m using the Gradle 5.6.2 Kotlin DSL. My plugin is written completely in Kotlin, and I’ve applied the following plugins (amongst a few others) to my plugin’s build:

`java-gradle-plugin`
kotlin("jvm")                   version "1.3.50"
id("com.gradle.plugin-publish") version "0.10.1"

e.g., I’d like to use the val SourceSet.kotlin extension property from:

I’d also like to use the val Project.sourceSets extension property from:

Hello you can use this dependency in build.gradle.kts of your plugin:
implementation(gradleKotlinDsl())
and then use extensions in code with using this import
import org.gradle.kotlin.dsl.*

1 Like

@drichter Thanks. I was only able to get certain properties & functions from implementation(gradleKotlinDsl()) & import org.gradle.kotlin.dsl.*

It found the the function, which is useful, but not the sourceSet property. From a build.gradle.kts, IntelliJ takes me from project.sourceSets to an extension property in org.gradle.kotlin.dsl, but it appears that Gradle generates the containing file (named Accessors2oadk7let745pm8ahqypkqzlk.kt), instead of it being in the dependency jars.

The Project.sourceSets extension property that I linked to in my initial post is from a different package, accessors, from a different file, conventions.kt.

Is there any way to import either of these extension properties?

Sorry I dont think you will be able to use dynamically generated Accessors* extensions, because they are generated when gradle is run and put to build script classpath according to settings in build script afaik. So you cannot add it as dependency because it is not known at plugin compilation time.

That’s right, you don’t get generated accessors in .kt files. But you can also use .gradle.kts files in your plugin project, and they do get generated accessors.

See Precompiled script plugins in the doc.

Hi @eskatos.
I’m writing Gradle plugin in Kotlin and I’m looking for a way how to extend some existing object. For example

fun Project.example1() {
    println("example")
}

or Android specific one

fun ProductFlavor.setSignKey(remoteSignKeyName: RemoteSignKeyName) {
    this.extra["signKeyName"] = remoteSignKeyName
}

But I didn’t found a way how to do that. Your response is the most promising thing that I found so far.
There is possible to use project.extensions.create which generates accessors in kt file event without using gradle.kts. There is really no way how to do something similar for any other class, directly from .kt files?

But I tried also you suggested solution via .gradle.kts and it also doesn’t work for me.
I simply put KotlinExtension.gradle.kts file to my plugin scr/main/kotlin folder and inside of project I can use

plugins {
    ...
    id("KotlinExtension")

This par works. But if I put something like

fun Project.example1() {
    println("example")
}

to this .gradle.kts file i still can call example1() method on project object