Hi there :wave::skin-tone-3: Wondering if anyone ...
# gradle
c
Hi there 👋🏼 Wondering if anyone can help me with a problem I'm struggling to work around. I have this super lightweight kotlin program that executes some script, thats a separate module in my android project. I can run this kotlin program via the command line:
./gradlew :myprogram:run
however I want to run this as part of the pre-defined
build
gradle task. In other words, I always want this to run when someone tries to build the android app, and prior to that app compiling. Any suggestions on how I would do that? Some things I have tried 🧵
1. Trying to get the gradle task and make it depend on pre-build:
Copy code
tasks.named(":myProgram:run").also { tasks.named("build").dependsOn(this) }
But I get an error that task
:myProgram:run
cannot be found.
2. Registering a new gradle task and trying to run the task on the command line:
Copy code
tasks.register("runMyProgram") {
    exec {
        commandLine("./gradlew :myProgram:run")
    }
    tasks.named("build").dependsOn(this)
}
im making these declarations above in the projects root gradle file
a
this code will first try to fetch the task that's defined in the subproject, but because the root build.gradle.kts file is evaluated first, Gradle won't have had a chance to create the
:myProgram:run
task yet
Copy code
tasks.named(":myProgram:run").also { tasks.named("build").dependsOn(this) }
Instead you can add a dependency based on the task path
Copy code
tasks.build {
  dependsOn(":myProgram:run")
}
(and using the generated Kotlin DSL accessor,
tasks.build
, for the build task)
c
oh nice, thanks this works perfectly!
fist bump 1