Search code examples
androidkotlinandroid-gradle-plugingradle-kotlin-dsl

How to rewrite Groovy gradle task to Kotlin gradle scripts


I have these two gradle tasks written in groovy and I can't find a proper way how to rewrite them to 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
}

I am especially not sure about how to replace the "commandLine" method and how to add a "type:Exec" argument to task.


Solution

  • I believe the following should do the trick:

    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")
        }
    
    }
    

    Some links:


    1. Switch the example code blocks from Groovy to Kotlin.