gradle plugin with kotlin - set boolean extension property
Asked Answered
A

1

6

I am trying to migrate a gradle plugin from groovy to kotlin. I have a boolean Property that I would like to initialize to false in my plugin extension:

open class MyPluginExtension(project: Project) {

    val myBooleanProperty: Property<Boolean> = project.objects.property(Boolean::class.java)

    init {
        myBooleanProperty.set(false)
    }
}

This fails with

java.lang.IllegalArgumentException: Cannot set the value of a property of type boolean using an instance of type java.lang.Boolean.

The exception is thrown in org.gradle.api.internal.provider.DefaultPropertyState#set(T)

Any thoughts on this?

Anabolite answered 6/12, 2017 at 8:6 Comment(1)
Looks like a bug, have you searched on github.com/gradle/kotlin-dsl/issues to see if it's been already filed?Cantatrice
C
1

Boolean::class.java in Kotlin refers to the primitive type. Try specifying the Object type instead:

val myBooleanProperty: Property<Boolean> = project.objects.property(Boolean::class.javaObjectType)
Crashaw answered 18/8, 2018 at 18:20 Comment(1)
Yes that is probably the reason. I cannot verify your answer anymore because the project has gone into a different direction in the meantime.Anabolite

© 2022 - 2024 — McMap. All rights reserved.