Understanding Gradle

igin
igin
Nov 1 · 3 min read

Many of us have had contact with the gradle build system. Mainly when building android apps. But whenever I had to change something in a build.gradle file I had this uneasy feeling of not knowing what I was looking at. In this article I want to dissect the syntax of a typical gradle file like this one:

Basic build.gradle

Groovy

Gradle is a build system that uses the groovy language for its configuration scripts (you can also write them in kotlin if you want). So to understand gradle scripts the first step is grasping the basics of groovy. In the following I want to give you a basic intro to the necessary parts of groovy (most of this information comes directly from the docs).

Basics

Ok so this looks pretty familiar if you have seen some scripting languages. Nothing special here. Groovy was designed with readability in mind so there are some shortcuts the language allows us to make. For example we can omit the parentheses in method calls:

So whenever you see a space between two expressions think about method invocation.

Closures

Similar to many functional style programming languages groovy supports the concept of closures. They are basically anonymous methods that can access the outer scope in some way. You define them with curly braces like this:

Note that defining this closure object does not yet execute the code inside the curly braces. Execution happens once you call thecall method on the closure object.

Passing Arguments to Closures

Obviously you can define closures with arguments. Be aware that if you don’t define any arguments the closure still has a single optional argument called it.

Understanding the gradle file

Equipped with the above knowledge of the groovy language we can begin to understand a basic gradle file. Lets looks at one specific part of such a file.

This is the basic definition of a task that prints “hello there”. A different way to write the same thing is this:

Now we can see that task is actually a normal method that gets called with the task name as a string. So we can guess that this function actually creates a task object and registers that in the project. To be precise it creates an object of the class org.gradle.api.Task . To actually get from the first version to the second gradle uses an internal (not well documented) compile step (as can be read in this stackoverflow answer).

So that explains the first part. But where does this doLast thing come from? For that we actually need another groovy feature called closure delegates. Basically you can tell a closure object to resolve methods by looking into an object first. In this case the delegate of the passed in closure is set to the created Task object. The doLast method is actually a method of the Task class as you can see in the documentation here. It takes as an argument a closure object.

So again another way to write this would be:

The same applies to other parts of a gradle file. See for example:

repositories is a standard method call that gets a closure as an argument. This closure is executed with the delegate set to a RepositoryHandler . That is why we have access to the mavenCentral method. After understanding this it became easier to read and debug gradle scripts for me. I hope you can gain similar benefits.

Gradle phases

One last insight I had during this journey was that gradle execution is divided into two phases. The configuration and the execution phase.

When executing

./gradlew buildSomething

it first executes the whole script. Meaning tasks are created and configured into a task graph. Then the graph for buildSomething will be executed. See more about this here.

Now we just need to learn some Java to actually build something.

    igin

    Written by

    igin

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade