Gradle basic introduction

The following is my translation and understanding based on official documents. If you are able, you can check Gradle’s official website directly.


Gradle is an open source build automation tool that focuses on flexibility and performance.

Gradle build scripts can be written in Groovy or Kotlin DSL. Since I write Android and usually develop Kotlin, I prefer to write Gradle in Kotlin.

Gradle has the following features:

  1. Strong ability to customize
  2. Fast builds (Gradle completes tasks quickly by reusing previously executed outputs, processing only changed inputs, and executing tasks in parallel)
  3. Powerful: Android official recommended build tools, and support multiple languages
  4. Based on the JVM, you must install the JDK before using Gradle

Gradle’s core features

Gradle is a universal build tool

Gradle allows us to build any project. There are no specific restrictions on what can be built and how it should be done. The only other limitation is that adding dependency management currently only supports Maven and Ivy-compatible repositories and file systems.

This does not mean that we need to do a lot of work to create a Gradle. Gradle adds pre-built functionality by adding conventions and generic plug-ins, making it easy to build general-type projects such as Java Library and Android Lugin. We can also create and publish custom plug-ins to encapsulate our conventions and build functionality. As an Android project, the com.android.application and com.andorid.library plug-ins already do a lot of common things for us that we can run after creating a project in Android Studio.

2. Gradle’s core build model

Gradle’s core build model is based onTAG (Directed acyclic graph)Essentially, a Gradle build creates a TAG based on each task and their dependencies, and executes the TAG in order. Here are two examples:Almost all Gradle builds can be abstracted into tags like those shown above, which is why Gradle is so flexible.

A Task consists of three things:

  1. Action
  2. Input
  3. Output

Of course, all three are optional for a task, depending on what the task needs to do. For example, Standard Lifecycle Tasks have no actions, their purpose is to aggregate multiple tasks.

You can specify which task to execute. For example, Gradle test will execute the test task.

The last thing to note is that Gradle has a robust incremental build system to speed up our builds, so avoid doing clean tasks unless you really need to.

3. Gradle Builds have three fixed life cycles
  1. Initialize the build execution environment and determine which projects will participate in the build
  2. The configuration phase generates tags based on the execution order and dependencies between tasks.
  3. The execution phase executes the TAG
Gradle supports extension in many ways

Ideally, if we can only use Gradle bundled build logic to build projects. But it’s almost impossible. Most builds have special requirements that mean we need to add custom build logic.

  • User-defined task types

When you want to build something that can’t be done by existing tasks, simply write your own task type. It is usually best to place the source files for the custom task types in the buildSrc directory or packaged plug-ins. You can then use custom task types just like any task provided by Gradle.

  • Custom task actions.

You can attach custom build logic that is executed before or after a Task through the task.dofirst () and task.dolast () methods.

  • Additional properties for projects and tasks

These allow you to add your own properties to a project or task, which you can then use from your own custom actions or any other build logic. Additional properties can even be applied to tasks that you did not explicitly create, such as tasks created by Gradle’s core plug-ins.

  • Custom conventions

Conventions are a powerful way to simplify builds, so users can understand and use them more easily. You can see this in builds that use standard project structures and naming conventions, such as Java Builds. You can write your own plugins that provide conventions – they just need to configure default values for the relevant aspects of the build.

  • Custom model

Gradle allows you to introduce new concepts into your build beyond task, file, and dependency configurations. You can see this in most language plug-ins, which add the concept of source sets to the build. Proper modeling of the build process can greatly improve the ease of use and efficiency of builds.