https://kotlinlang.org logo
n

nikola

02/24/2021, 5:57 AM
Hi all. I am trying to migrate from build.gradle (Groovy) to build.gradle.kts and I am stuck with translating certain part related to tasks. When running build I do not see this task executing. What am I doing wrong?
Copy code
// -----------Groovy Old
task generateCinteropConfig(type: Exec) {
    workingDir "${projectDir}"
    commandLine 'sh', "${projectDir}/generate_cinterop_conf.sh"
}

gradle.taskGraph.beforeTask { Task task ->
    if (task.project.name.toLowerCase().contains("ios")) {
        "sh ${projectDir}/generate_cinterop_conf.sh ${projectDir}".execute().text
    }
}

// ------------Kotlin DSL New
task<Exec>("generateCinteropConfig") {
    workingDir = File("${projectDir}")
    commandLine = listOf("sh", "${projectDir}/generate_cinterop_conf.sh")
}

tasks.register("generateCinteropConfig") {
    doFirst() {
        if (this.project.name.toLowerCase().contains("ios")) {
            Runtime.getRuntime().exec("${projectDir}/generate_cinterop_conf.sh ${projectDir}")
        }
    }
}
1
k

Kris Wong

02/24/2021, 1:59 PM
in your second block you are just registering a task
unless some other task is dependent on it, it won't execute unless you specify it on the command line
n

nikola

02/24/2021, 2:03 PM
I wanted this to be a prebuild task. How can I specify that this should execute before anything else in the build.gradle.kts?
k

Kris Wong

02/24/2021, 2:13 PM
Copy code
val myTask = tasks.register<Exec>("myTask") {}

tasks.named("preBuild") {
    dependsOn(myTask)
}
that might not be 100% right, but you get the idea
n

nikola

02/24/2021, 5:52 PM
@Kris Wong thank you for your help, I managed to make it work Here is the final solution:
Copy code
val generateCInteropConfig = task<Exec>("generateCinteropConfig") {
    workingDir = File("${projectDir}")
    commandLine = listOf("sh", "${projectDir}/generate_cinterop_conf.sh")
}

tasks.named("preBuild") {
    dependsOn(generateCInteropConfig)
}
🍻 1
9 Views