Nelson Tatius
Nelson Tatius

Reputation: 8043

What is the purpose of settings.gradle.kts?

What is the purpose of settings.gradle.kts? Why can't those propperties be initialized in build.gradle.kts? Why do we need a separate file for this?

Upvotes: 5

Views: 4699

Answers (1)

aSemy
aSemy

Reputation: 7139

Gradle works in phases

  1. Initialization
  2. Configuration
  3. Execution

settings.gradle.kts is used in the initialization phase. There's only one Settings object per Gradle build, and settings.gradle.kts is used to configure it.

Each Gradle build has at least one Project object, depending on how many subprojects there are. These are configured via build.gradle.kts files in the configuration phase.

Settings initialization

The Settings object must be configured first, and separately, because Gradle determines and configures a lot of properties and behaviours in the initialization phase, which cannot be determined on-the-fly in the configuration phase.

For example...

Multi-project builds

Gradle supports multi-project builds. Subprojects are declared in settings.gradle.kts, and that's how Gradle discovers other build.gradle.kts configurations.

Aside: Gradle can't automatically scan for nested build.gradle.kts files because

  • they might not be part of the current project, for example
    • if there's some integration test code that has a test build.gradle.kts.
    • if there's a nested project that's an composite build, and so not part of the current build.
  • a subproject might not have a build.gradle.kts file, which can happen if a subproject is configured via subprojects {} or allprojects{} (even though both are discouraged)
Gradle Build Cache

The Gradle Build cache is configured during the initialization phase, for an entire Gradle build.

Build plugins

Gradle plugins are usually used to configure a Gradle Project, but plugins can be used to configure anything, including the Gradle build.

One example of a build plugin is the Gradle Build Scan plugin, which measures the performance of Gradle builds. It wouldn't be possible to measure the performance correctly if the Gradle build had already started, so it must be applied before the Configuration phase.

But settings.gradle.kts isn't strictly required...

If a Gradle project isn't a multi-project build, then a settings.gradle.kts isn't strictly required. Gradle will still work, with sensible defaults, although one is recommended for improved performance, consistency, and clarity.

Upvotes: 4

Related Questions