Gradle Plugin with Property<Boolean> in Kotlin does not work

plugins

(mat) #1

I try to write a plugin with Kotlin. This is the code of my extension

private val serverSideProperty: Property<Boolean> = project.objects.property(Boolean::class.java)
val serverSideProvider: Provider<Boolean>
        get() = serverSideProperty

var serverSide : Boolean
       get()= serverSideProperty.get()
       set(value) = this.serverSideProperty

If I run a test, the following error occurred:

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

How can I solve this issue?


(Daniel Lacasse) #2

Thanks for the question @M_Raab. Could you provide a bit more information on the usage of your extension? It would seem the issue is related to how the extension is used as opposed to how it’s declared.


(mat) #3

It seems that Gradle handed over java.lang.Boolean to the configuration, but the type of the extension in Kotlin is declared as Kotlin.Boolean. If you use java.lang.Boolean it will work, but the compiler means, that java.lang.Boolean should not used in Kotlin.
The same occurs with integer types.


(Daniel Lacasse) #4

In your code sample above, you are using Boolean::class.java which resolve to the primitive java type boolean. Java template can only use boxed type for generic classes.

As a workaround, use the reified generic overloads the Kotlin DSL provides for now. Look at the code sample below:

private val serverSideProperty = project.objects.property<Boolean>()

val serverSideProvider: Provider<Boolean>
        get() = serverSideProperty


var serverSide : Boolean
       get()= serverSideProperty.get()
       set(value) = serverSideProperty.set(value)

serverSide = true
println(serverSideProvider.get())

I hope this helps you solve your issues.


(mat) #5

Thank You for this idea. It works … Great.