Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Describe Gradle's initialization, configuration and execution phases. Why is doing work like reading files or calling the network at configuration time a problem?

Gradle runs every build in three phases: initialization reads the settings file and decides which projects take part, configuration runs the build scripts and builds the task graph, and execution runs the selected tasks and their dependencies. Configuration code runs on every invocation, even gradle help, so slow work like reading files or calling the network there slows every build. Such side effects also break the configuration cache and make builds non-reproducible, so that work belongs in task actions or in lazy providers resolved at execution time.

Why this question is asked

Gradle build scripts look like ordinary programs, which makes it easy to write code that runs at the wrong time. A line that reads a file or calls a server at the top of build.gradle.kts seems harmless, until every command, including opening the project in the IDE, waits for it. Interviewers ask this because understanding the phases is the basis of fast, correct Gradle builds. In this answer, we will walk through the three phases, see when each piece of a build script runs, reproduce the configuration-time problem, and fix it with task actions and providers.

1The Three Phases of Every Build

In the initialization phase, Gradle finds and runs the settings file, settings.gradle.kts. It decides which projects belong to the build, through include statements for a multi-project build, and creates a Project object for each.

In the configuration phase, Gradle runs the build script of each project. Running the script registers and configures tasks, applies plugins and declares dependencies. From this, Gradle builds the task graph: which tasks exist and which tasks depend on which.

In the execution phase, Gradle takes the tasks we asked for on the command line, such as build, works out from the graph which other tasks they need, and runs their actions in order, skipping tasks whose inputs and outputs have not changed.

Step throughWhat happens when we run gradle build
  1. Initializationrun settings.gradle.kts, decide which projects take part
  2. Configurationrun each build.gradle.kts, register tasks, build the task graph
  3. Configuration cache hit?
    YesSkip configuration next time and reuse the graphNoConfiguration runs again on the next build
  4. Executionrun the actions of build and the tasks it depends on

4 steps in order. Play them, or step through them one at a time.

2When Each Piece of a Script Runs

The key to the phases is that code in a build script runs at different times depending on where it is. Top-level statements and the bodies of configuration blocks run during configuration. Only code inside doFirst or doLast, the task's actions, runs during execution, and only if the task actually runs.

Java
// build.gradle.kts
println("A: top level") // configuration, every build
tasks.register("hello") {
println("B: configuring hello") // configuration, only if hello is needed
doLast {
println("C: running hello") // execution, only when hello runs
}
}
$ gradle help
# A: top level
$ gradle hello
# A: top level
# B: configuring hello
# > Task :hello
# C: running hello

Notice that A prints even for gradle help, which has nothing to do with our task. That is the trap: whatever we put at the top level, or in an eagerly configured block, is paid for on every single invocation, including the IDE's project sync.

3Reproducing the Problem

Suppose a build needs the current Git commit for a version file and reads a release notes file. A natural first attempt does this directly in the script.

Java
// build.gradle.kts, the slow and fragile way
val gitSha = ProcessBuilder("git", "rev-parse", "HEAD")
.start().inputStream.bufferedReader().readText().trim() // runs every build
val notes = File("RELEASE_NOTES.md").readText() // runs every build
val latest = java.net.URI("https://releases.acme.example/latest").toURL().readText() // network call!
tasks.register("writeVersion") {
doLast { file("build/version.txt").writeText("$gitSha $latest") }
}
$ gradle help
# BUILD SUCCESSFUL in 4s # 3 seconds of it waiting for the network

Every command now runs git, reads a file and calls a server. If the server is down, gradle help fails. And the build is not reproducible: the same commit produces different results depending on what the server returned at configuration time.

4Why It Also Breaks the Configuration Cache

The configuration cache is a Gradle feature that saves the result of the configuration phase, the task graph with all its configured values, and reuses it on later builds when nothing that affects configuration has changed. On a large build, it can turn several seconds of configuration into almost nothing.

