如何将Groovy gradle任务重写为Kotlin gradle脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将Groovy gradle任务重写为Kotlin gradle脚本相关的知识,希望对你有一定的参考价值。

我有这两个用groovy编写的gradle任务,我找不到如何将它们重写为Kotlin的正确方法:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}


task clearAppCache(type: Exec) {
    def clearDataCommand = ['adb', 'shell', 'pm', 'clear', 'io.hruska.pocketplay']
    commandLine clearDataCommand
}

我特别不确定如何替换“commandLine”方法以及如何向task添加“type:Exec”参数。

答案

我相信以下应该可以做到这一点:

tasks {

    // will need to import KotlinCompile or use the fully qualified name
    withType<KotlinCompile> {
        kotlinOptions.jvmTarget = "1.8"
    }

    register<Exec>("clearAppCache") {
        commandLine = listOf("adb", "shell", "pm", "clear", "io.hrushka.pocketplay")
    }

}

一些链接:


1.将示例代码块从Groovy切换到Kotlin。

以上是关于如何将Groovy gradle任务重写为Kotlin gradle脚本的主要内容,如果未能解决你的问题,请参考以下文章