Custom Tasks
Automate work.
Custom Tasks is a free Kotlin Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is a Gradle Task?
A task is a unit of build work — compiling, copying files, running a tool. Custom tasks let you automate project-specific chores.
Registering a Task
Use tasks.register with the configuration-avoidance API. It is preferred over the older tasks.create because it is lazy.
tasks.register("hello") {
doLast {
println("Hello from Gradle")
}
}doFirst and doLast
Task actions run in the execution phase. doFirst prepends an action, doLast appends one. Code outside them runs during configuration.
tasks.register("greet") {
doFirst { println("start") }
doLast { println("end") }
}Task Description and Group
Set group and description so the task shows up neatly in ./gradlew tasks.
tasks.register("cleanLogs") {
group = "maintenance"
description = "Deletes generated log files"
doLast { /* ... */ }
}Typed Tasks
Register a task of a built-in type to reuse behavior. Copy copies files declaratively.
tasks.register<Copy>("copyDocs") {
from("src/docs")
into("build/docs")
}Task Dependencies
Express ordering with dependsOn. The dependency runs first.
tasks.register("deploy") {
dependsOn("build")
doLast { println("deploying") }
}Configuring an Existing Task
Reach into a pre-registered task (added by a plugin) and tweak it.
tasks.named("test") {
// configure the existing test task
}
tasks.test {
maxParallelForks = 2
}Task Inputs and Outputs
Declaring inputs and outputs enables up-to-date checking: Gradle skips a task when nothing changed.
tasks.register("generate") {
inputs.file("data/template.txt")
outputs.file("build/generated.txt")
doLast { /* generate */ }
}Incremental Builds
With proper inputs/outputs, re-running an unchanged task prints UP-TO-DATE and does no work. This is central to Gradle's speed.
Custom Task Classes
For reusable logic, define a class extending DefaultTask with a @TaskAction method.
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
abstract class GreetTask : DefaultTask() {
@TaskAction
fun run() {
println("Hello")
}
}Running Your Task
Invoke any registered task by name with the wrapper.
// ./gradlew hello
// ./gradlew copyDocs
// ./gradlew deployQuick Check
You want an action to run during the execution phase, after the task's other work. Which block do you use?
Recap
You can automate work with custom tasks:
tasks.register+doFirst/doLast- Typed tasks like
Copy,dependsOnfor ordering - Declare
inputs/outputsfor up-to-date checking - Custom
DefaultTaskclasses with@TaskAction
Next: version catalogs.
Frequently asked questions
Is the “Custom Tasks” lesson free?
Yes — the full text of “Custom Tasks” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Custom Tasks”?
Automate work. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Custom Tasks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.