For that to be correct, Gradle must know everything the configuration depends on. When a script reads a file or runs a process directly, Gradle either cannot see that dependency, or treats it as an input that invalidates the cache every time, and it reports such code as a problem when the configuration cache is enabled. A network call is the worst case, because its result can change with no change to any file. So configuration-time side effects make builds both slower and less trustworthy.

5The Fix: Task Actions and Lazy Providers

Work that a task needs belongs in the task's action, or in a provider, a value Gradle computes lazily when it is actually needed and that it can track as an input. Gradle offers providers for the common cases: reading files, running external processes, reading environment variables and Gradle properties.

Java
// build.gradle.kts, the lazy way
val gitSha = providers.exec {
commandLine("git", "rev-parse", "HEAD")
}.standardOutput.asText.map { it.trim() } // not run yet
val notes = providers.fileContents(layout.projectDirectory.file("RELEASE_NOTES.md")).asText
tasks.register("writeVersion") {
val out = layout.buildDirectory.file("version.txt")
inputs.property("sha", gitSha) // tracked input
outputs.file(out)
doLast {
out.get().asFile.writeText(gitSha.get()) // resolved at execution
}
}
$ gradle help
# BUILD SUCCESSFUL in 0.6s # nothing runs at configuration time

Now gradle help does no work at all, and writeVersion only runs git when it runs. Because the provider is declared as an input, Gradle can skip the task when the commit has not changed, and the configuration cache knows exactly what it depends on. The network call is gone from the build entirely; if a build really needs remote data, a dedicated task with declared outputs downloads it during execution, so the result is visible and cacheable.

6Follow-up questions

Is configuration time really significant in practice?

In small builds it is a fraction of a second, but it grows with the number of projects, plugins and eagerly configured tasks. Large multi-project builds often spend many seconds configuring, which is paid on every command and every IDE sync. That is why Gradle added task configuration avoidance and the configuration cache, and why build scans and --profile show configuration time separately.

How do you find configuration-time work in an existing build?

Run a build with --configuration-cache, which reports scripts and plugins that do unsupported work at configuration time, and look at a build scan or --profile output for slow configuration of particular projects. Adding a temporary println at the top of a script is a quick way to confirm when code runs.

What is the difference between doFirst and doLast?

Both add actions to a task that run during execution. doFirst adds an action at the start of the task's action list and doLast at the end. For a task we define ourselves, doLast is the usual choice; doFirst is mostly used to add something before the existing actions of a plugin's task.

7Conclusion

In this answer, we saw that Gradle runs initialization to decide which projects take part, configuration to run build scripts and build the task graph, and execution to run the selected tasks. Code at the top level of a script or in configuration blocks runs on every build, so reading files, running processes or calling the network there slows every command, breaks the configuration cache and makes builds non-reproducible. The fix is to put such work in task actions or lazy providers resolved at execution time.

What to say in the interviewAbout 90 seconds aloud

Open with

Gradle builds go through three phases. In initialization, it runs the settings file and decides which projects take part in the build. In configuration, it runs the build script of every project, registers and configures tasks, applies plugins and builds the task graph.

Then, if they want more

In execution, it runs the tasks I asked for and the tasks they depend on, skipping the ones that are up to date. The thing to understand is that top-level script code and configuration blocks run during configuration, on every invocation, even gradle help or an IDE sync.

Only code inside doFirst or doLast runs at execution, and only if the task runs. So if a script reads a file, runs git or calls a server at the top level, every command pays for it, and if the server is down even gradle help fails.

It also hurts correctness: the configuration cache, which saves the configured task graph to skip configuration next time, cannot reuse a configuration that depends on untracked side effects, and the build is not reproducible if a network value can change.

The fix is to move the work into task actions or into lazy providers, for example providers.exec for git or providers.fileContents for a file, declared as task inputs and resolved only at execution time. Then configuration stays fast and the cache can work.

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 "Describe Gradle's initialization, configuration and execution phases. Why is doing work...": https://scrivio-interview-library.netlify.app/java/gradle/f726ce114d/