Reviewing Your Gradle Scripts
In the discussion of Android Studio, this book has mentioned something called “Gradle”, without a lot of explanation.
In this chapter, the mysteries of Gradle will be revealed to you.
(well, OK, some of the mysteries…)
Gradle: The Big Questions
First, let us “set the stage” by examining what this is all about, through a series of fictionally-asked questions (FAQs).
What is Gradle?
Gradle is software for building software, otherwise known as “build automation software” or “build systems”. You may have used other build systems before in other environments, such as make
(C/C++), rake
(Ruby), Ant (Java), Maven (Java), etc.
These tools know — via intrinsic capabilities and rules that you teach them — how to determine what needs to be created (e.g., based on file changes) and how to create them. A build system does not compile, link, package, etc. applications directly, but instead directs separate compilers, linkers, and packagers to do that work.
Gradle, as used by default in Android Studio Arctic Fox, uses a domain-specific language (DSL) built on top of Groovy to accomplish these tasks.
What is Groovy?
There are many programming languages that are designed to run on top of the Java VM. Kotlin is one of particular importance for Android developers. Groovy is another.
As with Java, Groovy supports:
- Defining classes with the
class
keyword - Creating subclasses using
extends
- Importing classes from external JARs using
import
- Defining method bodies using braces (
{
and}
) - Objects are created via the
new
operator
Groovy also resembles Kotlin in some ways:
- You can have free-standing statements outside of a class
- You can use string interpolation (e.g.,
"Hello, $name"
to dynamically insert aname
value into the string) - You can skip types on variable declarations (e.g.,
def foo = 1;
, akin tovar foo = 1
in Kotlin)
What Does Android Have To Do with Gradle?
Google has published the Android Gradle Plugin, which gives Gradle the ability to build Android projects. Google is also using Gradle and the Android Gradle Plugin as the build system behind Android Studio.
Hey, I Thought I Read That Gradle Used Kotlin Scripts?
There is an option, starting with Android Studio 4.0, to use Kotlin scripts for defining your Gradle builds, instead of Groovy scripts. If you see a project with build.gradle.kts
files instead of build.gradle
files, that project is using Kotlin Gradle scripts instead of Groovy ones.
This is likely to prove to be the long-term direction for Android. However, this book is going to focus on Groovy scripts, for a few reasons:
- The new-project wizard still generates Groovy scripts
- The vast majority of existing projects — such as the one you might start helping on — will be using Groovy scripts
- In the end, both scripts wind up doing the same work, with the sole difference being some syntax variances between Groovy and Kotlin
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.