skip to content

How does Gradle locate and load settings.gradle.kts and each build.gradle.kts, and how does that affect the root project name?

level: middleimportance: should knowfreq 35%

answer

  1. upward search for settings file
  2. settings dir = build root
  3. root name defaults to dir name
  4. set rootProject.name explicitly
  5. buildFileName overridable per descriptor

basics

~10 s

Gradle searches upward from the current directory for settings.gradle(.kts) to find the build root. Each project loads build.gradle.kts from its projectDir. The root project name defaults to the root directory name.

solid answer

~50 s

When you run Gradle, it looks for `settings.gradle.kts` (or `.gradle`) starting in the current directory and walking up, to determine the **build root** — the directory containing settings is the root project's directory. That's how Gradle knows where the multi-project build begins. The `Settings` script then declares subprojects via `include`, and for each declared project (plus the root) Gradle loads a `build.gradle.kts` from that project's `projectDir` (default name `build.gradle.kts`, overridable via the project descriptor's `buildFileName`). The **root project name** is significant: by default it's the *name of the root directory*, which is fragile (rename the folder, rename the project). That's why you almost always set `rootProject.name = "..."` explicitly in settings — it stabilises artifact coordinates and project paths regardless of the checkout directory name. Subproject names default to their directory names unless changed in the descriptor.

code

kotlin · 5 lines
kotlin
// settings.gradle.kts
rootProject.name = "shop"
include(":catalog")
project(":catalog").projectDir = file("modules/catalog")
project(":catalog").buildFileName = "catalog.gradle.kts"

go deeper

for a junior

Know that settings marks the build root and each project has its own build.gradle.kts.

for a middle

Explain upward search for the settings file and why you should set rootProject.name explicitly.

for a senior

Discuss buildFileName/projectDir overrides on descriptors and the consequences of a directory-derived root name for artifact coordinates.

for a principal

Tie stable project identity to reproducible builds, CI checkout independence, and consistent artifact coordinates across the org.

## Finding the build root Gradle determines where a build starts by searching for a **settings file** (`settings.gradle.kts` or `settings.gradle`). It checks the current directory and then walks up the directory tree. The directory that contains the settings file is the **root project directory**, and that settings file defines the whole build. If no settings file is found, Gradle treats the current directory as a single-project build with a synthesised default `Settings`. ## Loading build scripts For the root and each `include`d subproject, Gradle resolves a `projectDir` and loads its build script: - Default build-script name: `build.gradle.kts` (Kotlin) or `build.gradle` (Groovy). - A project with no build script is still a valid project — it just has no per-project configuration. - You can override the file name per project: `project(":legacy").buildFileName = "legacy.gradle.kts"` (set on the `ProjectDescriptor` in settings). ## Root project name — why set it ```kotlin // settings.gradle.kts rootProject.name = "shop" // explicit and stable include(":catalog", ":checkout") ``` If you omit `rootProject.name`, Gradle uses the **root directory's name**. That couples your project identity (used in artifact group/name, project paths, and reproducible builds) to whatever the folder happens to be called on a given machine or CI checkout. Setting it explicitly is a best practice. Subproject names default to their directory names; `include(":catalog")` expects a directory `catalog` and names the project `catalog`. You can remap via `project(":catalog").name`/`projectDir` in the descriptor. ## Practical takeaway - One settings file marks and configures the build root (found by upward search). - Each project loads its own build script from its projectDir; the file name is overridable. - Always set `rootProject.name` so identity doesn't depend on the checkout folder.

  • What is the default root project name if you don't set rootProject.name?
    The name of the root project directory (the folder containing settings.gradle.kts), which makes project identity depend on the checkout folder name.
  • Can a project exist without a build.gradle.kts?
    Yes. A project with no build script is valid; it simply contributes no per-project configuration. Only settings is mandatory to declare structure.

saying these in an interview costs you the question

  • Saying Gradle finds settings by searching downward into subdirectories (it searches upward).
  • Claiming every project must have a build script.

context