# General Gradle Best Practices

version 9.6.0-20260429005316+0000

## Use Kotlin DSL

Prefer the Kotlin DSL (`build.gradle.kts`) over the Groovy DSL (`build.gradle`) when authoring new builds or creating new subprojects in existing builds.

### Explanation

The Kotlin DSL offers several advantages over the Groovy DSL:

- Strict typing: IDEs provide better auto-completion and navigation with the Kotlin DSL.

- Improved readability: Code written in Kotlin is often easier to follow and understand.

- Single-language stack: Projects that already use Kotlin for production and test code don’t need to introduce Groovy just for the build.

Since Gradle 8.0, Kotlin DSL is the default for new builds created with `gradle init`. Android Studio also defaults to Kotlin DSL.

### References

- Kotlin DSL Primer

### Tags

`#kotlin-dsl`

## Use the Latest Minor Version of Gradle

Stay on the latest minor version of the major Gradle release you’re using, and regularly update your plugins to the latest compatible versions.

### Explanation

Gradle follows a fairly predictable, time-based release cadence. Only the latest minor version of the current and previous major release is actively supported.

We recommend the following strategy:

- Try upgrading directly to the latest minor version of your current major Gradle release.

- If that fails, upgrade one minor version at a time to isolate regressions or compatibility issues.

Each new minor version includes:

- Performance and stability improvements.

- Deprecation warnings that help you prepare for the next major release.

- Fixes for known bugs and security vulnerabilities.

Use the `wrapper` task to update your project:

    ./gradlew wrapper --gradle-version <version>

You can also install the latest Gradle versions easily using tools like SDKMAN! or Homebrew, depending on your platform.

### Plugin Compatibility

Always use the latest compatible version of each plugin:

- Upgrade Gradle before plugins.

- Test plugin compatibility using shadow jobs.

- Consult changelogs when updating.

Subscribe to the Gradle newsletter to stay informed about new Gradle releases, features, and plugins.

### References

- Upgrade Guide

### Tags

`#plugins`

## Apply Plugins Using the `plugins` Block

You should always use the `plugins` block to apply plugins in your build scripts.

### Explanation

The `plugins` block is the preferred way to apply plugins in Gradle. The plugins API allows Gradle to better manage the loading of plugins and it is both more concise and less error-prone than adding dependencies to the buildscript’s classpath explicitly in order to use the `apply` method.

It allows Gradle to optimize the loading and reuse of plugin classes and helps inform tools about the potential properties and values in extensions the plugins will add to the build script. It is constrained to be idempotent (produce the same result every time) and side effect-free (safe for Gradle to execute at any time).

### Example

#### Don’t Do This

build.gradle.kts

    buildscript {
        repositories {
            gradlePluginPortal() (1)
        }

        dependencies {
            classpath("com.google.protobuf:com.google.protobuf.gradle.plugin:0.9.4") (2)
        }
    }

    apply(plugin = "java") (3)
    apply(plugin = "com.google.protobuf") (4)

build.gradle

    buildscript {
        repositories {
            gradlePluginPortal() (1)
        }

        dependencies {
            classpath("com.google.protobuf:com.google.protobuf.gradle.plugin:0.9.4") (2)
        }
    }

    apply plugin: "java" (3)
    apply plugin: "com.google.protobuf" (4)

1 | Declare a Repository: To use the legacy plugin application syntax, you need to explicitly tell Gradle where to find a plugin.
2 | Declare a Plugin Dependency: To use the legacy plugin application syntax with third-party plugins, you need to explicitly tell Gradle the full coordinates of the plugin.
3 | Apply a Core Plugin: This is very similar using either method.
4 | Apply a Third-Party Plugin: The syntax is the same as for core Gradle plugins, but the version is not present at the point of application in your buildscript.

#### Do This Instead

build.gradle.kts

    plugins {
        id("java") (1)
        id("com.google.protobuf").version("0.9.4") (2)
    }

build.gradle

    plugins {
        id("java") (1)
        id("com.google.protobuf").version("0.9.4") (2)
    }

1 | Apply a Core Plugin: This is very similar using either method.
2 | Apply a Third-Party Plugin: You specify the version using method chaining in the `plugins` block itself.

### References

- Using Plugins

### Tags

`#structuring-builds`

## Don’t Assume your Plugin is Applied after Another

Gradle’s plugin application is deterministic but opaque. It is difficult to reason about, especially across multiple build scripts, projects, convention plugins, or included builds.

As a result, you should not write build logic or plugins that depend on a specific plugin application order.

### Explanation

In a single `build.gradle(.kts)` file, plugins appear to be applied sequentially:

build.gradle.kts

    plugins {
        id("pluginA")
        id("pluginB")
    }

build.gradle

    plugins {
        id("pluginA")
        id("pluginB")
    }

However, in multi-project builds, with multiple `build.gradle(.kts)` files or a convention plugin, plugin application can be hard to determine:

app/build.gradle.kts

    plugins {
        id("pluginA")
        id("pluginB")
    }

lib/build.gradle.kts

    plugins {
        id("pluginC")
        id("pluginD")
    }

app/build.gradle

    plugins {
        id("pluginA")
        id("pluginB")
    }

lib/build.gradle

    plugins {
        id("pluginC")
        id("pluginD")
    }

You cannot assume whether `pluginA`, `pluginB`, `pluginC`, or `pluginD` will be applied first because `pluginA` could apply `pluginD`.

#### Build Engineers

Writing build logic that assumes plugin ordering can lead to brittle behavior and fragile builds that break when project structure changes.

Don’t rely on blocks like `allprojects {}`, `subprojects {}`, or `afterEvaluate {}` that are highly dependent on project structure and file layout. They can be difficult to decipher and may depend on configuration details that are hard to understand completely without running the build.

Avoid build logic that assumes ordering between different projects, included builds, or applied scripts. While the application order is deterministic, minor structural changes (such as adding a new project or renaming an include) can easily result in an unexpected change to that plugin application order.

#### Plugin Developers

Users should be able to apply your plugin in either order and have it behave correctly:

build.gradle.kts

    plugins {
      id("my-plugin")
      id("plugin-i-depend-on")
    }

build.gradle

    plugins {
      id("my-plugin")
      id("plugin-i-depend-on")
    }

or

build.gradle.kts

    plugins {
      id("plugin-i-depend-on")
      id("my-plugin")
    }

build.gradle

    plugins {
      id("plugin-i-depend-on")
      id("my-plugin")
    }

If your plugin only works in one of these cases, it’s relying on plugin order and will be fragile in real builds.

If your plugin cannot function without another plugin, apply it explicitly at the start of `Plugin.apply`:

MyPlugin.kt

    // Ensure required plugin is applied
    project.pluginManager.apply("com.example.required-plugin")

MyPlugin.groovy

    // Ensure required plugin is applied
    project.pluginManager.apply('com.example.required-plugin')

If your plugin only needs to integrate with another plugin when it’s present, react to its application using `pluginManager.withPlugin()` or `plugins.configureEach {}`:

MyPlugin.kt

    // Configure behavior that depends on required-plugin using the plugin id (preferred)
    project.pluginManager.withPlugin("com.example.required-plugin") {  }

    // Configure behavior that depends on RequiredPlugin using the plugin class (if no id is available)
    project.plugins.configureEach { plugin ->
        when (plugin) { is com.example.RequiredPlugin -> {  } }
    }

MyPlugin.groovy

    // Configure behavior that depends on required-plugin using the plugin id (preferred)
    project.pluginManager.withPlugin('com.example.required-plugin') {  }

    // Configure behavior that depends on RequiredPlugin using the plugin class (if no id is available)
    project.plugins.configureEach { plugin ->
        if (plugin instanceof com.example.RequiredPlugin) {  }
    }

This is order-independent and safe.

### Example

Given the following project layout:

    .
    ├── app/
    │   └── build.gradle.kts
    ├── buildSrc/
    │   ├── build.gradle.kts
    │   └── src/main/kotlin/MyPlugin.kt
    ├── settings.gradle.kts
    └── build.gradle.kts

    .
    ├── app/
    │   └── build.gradle
    ├── buildSrc/
    │   ├── build.gradle
    │   └── src/main/groovy/MyPlugin.groovy
    ├── settings.gradle
    └── build.gradle

#### Don’t Do This

This setup makes several assumptions about the `java` plugin.

In the root build, `subprojects {}` and `afterEvaluate {}` obscure when a plugin is applied and attempt to force ordering:

build.gradle.kts

    subprojects {
        // Apply the Java plugin to every subproject
        afterEvaluate {
            // This runs after the app subproject’s build script is evaluated and results in an error
            pluginManager.apply("java")
        }
    }

build.gradle

    subprojects {
        // Apply the Java plugin to every subproject
        afterEvaluate {
            // This runs after the app subproject’s build script is evaluated and results in an error
            apply plugin: 'java'
        }
    }

In the `app` subproject, the build file uses `extensions.getByType(…​)` which assumes `java` has already been applied:

app/build.gradle.kts

    plugins {
        id("myplugin")
    }
    // Assumes 'java' plugin is present
    extensions.getByType<org.gradle.api.plugins.JavaPluginExtension>().apply {
        toolchain.languageVersion.set(JavaLanguageVersion.of(21))
    }

app/build.gradle

    plugins {
        id 'myplugin'
    }
    // Assumes 'java' plugin is present
    project.extensions.getByType(org.gradle.api.plugins.JavaPluginExtension).with {
        toolchain.languageVersion.set(org.gradle.jvm.toolchain.JavaLanguageVersion.of(21))
    }

In the plugin implementation, `MyPlugin.kt` or `MyPlugin.groovy` also assumes `java` is already applied:

buildSrc/src/main/kotlin/MyPlugin.kt

    class MyPlugin : Plugin<Project> {
        override fun apply(project: Project) {
            // Assumes 'java' plugin is present
            // WARNING: This will fail if the 'java' plugin hasn't been applied yet.
            project.extensions.getByType(JavaPluginExtension::class.java).toolchain {
                languageVersion.set(JavaLanguageVersion.of(21))
            }
        }
    }

buildSrc/src/main/groovy/MyPlugin.groovy

    class MyPlugin implements Plugin<Project> {
        void apply(Project project) {
            // Assumes 'java' plugin is present
            // WARNING: This will fail if the 'java' plugin hasn't been applied yet.
            project.extensions.configure(JavaPluginExtension) {
                it.toolchain {
                    it.languageVersion.set(JavaLanguageVersion.of(21))
                }
            }
        }
    }

#### Do This Instead

The fixed version removes `afterEvaluate` and avoids assumptions about when or where the `java` plugin is applied.

The root build file has been deleted as there is no longer a need to use `subprojects {}`.

In the `app` subproject, the build file uses `plugins.withPlugin("java") {}` to safely configure tasks once `java` is applied:

app/build.gradle.kts

    pluginManager.withPlugin("java") {
        extensions.configure<org.gradle.api.plugins.JavaPluginExtension> {
            toolchain.languageVersion.set(JavaLanguageVersion.of(21))
        }
    }

app/build.gradle

    project.pluginManager.withPlugin('java') {
        project.extensions.configure(org.gradle.api.plugins.JavaPluginExtension) {
            it.toolchain {
                languageVersion.set(JavaLanguageVersion.of(21))
            }
        }
    }

In the plugin implementation, `MyPlugin.kt` or `MyPlugin.groovy` explicitly applies the `java` plugin:

buildSrc/src/main/kotlin/MyPlugin.kt

    class MyPlugin : Plugin<Project> {
        override fun apply(project: Project) {
            // If your plugin requires 'java', apply it so order doesn’t matter
            project.pluginManager.apply("java")
            // Now it's safe to configure Java things immediately
            project.extensions.configure(JavaPluginExtension::class.java) {
                toolchain.languageVersion.set(JavaLanguageVersion.of(21))
            }
        }
    }

buildSrc/src/main/groovy/MyPlugin.groovy

    class MyPlugin implements Plugin<Project> {
        void apply(Project project) {
            // If your plugin requires 'java', apply it so order doesn’t matter
            project.pluginManager.apply('java')
            // Now it's safe to configure Java things immediately
            project.extensions.configure(JavaPluginExtension) {
                it.toolchain {
                    it.languageVersion.set(JavaLanguageVersion.of(21))
                }
            }
        }
    }

### References

- Using Plugins

### Tags

`#structuring-builds`, `#plugins`

## Do Not Use Internal APIs

Do not use APIs from a package where any segment of the package is `internal`, or types that have `Internal` or `Impl` as a suffix in the name.

### Explanation

Using internal APIs is inherently risky and can cause significant problems during upgrades. Gradle and many plugins (such as Android Gradle Plugin and Kotlin Gradle Plugin) treat these internal APIs as subject to unannounced breaking changes during any new Gradle release, even during minor releases. There have been numerous cases where even highly experienced plugin developers have been bitten by their usage of such APIs leading to unexpected breakages for their users.

If you require specific functionality that is missing, it’s best to submit a feature request. As a temporary workaround consider copying the necessary code into your own codebase and extending a Gradle public type with your own custom implementation using the copied code.

### Example

#### Don’t Do This

build.gradle.kts

    import org.gradle.api.internal.attributes.AttributeContainerInternal

    configurations.create("bad") {
        attributes {
            attribute(Usage.USAGE_ATTRIBUTE, objects.named<Usage>(Usage.JAVA_RUNTIME))
            attribute(Category.CATEGORY_ATTRIBUTE, objects.named<Category>(Category.LIBRARY))
        }
        val badMap = (attributes as AttributeContainerInternal).asMap() (1)
        logger.warn("Bad map")
        badMap.forEach { (key, value) ->
            logger.warn("$key -> $value")
        }
    }

build.gradle

    import org.gradle.api.internal.attributes.AttributeContainerInternal

    configurations.create("bad") {
        attributes {
            attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_RUNTIME))
            attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.LIBRARY))
        }
        def badMap = (attributes as AttributeContainerInternal).asMap() (1)
        logger.warn("Bad map")
        badMap.each {
            logger.warn("${it.key} -> ${it.value}")
        }
    }

1 | Casting to `AttributeContainerInternal` and using `toMap()` should be avoided as it relies on an internal API.

#### Do This Instead

build.gradle.kts

    configurations.create("good") {
        attributes {
            attribute(Usage.USAGE_ATTRIBUTE, objects.named<Usage>(Usage.JAVA_RUNTIME))
            attribute(Category.CATEGORY_ATTRIBUTE, objects.named<Category>(Category.LIBRARY))
        }
        val goodMap = attributes.keySet().associate { (1)
            Attribute.of(it.name, it.type) to attributes.getAttribute(it)
        }
        logger.warn("Good map")
        goodMap.forEach { (key, value) ->
            logger.warn("$key -> $value")
        }
    }

build.gradle

    configurations.create("good") {
        attributes {
            attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_RUNTIME))
            attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.LIBRARY))
        }
        def goodMap = attributes.keySet().collectEntries {
            [Attribute.of(it.name, it.type), attributes.getAttribute(it as Attribute<Object>)]
        }
        logger.warn("Good map")
        goodMap.each {
            logger.warn("$it.key -> $it.value")
        }
    }

1 | Implementing your own version of `toMap()` that only uses public APIs is a lot more robust.

### References

- Gradle API

### Tags

`#upgrades`

## Set Build Flags in `gradle.properties`

Set Gradle build property flags in the `gradle.properties` file.

### Explanation

Instead of using command-line options or environment variables, set build flags in the root project’s `gradle.properties` file.

Gradle comes with a long list of Gradle properties, which have names that begin with `org.gradle` and can be used to configure the behavior of the build tool. These properties can have a major impact on build performance, so it’s important to understand how they work.

You should not rely on supplying these properties via the command-line for every Gradle invocation. Providing these properties via the command line is intended for short-term testing and debugging purposes, but it’s prone to being forgotten or inconsistently applied across environments. A permanent, idiomatic location to set and share these properties is in the `gradle.properties` file located in the root project directory. This file should be added to source control in order to share these properties across different machines and between developers.

You should understand the default values of the properties your build uses and avoid explicitly setting properties to those defaults. Any change to a property’s default value in Gradle will follow the standard deprecation cycle, and users will be properly notified.

| Properties set this way are not inherited across build boundaries when using composite builds.

### Example

#### Don’t Do This

    ├── build.gradle.kts
    └── settings.gradle.kts

    ├── build.gradle
    └── settings.gradle

build.gradle.kts

    tasks.register("first") {
        doLast {
            throw GradleException("First task failing as expected")
        }
    }

    tasks.register("second") {
        doLast {
            logger.lifecycle("Second task succeeding as expected")
        }
    }

    tasks.register("run") {
        dependsOn("first", "second")
    }

build.gradle

    tasks.register("first") {
        doLast {
            throw new GradleException("First task failing as expected")
        }
    }

    tasks.register("second") {
        doLast {
            logger.lifecycle("Second task succeeding as expected")
        }
    }

    tasks.register("run") {
        dependsOn("first", "second")
    }

This build is run with `gradle run -Dorg.gradle.continue=true`, so that the failure of the `first` task does not prevent the `second` task from executing.

This relies on person running the build to remember to set this property, which is error prone and not portable across different machines and environments.

#### Do This Instead

    ├── build.gradle.kts
    └── gradle.properties
    └── settings.gradle.kts

    ├── build.gradle
    └── gradle.properties
    └── settings.gradle

gradle.properties

    org.gradle.continue=true

This build sets the `org.gradle.continue` property in the `gradle.properties` file.

Now it can be executed using only `gradle run`, and the continue property will always be set automatically across all environments.

### References

- Gradle properties

### Tags

`#properties`

## Name Your Root Project

Always name your root project in the `settings.gradle(.kts)` file.

### Explanation

While an empty `settings.gradle(.kts)` file is enough to create a multi-project build, you should always set the `rootProject.name` property.

By default, the root project’s name is taken from the directory containing the build. This can be problematic if the directory name contains spaces, Gradle logical path separators, or other special characters. It also makes task paths dependent on the directory name, rather than being reliably defined.

Explicitly setting the root project’s name ensures consistency across environments. Project names appear in error messages, logs, and reports, and builds often run on different machines, such as CI servers. Builds may execute on a variety of machines or environments, such as CI servers, and should report the same root project name anywhere to make the project more comprehensible.

### Example

#### Don’t Do This

settings.gradle.kts

    // Left empty

settings.gradle

    // Left empty

In this build, the settings file is empty and the root project has no explicit name. Running the `projects` report shows that Gradle assigns an implicit name to the root project, derived from the build’s current directory.

Unfortunately that name varies based on where the project currently lives. For example, if the project is checked out into a directory named `some-directory-name`, the output of `./gradlew projects` will look like this:

    > Task :projects

    Projects:

    ------------------------------------------------------------
    Root project 'some-directory-name'
    ------------------------------------------------------------

#### Do This Instead

settings.gradle.kts

    rootProject.name = "my-example-project"

settings.gradle

    rootProject.name = "my-example-project"

In this build, the root project is explicitly named. The explicit name `my-example-project` will be used in all reports, logs, and error messages. Regardless of where the project lives, the output of `./gradlew projects` will look like this:

nameYourRootProject-do.out

    > Task :projects

    Projects:

    ------------------------------------------------------------
    Root project 'my-example-project'
    ------------------------------------------------------------

    Project hierarchy:

    Root project 'my-example-project'
    No sub-projects

    To see a list of the tasks of a project, run gradle <project-path>:tasks
    For example, try running gradle :tasks

    BUILD SUCCESSFUL in 0s
    1 actionable task: 1 executed

### References

- Naming recommendations

### Tags

`#settings`

## Do not use `gradle.properties` in subprojects

Do not place a `gradle.properties` file inside subprojects to configure your build.

### Explanation

Gradle allows `gradle.properties` files in both the root project and subprojects, but support for subproject properties is inconsistent. Gradle itself and many popular plugins (such as the Android Gradle Plugin and Kotlin Gradle Plugin) do not reliably handle this pattern.

Using subproject `gradle.properties` files also makes it harder to understand and debug your build. Property values may be scattered across multiple locations, overridden in unexpected ways, or difficult to trace back to their source.

If you need to set properties for a single subproject, define them directly in that subproject’s `build.gradle(.kts)`. If you need to apply properties across multiple subprojects, extract the configuration into a convention plugin.

### Example

#### Don’t Do This

    ├── app
    │   ├── ⋮
    │   ├── build.gradle.kts
    │   └── gradle.properties
    ├── utilities
    │   ├── ⋮
    │   ├── build.gradle.kts
    │   └── gradle.properties
    └── settings.gradle.kts

    ├── app
    │   ├── ⋮
    │   ├── build.gradle
    │   └── gradle.properties
    ├── utilities
    │   ├── ⋮
    │   ├── build.gradle
    │   └── gradle.properties
    └── settings.gradle

gradle.properties

    # This file is located in /app
    propertyA=fixedValue
    propertyB=someValue

build.gradle.kts

    // This file is located in /app
    tasks.register("printProperties") { (1)
        val propA = project.findProperty("propertyA") (2)
        val propB = project.findProperty("propertyB")

        doLast {
            println("propertyA in app: $propA")
            println("propertyB in app: $propB")
        }
    }

build.gradle

    // This file is located in /app
    tasks.register("printProperties") { (1)
        def propA = project.findProperty("propertyA") (2)
        def propB = project.findProperty("propertyB")

        doLast {
            println "propertyA in app: $propA"
            println "propertyB in app: $propB"
        }
    }

gradle.properties

    # This file is located in /util
    propertyA=fixedValue
    propertyB=otherValue

build.gradle.kts

    // This file is located in /util
    tasks.register("printProperties") {
        val propA = project.findProperty("propertyA")
        val propB = project.findProperty("propertyB") (3)

        doLast {
            println("propertyA in util: $propA")
            println("propertyB in util: $propB")
        }
    }

build.gradle

    // This file is located in /util
    tasks.register("printProperties") {
        def propA = project.findProperty("propertyA")
        def propB = project.findProperty("propertyB") (3)

        doLast {
            println "propertyA in util: $propA"
            println "propertyB in util: $propB"
        }
    }

1 | Register a task that uses the value of properties in each subproject.
2 | The task reads properties, which are supplied by the project-local `app/gradle.properties` file. `propertyA` does not vary between subprojects.
3 | 'util’s print task reads the properties which are supplied by `util/gradle.properties`. `propertyB` varies between subprojects.

This structure requires duplicating properties that are shared between subprojects and is not guaranteed to remain supported.

#### Do This Instead

    ├── buildSrc
    │   └──  ⋮
    ├── app
    │   ├── ⋮
    │   └── build.gradle.kts
    ├── utilities
    │   ├── ⋮
    │   └── build.gradle.kts
    ├── settings.gradle.kts
    └── gradle.properties

    ├── buildSrc
    │   └──  ⋮
    ├── app
    │   ├── ⋮
    │   └──  build.gradle
    ├── utilities
    │   ├── ⋮
    │   └── build.gradle
    ├── settings.gradle
    └── gradle.properties

gradle.properties

    # This file is located in the root of the build
    propertyA=fixedValue
    propertyB=someValue

ProjectProperties.kt

    import org.gradle.api.provider.Property

    interface ProjectProperties { (1)
        val propertyA: Property<String>
        val propertyB: Property<String>
    }

ProjectProperties.groovy

    import org.gradle.api.provider.Property

    interface ProjectProperties { (1)
        Property<String> getPropertyA()
        Property<String> getPropertyB()
    }

project-properties.gradle.kts

    extensions.create<ProjectProperties>("myProperties") (2)

    tasks.register("printProperties") { (3)
        val myProperties = project.extensions.getByName("myProperties") as ProjectProperties
        val projectName = project.name

        doLast {
            println("propertyA in ${projectName}: ${myProperties.propertyA.get()}")
            println("propertyB in ${projectName}: ${myProperties.propertyB.get()}")
        }
    }

project-properties.gradle

    extensions.create("myProperties", ProjectProperties) (2)

    tasks.register("printProperties") { (3)
        def myProperties = project.extensions.getByName("myProperties") as ProjectProperties
        def projectName = project.name

        doLast {
            println("propertyA in ${projectName}: ${myProperties.propertyA.get()}")
            println("propertyB in ${projectName}: ${myProperties.propertyB.get()}")
        }
    }

build.gradle.kts

    // This file is located in /app
    plugins { (4)
        id("project-properties")
    }

    myProperties { (5)
        propertyA = providers.gradleProperty("propertyA")
        propertyB = providers.gradleProperty("propertyB")
    }

build.gradle

    // This file is located in /app
    plugins { (4)
        id "project-properties"
    }

    myProperties { (5)
        propertyA = providers.gradleProperty("propertyA")
        propertyB = providers.gradleProperty("propertyB")
    }

build.gradle.kts

    // This file is located in /util
    plugins {
        id("project-properties")
    }

    myProperties {
        propertyA = providers.gradleProperty("propertyA")
        propertyB = "otherValue" (6)
    }

build.gradle

    // This file is located in /util
    plugins {
        id "project-properties"
    }

    myProperties {
        propertyA = providers.gradleProperty("propertyA")
        propertyB = "otherValue" (6)
    }

1 | Define a simple extension type in `buildSrc` to hold property values.
2 | Register that property in a convention plugin.
3 | Register tasks using property values in the convention plugin.
4 | Apply the convention plugin in each subproject.
5 | Set the extension’s property values in each subproject’s build script. This uses the values defined in the root `gradle.properties` file. The task reads values from the extension, not directly from the project properties.
6 | When values need to vary between subprojects, they can be set directly on the extension.

This structure uses an extension type to hold values, allowing properties to be strongly typed, and for property values and operations on properties to be defined in a single location. Overriding values per subproject remains straightforward.

### References

- Gradle properties

### Tags

`#properties`

## Avoid `afterEvaluate`

Do not use `project.afterEvaluate {}` to configure tasks, wire properties, or react to plugin application. Use lazy properties and `pluginManager.withPlugin()` instead.

### Explanation

`afterEvaluate` registers a callback that runs after Gradle finishes evaluating and configuring a project. It was historically used to "delay" reading a value until configuration was complete — for example, reading an extension property that users set at the bottom of their build script, or checking whether another plugin was applied.

This pattern is outdated, and problematic for several reasons:

- Ordering is fragile. Multiple `afterEvaluate` callbacks execute in registration order. If two plugins or scripts both use `afterEvaluate`, one may see stale or incomplete configuration depending on which was registered first. This creates subtle bugs that are extremely difficult to diagnose.

- It defeats task configuration avoidance. Tasks registered or configured inside `afterEvaluate` are touched eagerly during configuration, even if they will never execute. This may cause unnecessary work and slow down the configuration phase of the build.

- It is incompatible with the Configuration Cache. `afterEvaluate` callbacks capture mutable project state that cannot be serialized reliably.

Gradle’s lazy `Property` and `Provider` types solve the same underlying problem — deferring value resolution — without any of these drawbacks. A `Property<T>` can be wired at configuration time but its value is resolved only when needed, typically during task execution. This makes configuration order-independent and fully compatible with the configuration cache.

Similarly, `pluginManager.withPlugin()` reacts to plugin application safely and immediately, regardless of when the plugin is actually applied — no callback ordering to worry about.

#### When `afterEvaluate` may still be appropriate

There are narrow use cases where `afterEvaluate` remains the only available hook:

- Fail-fast validation — verifying that required project configuration has been set and failing the build early with a clear error message.

- Logging or reporting — printing diagnostic information about the project’s final configuration state.

Even in these cases, exercise caution: your `afterEvaluate` must be the last (or only) one registered to see the final configuration state. If another plugin registers an `afterEvaluate` after yours, your callback may see incomplete configuration.

If you find yourself reaching for `afterEvaluate` because Gradle’s lazy APIs do not cover your use case, consider filing a bug. `afterEvaluate` should be a last resort, not a first choice.

### Example

Given the following project layout:

    .
    ├── build.gradle.kts
    ├── buildSrc/
    │   ├── build.gradle.kts
    │   └── src/main/kotlin/
    │       └── AppInfoPlugin.kt
    └── settings.gradle.kts

    .
    ├── build.gradle
    ├── buildSrc/
    │   ├── build.gradle
    │   └── src/main/groovy/
    │       └── AppInfoPlugin.groovy
    └── settings.gradle

#### Don’t Do This

The plugin uses `afterEvaluate` to delay reading the extension value and to check whether `java-library` was applied:

buildSrc/src/main/kotlin/AppInfoPlugin.kt

    interface AppInfoExtension {
        val appName: Property<String>
    }

    class AppInfoPlugin : Plugin<Project> {
        override fun apply(project: Project) {
            val extension = project.extensions.create("appInfo", AppInfoExtension::class.java)

            project.afterEvaluate { (1)
                val name = extension.appName.getOrElse("unnamed") (2)

                tasks.register("printAppInfo") { (3)
                    doLast {
                        println("App: $name")
                    }
                }

                if (plugins.hasPlugin("java-library")) { (4)
                    tasks.named("printAppInfo") {
                        doLast {
                            println("Jar: $name.jar")
                        }
                    }
                    tasks.named("jar", Jar::class.java) {
                        archiveBaseName.set(name)
                    }
                }
            }
        }
    }

buildSrc/src/main/groovy/AppInfoPlugin.groovy

    interface AppInfoExtension {
        Property<String> getAppName()
    }

    class AppInfoPlugin implements Plugin<Project> {
        void apply(Project project) {
            def extension = project.extensions.create("appInfo", AppInfoExtension)

            project.afterEvaluate { (1)
                def name = extension.appName.getOrElse("unnamed") (2)

                project.tasks.register("printAppInfo") { (3)
                    doLast {
                        println "App: $name"
                    }
                }

                if (project.plugins.hasPlugin("java-library")) { (4)
                    project.tasks.named("printAppInfo") {
                        doLast {
                            println "Jar: ${name}.jar"
                        }
                    }
                    project.tasks.named("jar", Jar) {
                        archiveBaseName.set(name)
                    }
                }
            }
        }
    }

