1. Introduction

When working on a Kotlin project with Gradle, there are scenarios where we need to exclude certain libraries or transitive dependencies to avoid conflicts or reduce the size of our final build. Gradle provides mechanisms to exclude libraries globally across all dependencies.

In this tutorial, we’ll explore how to exclude a library or a transitive dependency using the Kotlin DSL syntax in a Gradle build script.

2. Understanding the Problem

Dependencies in a Gradle project often include other libraries as transitive dependencies. However, there are several reasons why we might need to exclude specific libraries:

  • Version Conflicts: Different dependencies may include varying versions of the same library, causing compatibility issues or runtime errors. For example, if two libraries bring in different versions of commons-logging, we might face runtime inconsistencies.
  • Optimizing Build Size: Unused libraries can add unnecessary weight to the final build. Excluding these unwanted transitive dependencies helps reduce the overall size of the project output.
  • Replacing Default Libraries: Sometimes, we need to replace a default dependency with an alternative. For instance, certain Spring Boot starters include commons-logging by default, but we might prefer to use slf4j instead. By excluding the unwanted library, we can include the replacement explicitly.

By understanding these common scenarios, we can effectively manage the dependency graph and ensure only the required libraries are included in the build.

3. Excluding a Library in Kotlin DSL

Gradle provides flexible ways to exclude unwanted libraries or transitive dependencies in a Kotlin DSL build script. We can achieve this either globally, across all dependencies, or at a more granular level for specific dependencies. The exclusion mechanisms leverage the configurations and dependencies blocks, allowing us to control the dependency graph precisely.

In the following sections, we’ll explore how to exclude libraries globally or for individual dependencies using the Kotlin DSL syntax.

3.1. Exclude a Library Globally

To exclude a library or transitive dependency globally across all dependencies, we use the configurations block in our build.gradle.kts file:

configurations.all {
    exclude(group = "org.apache.commons", module = "commons-logging")
}

In this example, the exclude function specifies the group and module of the dependency we want to remove. The group refers to the group ID, and the module refers to the artifact ID of the library.

3.2. Excluding a Library from Specific Dependencies

If we only want to exclude a library from a particular dependency instead of applying it globally, we use the dependencies block with an inline exclude function:

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web") {
        exclude(group = "org.apache.commons", module = "commons-logging")
    }
}

In this example, the spring-boot-starter-web dependency includes commons-logging as a transitive dependency. By adding the exclude block, we remove commons-logging specifically for this dependency while leaving other dependencies unaffected.

3.3. Excluding Multiple Libraries

To exclude multiple libraries, we can add multiple exclude statements inside the configurations block:

configurations.all {
    exclude(group = "org.apache.commons", module = "commons-logging")
    exclude(group = "ch.qos.logback", module = "logback-classic")
}

Alternatively, for better readability and maintainability, we can define a list of exclusions and loop over them:

val exclusions = listOf(
    "org.apache.commons" to "commons-logging",
    "ch.qos.logback" to "logback-classic"
)

configurations.all {
    exclusions.forEach { (group, module) ->
        exclude(group = group, module = module)
    }
}

This approach is particularly useful when dealing with a large number of exclusions, as it keeps the code clean and easier to maintain.

4. Verifying Exclusions

To verify that exclusions are applied, we can use Gradle’s dependency insight task:

./gradlew dependencies --configuration implementation

This will display the resolved dependency graph, helping us confirm that the unwanted libraries are excluded.

5. Conclusion

In this article, we explored how to exclude a library or transitive dependency in Gradle using Kotlin DSL. Whether globally or for a specific dependency, Gradle provides flexible options to manage exclusions effectively.

By carefully applying exclusions, we can resolve dependency conflicts, optimize our build, and ensure compatibility with desired libraries.


原始标题:Excluding a Library from All Dependencies in Kotlin DSL in Gradle