Introduction to Gradle
Gradle is a build system: its goal is to build your project. To install Gradle:
brew update && brew install gradle
Gradle revolves around two main concepts:
Gradle Tasks
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:
task(hello) {
doLast {
println "Hello World!"
}
}
To run the task, open your favorite console app and run the following:
gradle -b hello.gradle hello
This will output to:
> Task :hello
Hello World!
Congratulations! You just wrote your first Gradle task! Note that the programming language here is Groovy, but Kotlin is on its way!
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.
Gradle Plugins
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:
cd my-java-project
gradle tasks
# or use the Gradle wrapper instead
./gradlew tasks
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.
Conclusion
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!