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

问题描述 投票:0回答:1

我有这两个用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”参数。

android kotlin android-gradle gradle-kotlin-dsl
1个回答
3
投票

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

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。

© www.soinside.com 2019 - 2024. All rights reserved.