How to use correctly org.gradle.api.provider.Property in a plugin extension

Hello, i have a little problem with plugin extension in kotlin:

i try to define a extension with a property like this:

var myStringProperty: Property = project.objects.property(String::class.java)

The extension class is just a pojo,
but when i try to use the extension in a build.gradle.kts:

[myextensionname]{
myStringProperty=“test”
}

i always have this message:
Type mismatch: inferred type is String but Property was expected

the Property class can only be used in Task classes ?

Kotlin DSL does not support assignment as you are attempting, you need to use the set method of the property.

myExt {
  myStringProperty.set("test")
}

The assignment, myStringProperty = "test" is trying to set the value of the kotlin property/field instead of the value held by the Property instance, which is why you get an error saying it cannot assign a String to a Property.

The Groovy DSL supports magic that converts assignment to a set call.

Also; You most likely never want the Property instance to change in the extension and it should be final/read-only (val instead of var).

Actually, in 8.1 the assignment syntax will finally work also in Kotlin DSL.
Here the dogfood PR that makes Gradles own build use that already: https://github.com/gradle/gradle/pull/23547 :slight_smile:

1 Like

Well, experimentally at least, needs to be opted-in with a system property still. But at least it moves. :slight_smile: