# General Gradle Best Practices

version 9.1.0-20250821012450+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`

## 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`