Quick revision
The short answer
tasks.register is the lazy task configuration avoidance API: it registers the task's name and configuration, but Gradle only creates and configures the task if the build actually needs it. tasks.create creates and configures the task immediately on every build. In a large build with thousands of tasks, eager creation adds noticeable configuration time to every command, so we use register, configure existing tasks with tasks.named and configureEach, use Provider and Property types for inputs, and avoid tasks.getByName and eager iteration, which force creation.
What to remember
In this answer, we saw that tasks.register registers a task lazily, so Gradle creates and configures it only when the build needs it, while tasks.create creates and configures it on every build. In large builds with thousands of tasks, eager creation adds configuration time to every command. We configured existing tasks lazily with tasks.named and withType().configureEach, used Property and Provider types for inputs, and avoided getByName, tasks.all, forEach and the action form of withType, which force creation.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
tasks.register is the lazy task configuration avoidance API: it registers the task's name and configuration, but Gradle only creates and configures the task if the build actually needs it. tasks.create creates and configures the task immediately on every build. In a large build with thousands of tasks, eager creation adds noticeable configuration time to every command, so we use register, configure existing tasks with tasks.named and configureEach, use Provider and Property types for inputs, and avoid tasks.getByName and eager iteration, which force creation.
Why this question is asked
Two lines that look almost the same can make a big difference in a large build. With create, every task is built and configured on every invocation, whether it runs or not; with register, Gradle only does that work for tasks in the graph. Interviewers ask this to see whether we write build logic that scales and know which APIs quietly defeat the optimisation. In this answer, we will recap the configuration phase, compare the two calls with a small example, see why it matters at scale, configure existing tasks lazily, use lazy properties for inputs, and look at the APIs that force eager creation.
1A Quick Recap of the Configuration Phase
Gradle runs every build in three phases: initialization decides which projects take part, configuration runs the build scripts and sets up tasks, and execution runs the tasks we asked for. The configuration phase runs for every command, even gradle help, so anything done there is paid for every time.
A task in Gradle is an object with a name, configuration such as inputs and outputs, and actions. Creating and configuring that object costs time: running the configuration block, applying conventions, and often resolving values. The question is whether that cost is paid for every task in the build or only for the tasks the build will actually run.
2Register Versus create on a Small Example
We can see the difference by printing from the configuration block of each task.
| Aspect | tasks.register | tasks.create |
|---|---|---|
| Returns | TaskProvider, a lazy handle | Task, the real object |
| Task created | Only if needed by the graph | Always, immediately |
| Configuration block runs | Only if the task is realised | On every build |
| Cost in gradle help | None | Full cost for every such task |
| Recommended | Yes | No, legacy eager API |
Notice that eagerReport is configured even for gradle help and for lazyReport, which have nothing to do with it. lazyReport is configured only when it is part of the task graph. register returns a TaskProvider, a handle to a task that may not exist yet, while create returns the Task object itself, which is why it must build it immediately. The eager API is discouraged in current Gradle versions; check your version's deprecation notes.
3Why It Matters in Large Builds
One eager task costs a few milliseconds. But large builds have many projects, and plugins add many tasks to each one: compile, test, jar, javadoc, checkstyle and so on. A build with 200 projects can easily have several thousand tasks. If they were all created eagerly, with configuration blocks that resolve values or touch the file system, configuration could take many seconds, paid on every command and every IDE sync, even when we only run one project's tests.
With register, Gradle realises, meaning creates and configures, only the tasks in the graph for the current command. Running tests in one project might realise a few dozen tasks instead of thousands. That is why Gradle calls this API task configuration avoidance, and why the built-in plugins use it.
4Configuring Existing Tasks Lazily
We often need to change tasks that plugins created, such as the test task. The lazy way is tasks.named for one task and withType with configureEach for a group. Both record the configuration and apply it only when the task is realised.
Notice the subtle case in the middle. withType followed by configureEach is lazy, but passing the action straight to withType is eager. A single eager call like tasks.all inside a plugin applied to every project can undo the benefit for the whole build, which is why it is worth checking build plugins as well as build scripts.
5Lazy Inputs With Provider and Property
Laziness should continue inside the task. If a task's inputs are computed during configuration, the cost returns. Gradle's Property and Provider types hold values that are computed only when needed, and they let Gradle track inputs and connect one task's output to another task's input.
- Creating a task?YesUse tasks.registerNoGo on
- Configuring an existing task?Yestasks.named or withType().configureEachNoGo on
- Calling getByName, all, forEach or withType with an action?YesEager: replace itNoGo on
- Inputs computed at configuration time?YesUse Property and ProviderNoLazy enough
4 steps in order. Play them, or step through them one at a time.
Notice the last task. By using greet's output provider as an input, Gradle knows that publishGreeting depends on greet, without an explicit dependsOn, and still without creating greet until it is needed. The direct assignment syntax for properties works in recent Gradle versions with the Kotlin DSL; older versions use message.set(...).
6Follow-up questions
How can you check whether tasks are being created eagerly?
A build scan shows how many tasks were created during configuration and which ones, and comparing that with the number of tasks that actually ran reveals eager creation. A quick check is a println in a task's configuration block and running gradle help: if it prints, something realises the task eagerly.
Is it ever necessary to realise a task during configuration?
Rarely. Most needs, such as wiring dependencies, reading outputs or configuring other tasks, can be expressed with providers, named and configureEach. If a plugin truly needs a task object immediately, it is usually a sign the plugin's design should change, because it will slow every build that applies it.
Does register mean a task's dependencies are also lazy?
Yes. Dependencies declared through providers, such as dependsOn(otherTaskProvider) or using another task's output provider as an input, are recorded without creating the other task. Gradle creates both only when they are needed in the task graph.
7Conclusion
In this answer, we saw that tasks.register registers a task lazily, so Gradle creates and configures it only when the build needs it, while tasks.create creates and configures it on every build. In large builds with thousands of tasks, eager creation adds configuration time to every command. We configured existing tasks lazily with tasks.named and withType().configureEach, used Property and Provider types for inputs, and avoided getByName, tasks.all, forEach and the action form of withType, which force creation.
Open with
The difference is when the task object is created and configured. tasks.register is the task configuration avoidance API: it records the name, type and configuration block and returns a TaskProvider, and Gradle only creates and configures the task when it is actually needed in the task graph. tasks.create creates the Task object and runs its configuration block immediately, on every build, even gradle help.
Then, if they want more
On a small build that does not matter, but large builds have many projects and plugins add many tasks to each, so you can easily have thousands of tasks. If they are all configured eagerly, configuration takes seconds on every command and every IDE sync. With register, running one project's tests only realises the few dozen tasks that are needed.
So in practice I always use register for new tasks. For tasks that plugins create, I use tasks.named for one task, and withType followed by configureEach for a group, which are also lazy. I avoid tasks.getByName, tasks.all, iterating over tasks and passing an action directly to withType, because they all force tasks to be created.
And inside tasks I use Property and Provider types for inputs, so values are only computed at execution, and wiring one task's output provider into another task's input gives the dependency automatically without creating anything early.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Gradle user manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is the difference between tasks.register("x") and tasks.create("x") in Gradle, and...": https://scrivio-interview-library.netlify.app/java/gradle/c9338c6a3c/