Gradle is a build system: its goal is to build your project. To install Gradle:
Gradle revolves around two main concepts:
A task is something Gradle needs to run in order to build your project. Let’s create a file named “hello.gradle” and write the following code in it:
To run the task, open your favorite console app and run the following:
This will output to:
By default, Gradle comes with many tasks and to extend Gradle capabilities, tasks are packaged into Gradle Plugins to offer more specific tasks for your type of project.
Plugins add tasks to your project to build whatever you want to build.
A Java developer which would want to build his Java project, will use a Gradle Plugin called the Java Gradle Plugin.
Gradle is not limited to the Java world! An iOS developer could also use Gradle to build his iOS app by using a specific plugin created to build iOS projects. A .Net developer could build his own .Net project by using a specific .Net Gradle Plugin. You get the idea!
On Android, Google develops the Android Gradle Plugin. This allows new and existing Android Projects to build applications (.apk) and libraries (.aar).
Let’s see a concrete example. Download IntelliJ and create a basic Gradle based Java project:
In this newly created project, you can see two important files:
The settings.gradle file is the main entry point for Gradle in a folder. In this file, the main project is configured. If you have a multi-modules project, the list of modules will be set. In every project/module, a build.gradle file will indicate to Gradle how you intend to build your project/module.
Your build.gradle will look like this:
On line number 4 you can see that the Java Gradle Plugin is applied. This means a list of Java related tasks were added through this plugin and now we can build a Java project with it!
The most important task you need to learn about is a task named “tasks”. This is the first task I will run when I enter an existing project to see what are the tasks that are defined in this project. Run “gradle tasks” inside your newly created project:
The Gradle Wrapper is an executable script that embeds Gradle inside your project. That way every project on your computer can have its own Gradle version and you don’t have to install every Gradle version directly on your computer. Note that you don’t have to use a Gradle wrapper but it is considered a good practice to do so.
And the output will look like this:
You can see now that we have a task named jar which will compile the Java code and package it inside a Jar file that you will find in the folder builds/libs inside your project.
Gradle is awesome! And even more with Kotlin! Everything you do that revolves around your project can be automated into your project Gradle build. For example, at my current job, our team uses daily a Gradle Plugin to download and adapt string translations from an internal company tool the translations team uses.
To continue learning about Gradle you should totally have a look at the official guides!
Thanks to Guy Tsype for proofreading!