简体   繁体   中英

java.lang.VerifyError creating a gradle task with kotlin

I try to write a gradle task with kotlin, this is my code.:

GreetingTask.kt

class GreetingTask : DefaultTask() {
    @TaskAction
    fun greet() {
        println("greet!")
    }
}

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:0.12.613"
    }
}

apply plugin: "kotlin"

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:0.12.613"
    compile gradleApi()
}

GreetingTaskTest

class GreetingTaskTest {

    @Test
    public fun canAddTaskToProject() {
        val project = ProjectBuilder.builder().build()
        val task = project.task(hashMapOf("type" to javaClass<GreetingTask>()), "greeting")
        assertTrue(task is GreetingTask)
    }
}

When running the test now it results in a:

java.lang.VerifyError at GreetingTaskTest.kt:20
// reason -> Cannot inherit from final class

Which is this line:

val task = project.task(hashMapOf("type" to javaClass<GreetingTask>()), "greeting")

What i would like to know is:

Where does this issue come from and how do i fix it?

Classes in Kotlin are final by default, compared to open in java.

Declare the class GreetingTask as "open" and this error message is gone.

open class GreetingTask : DefaultTask() { 
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM