What Is New in Gradle 4
Gradle 4 delivers a mix of performance boosts, new features for Java and Android builds, and foundational improvements to the dependency resolution engine. This release focuses on faster builds and a more expressive build script DSL.
| Category | Key Changes |
|---|---|
| Performance & Core | Compile avoidance for Java, new Gradle Daemon lifecycle, improved incremental compilation. |
| New Features | Java Library plugin, source set dependencies, public API for custom tasks, lazy configuration. |
| Dependency Management | Component metadata rules API, compileOnly API dependencies, BOM import support. |
| Improvements & Updates | Updated wrapper with SHA-256 checksum, Kotlin DSL 1.0, improved error messages. |
| Deprecated & Breaking Changes | Legacy software model deprecated, changes to Play and OSGi plugins, task output caching stable. |
How does Gradle 4 speed up Java builds?
It introduces compile avoidance for Java projects. If a dependent library changes only its implementation (not its public API), Gradle skips recompiling modules that depend on it. This works with annotation processors too.
In practice, this means fewer unnecessary compilations in multi-module builds. You also get a redesigned Daemon that uses less memory and a more robust incremental compiler for Java.
What's the deal with the new Java Library plugin?
The new java-library plugin is a key addition. It extends the standard Java plugin by introducing an api configuration. Dependencies declared in api are exposed to consumers, while implementation dependencies are kept internal.
This matters because it enforces better modularity. It makes the boundaries between your project's modules explicit, which is crucial for large-scale builds and enables the new compile avoidance feature.
What improvements were made to dependency resolution?
Gradle 4 provides a public API for component metadata rules. This lets you manipulate dependency details directly in your build script, like forcing a specific version or changing a dependency's scope.
You can now also import Maven BOM (Bill of Materials) files to align dependency versions. The compileOnly configuration is now available for all JVM projects, not just Android, which is great for dependencies needed only at compile time.
Are there new ways to write build logic?
Yes, the Kotlin DSL reaches a 1.0 milestone, offering a statically-typed alternative to Groovy with better IDE support. For custom task development, there's a new public API that makes tasks easier to write and maintain.
The concept of "lazy configuration" is introduced, allowing property values to be configured lazily. This can help avoid expensive work during build configuration phase, moving it to execution time instead.
What got deprecated or changed in a breaking way?
The legacy software model (the components block) is now deprecated. The Play and OSGi plugins have breaking changes; the Play plugin requires explicit configuration, and OSGi now uses Bnd by default.
Task output caching is now stable and enabled by default. This is a big deal for CI environments, as cached task outputs can be reused across different machines, cutting down build times significantly.
FAQ
Should I switch from the 'java' plugin to 'java-library'?
Yes, for any project that is consumed as a library by other Gradle projects. Using the api and implementation configurations properly is essential for build performance and clean boundaries.
Does compile avoidance work with annotation processors?
Yes, it does. Gradle 4's compile avoidance is aware of annotation processors, so if an API doesn't change, recompilation is skipped even when processors are involved.
What happens to the old Gradle Daemon?
The old Daemon is replaced. The new one is more efficient and stops automatically after 3 hours of idle time, or you can stop it manually with gradle --stop.
Is the Kotlin DSL ready for production?
Gradle 4 marks the Kotlin DSL 1.0 release, making it stable and supported for production use. It offers excellent IDE assistance in IntelliJ IDEA.
How do I import a Maven BOM now?
Use the platform or enforcedPlatform keywords in your dependencies block. For example: implementation platform('org.springframework.boot:spring-boot-dependencies:1.5.4.RELEASE').