Gradle

Gradle is an open-source build automation tool. A build process typically picks correct versions of project source code, resolves dependencies, assembles necessary assets, checks code against coding guidelines, executes unit tests, compiles code, and publishes the resulting software artifacts. Gradle can do all of these.
Gradle is performant, flexible and extensible. Via both convention and configuration, it balances ease of use and flexibility. Through plugins, the basic feature set of Gradle can be extended. It runs on the JVM and hence can be used on any platform. Gradle can be used for projects written in Java, C++, Swift, and more. Many IDEs and CI/CD tools support Gradle.
Since it's release in 2008, adoption of Gradle has grown steadily. In 2020, Maven continued to be the more popular build tool for traditional Java applications. However, Gradle is more widely used by Android mobile app developers.
Discussion
-
What are the main features of Gradle? Gradle has dozens of features. These relate to authoring of build scripts, Gradle execution, and integrations to IDEs, languages and repositories.
Writing build scripts is made easy with Groovy or Kotlin Domain-Specific Language (DSL). Well-defined conventions make it easier for developers to write build scripts. However, Gradle is flexible enough to allow easy customization.
Gradle downloads and manages transitive dependencies. Even files can be declared as dependencies. Remote dependencies can be cached locally. By locking dynamic dependencies to specific versions, builds become deterministic and reproducible.
For performance, Gradle does incremental builds, skipping tasks and subtasks when their inputs haven't changed. It caches previous builds. It executes tasks in parallel. Build scans enable developers to analyse and compare past builds to find problems or optimize builds. Execution can be controlled via options. For example, some tasks can be excluded. Build can be configured to continue despite failures.
Gradle has specific features and support for Java, Groovy, Scala, Android, C/C++, Assembler, GCC, Clang, CUnit, etc. The Gradle Tooling API allows Gradle SDK to be used within IDEs and CI/CD tools.
-
Which are the main building blocks of Gradle? A task contains logic to execute something specific, such as compiling, testing or deploying software. Gradle comes with many in-built tasks and developers can create custom tasks. Tasks are composed of inputs, actions and outputs.
A build is the execution of a set of tasks. A project is what a build achieves. For example, assembling a jar, war or zip file can be a project. Projects can have sub-projects. A build can contain more than one project. A project's build script is named
build.gradle
orbuild.gradle.kts
. Build happens in three phases: initialization (set up the build environment for relevant projects), configuration (which tasks to run) and execution (run tasks). Execution can be started from command line or from an IDE.Gradle's design is minimalistic. It's extended by plugins. A plugin helps us reuse logic or configuration across projects. It helps us write more maintainable build scripts.
-
How does Gradle compare against Apache Maven? It's claimed that Gradle is 2x faster than Maven for most cases and 100x faster for large builds. This is mainly due to incremental builds, better build caching and the Gradle daemon. Gradle daemon runs in the background, thus eliminating JVM startup time. It caches information across builds. It watches the file system to determine what needs to be built even before a build is started. Gradle's Build Scan helps developers visualize and optimize builds.
Maven relies on XML for build configuration. Gradle scripts are in DSL. They're configuration but they're also code. They can be easier to maintain than XML.
Gradle is more flexible. It's Tooling API allows us to embed it into IDEs and other tools. Customization with Maven can be hard. While Maven has better support with IDEs, Gradle is getting better here, particularly with Kotlin DSL.
While Maven can override a dependency by version, Gradle has dependency selection and substitution rules. These can be declared once and used project-wide. Substitution enables composite builds across projects. In fact, it's been said that Maven is not suited for multi-project builds.
-
What's the right way to organize a Gradle project? Start by running
gradle wrapper
. Then launch Gradle withgradlew
(Linux) orgradlew.bat
(Windows). The wrapper automatically installs Gradle and eases future upgrades.Follow common conventions in organizing files and folders. For example, keep Java and Kotlin files in separate folders. Keep unit tests separate from integration tests.
Gradle always looks for the
settings.gradle
file. Create at least an empty file to speed up the search for this file. Properties can be specified ingradle.properties
. Properties specified in build scripts can be hard to maintain. Properties passed via the command line are useful for ad hoc scenarios.Each task should write its output to a separate folder. This allows Gradle to apply incremental build for faster builds. Due to incremental build, there's no need to clean before a build.
When the build script file
build.gradle
becomes large and unmaintainable, it's a good idea to modularize it into multiple files and organize them within abuildSrc/
folder. Thus declarations are separated from implementations. Additional dependencies can be inbuildSrc/build.gradle
. -
What are some best practices when using Gradle? Write build scripts in a declarative manner. DSL enables this. Avoid writing imperative code in build scripts. Complex imperative code can be moved to a binary plugin.
When a project has many dependencies, the ordering of repositories might matter. The more often used repositories should be listed first.
Minimize logic during the configuration phase of a build. These would be executed every time even when the associated task is not executed.
Use multi-project or composite builds rather than triggering another build using the
GradleBuild
task type. Avoid inter-project configuration, such as depending on a task from another project.On multicore systems, run tests in parallel with
test { maxParallelForks 3 }
.Don't include passwords in build scripts or
gradle.properties
file.Avoid using internal Gradle APIs. Their use can break build scripts if Gradle or plugins change. For example, don't use
org.gradle.internal.os.OperatingSystem
. Instead, callSystem.getProperty("os.name")
from Apache Commons SystemUtils.Use the Gradle lint plugin to enforce a coding style to build scripts. Follow conventions when declaring tasks. An example is to always declare
group
anddescription
so that tasks become discoverable.
Milestones
2014
Gradle achieves one million downloads per month. A 2016 study of 10,000 projects shows that Maven is the most popular build tool (53%) while Gradle is used by only 11% of the projects. However, Gradle adoption continues to grow, reaching 10M downloads per month (2019) and 30M downloads per month (2022).
2016

Gradle v3.0 is released. This release enables Gradle Daemon by default. There's better IDE support for IDEA and Eclipse. While Groovy remains the primary build language, now there's support for Kotlin as well. Java 9 is supported. Gradle Cloud Services is part of the release and Build Scans is the first such service. A build scan provides insights into a build and identifies problems. Gradle Enterprise (first released in June 2016) is an on-premise version of Gradle Cloud Services.
2017
2018
2019
2021
Gradle v7.0 is released. With file system watching enabled by default, incremental builds are faster. Builds are made more secure by running integrity checks on the dependencies. There's support for Java 16. In multi-project builds, dependency versions can be shared using an experimental feature called version catalogs.
Sample Code
References
- Android Developers. 2022. "Android Gradle plugin release notes." Android Studio, Android Developers, November 22. Accessed 2022-11-27.
- Baeldung. 2017. "Introduction to Gradle." Baeldung, November 23. Updated 2022-07-13. Accessed 2022-11-27.
- Gradle. 2014. "Gradle Release Notes." Documentation, Gradle v2.0. Accessed 2022-11-27.
- Gradle. 2016. "Gradle Release Notes." Documentation, Gradle v3.0. Accessed 2022-11-27.
- Gradle. 2017. "Gradle Release Notes." Documentation, Gradle v4.0. Accessed 2022-11-27.
- Gradle. 2018a. "Gradle Release Notes." Documentation, Gradle v5.0. Accessed 2022-11-27.
- Gradle. 2018b. "What's new in Gradle 5.0." Gradle. Accessed 2022-11-27.
- Gradle. 2019. "Gradle Release Notes." Documentation, Gradle v6.0. Accessed 2022-11-27.
- Gradle. 2021a. "Gradle Release Notes." Documentation, Gradle v7.0. Accessed 2022-11-27.
- Gradle. 2021b. "What's new in Gradle 7.0." Gradle. Accessed 2022-11-27.
- Gradle. 2022a. "The Gradle Daemon." Gradle. Accessed 2022-11-27.
- Gradle. 2022b. "Gradle Enterprise releases." Gradle. Accessed 2022-11-27.
- Gradle. 2022c. "Gradle vs Maven Comparison." Gradle. Accessed 2022-11-27.
- Gradle. 2022d. "Gradle Build Tool Features." Gradle. Accessed 2022-11-27.
- Gradle. 2022e. "Our Story." Gradle. Accessed 2022-11-27.
- Gradle. 2022f. "Releases." Gradle. Accessed 2022-11-27.
- Gradle. 2022g. "What is Gradle?" Documentation, Gradle v7.6. Accessed 2022-11-27.
- Gradle. 2022h. "Best practices for authoring maintainable builds." Documentation, Gradle v7.6. Accessed 2022-11-27.
- Gradle. 2022i. "Organizing Gradle Projects." Documentation, Gradle v7.6. Accessed 2022-11-27.
- Gregory, T. 2021. "10 Gradle best practices to supercharge your project." July 7. Accessed 2022-11-27.
- Muschko, B. 2014. "Gradle in Action." Manning Publications. Accessed 2022-11-27.
- NovaOrdis. 2020. "File:Gradle Configuration Scripts.png." NovaOrdis Knowledge Base, October 17. Accessed 2022-11-28.
- Redlich, M. 2016. "Introducing Gradle Build Scans." InfoQ, December 23. Accessed 2022-11-27.
- Sulír, M. and J. Porubän. 2016. "A Quantitative Study of Java Software Buildability." Proceedings of the 7th International Workshop on Evaluation and Usability of Programming Languages and Tools (PLATEAU 2016), ACM, pp. 17-25. doi: 10.1145/3001878.3001882. Accessed 2022-11-27.
- Tozzi, C. 2018. "4 reasons why Gradle may be the right Java build tool." TheServerSide, TechTarget, October 22. Accessed 2022-11-27.
- Vaggalis, N. 2020. "Where's Java Going In 2020." I Programmer, 24 February. Accessed 2022-11-27.
- Wikipedia. 2022. "Software build." Wikipedia, June 16. Accessed 2022-11-27.
- van de Kamp, Lars, Ingmar Wever, Julian Hols, and Hugo Bijmans. 2017. "Gradle: adaptable, fast automation for all." In: Delft Students on Software Architecture: DESOSA 2017, Version 1.0, Delft University of Technology, April 17. Accessed 2022-11-27.
Further Reading
Article Stats
Cite As
See Also
- Build Automation
- Domain-Specific Language
- DevOps
- Dependency Manager
- Gradle Enterprise
- Android Gradle Plugin