1 | The plugin’s `afterEvaluate` runs before any `afterEvaluate` registered later in the build script — ordering depends on registration order.
2 | `getOrElse` reads the property’s current value immediately. If the value is set in a later `afterEvaluate`, this will never see it.
3 | Registering a task inside `afterEvaluate` defeats task configuration avoidance.
4 | Checking plugin presence inside `afterEvaluate` assumes all plugins have been applied before this callback runs.

The build script applies the plugin and sets the extension value in its own `afterEvaluate`:

build.gradle.kts

    plugins {
        id("java-library")
        id("app-info-plugin")
    }

    afterEvaluate {
        the<AppInfoExtension>().appName.set("my-app") (1)
    }

build.gradle

    plugins {
        id 'java-library'
        id 'app-info-plugin'
    }

    afterEvaluate {
        appInfo { (1)
            appName.set('my-app')
        }
    }

1 | This `afterEvaluate` runs after the plugin’s — by the time it sets the name, the plugin has already captured the default value.

Running `printAppInfo` outputs `unnamed` — not `my-app` as the user intended:

    > Task :printAppInfo
    App: unnamed
    Jar: unnamed.jar

    BUILD SUCCESSFUL in 0s
    5 actionable tasks: 5 executed

The plugin’s `afterEvaluate` callback was registered first (during `Plugin.apply()`) and ran first, reading the property before the build script’s `afterEvaluate` had a chance to set it.

#### Do This Instead

The proper way to write this plugin uses lazy `Property` types and `pluginManager.withPlugin()`:

buildSrc/src/main/kotlin/AppInfoPlugin.kt

    interface AppInfoExtension {
        val appName: Property<String>
    }

    class AppInfoPlugin : Plugin<Project> {
        override fun apply(project: Project) {
            val extension = project.extensions.create("appInfo", AppInfoExtension::class.java)
            extension.appName.convention("unnamed") (1)

            project.tasks.register("printAppInfo") {
                val name = extension.appName
                doLast {
                    println("App: ${name.get()}") (2)
                }
            }

            project.pluginManager.withPlugin("java-library") { (3)
                project.tasks.named("printAppInfo") {
                    val jarName = extension.appName
                    doLast {
                        println("Jar: ${jarName.get()}.jar")
                    }
                }
                project.tasks.named("jar", Jar::class.java) {
                    archiveBaseName.set(extension.appName)
                }
            }
        }
    }

buildSrc/src/main/groovy/AppInfoPlugin.groovy

    interface AppInfoExtension {
        Property<String> getAppName()
    }

    class AppInfoPlugin implements Plugin<Project> {
        void apply(Project project) {
            def extension = project.extensions.create("appInfo", AppInfoExtension)
            extension.appName.convention("unnamed") (1)

            project.tasks.register("printAppInfo") {
                def name = extension.appName
                doLast {
                    println "App: ${name.get()}" (2)
                }
            }

            project.pluginManager.withPlugin("java-library") { (3)
                project.tasks.named("printAppInfo") {
                    def jarName = extension.appName
                    doLast {
                        println "Jar: ${jarName.get()}.jar"
                    }
                }
                project.tasks.named("jar", Jar) {
                    archiveBaseName.set(extension.appName)
                }
            }
        }
    }

1 | `convention()` provides a default value that is used only if no explicit value is set via `set()`.
2 | The value is resolved at execution time via `get()` — configuration order does not matter.
3 | `pluginManager.withPlugin()` fires when the plugin is applied, regardless of order. If the plugin is never applied, the callback is never invoked.

The build script is nearly identical — the change is in the plugin, not the consumer:

build.gradle.kts

    plugins {
        id("java-library")
        id("app-info-plugin")
    }

    appInfo {
        appName.set("my-app") (1)
    }

build.gradle

    plugins {
        id 'java-library'
        id 'app-info-plugin'
    }

    appInfo {
        appName.set('my-app') (1)
    }

1 | The value is set during normal configuration. Because the plugin wires the `Property` lazily, it is only read at execution time.

Running `printAppInfo` now correctly outputs `my-app`:

    $ gradlew printAppInfo
    > Task :printAppInfo
    App: my-app
    Jar: my-app.jar

    BUILD SUCCESSFUL in 0s
    5 actionable tasks: 5 executed

### References

- Properties and Providers

- Lazy Configuration

- Task Configuration Avoidance

- Don’t Assume your Plugin is Applied after Another

### Tags

`#task-configuration-avoidance`, `#plugins`, `#structuring-builds`