Gradle Build Scripts

Here’s an example of what
a Gradle build script looks like. Even without any explanation, it’s reasonably easy to
figure out what’s going on. We have what kind of looks like
a stripped down JSON syntax. However, down here, where it
looks like we’re creating a task, there’s what looks like
a Java print line statement. What’s going on here? Is this script just structured
data like JSON or XML? Or is this actual Java code? Well, the answer is that Gradle scripts
are written in a special purpose build language provided by Gradle. But it’s not a language
that’s developed by scratch. It sits on top of a generic
scripting language called Groovy. Groovy has a lot of syntactic sugar in
other features to allow us to write build scripts that look a lot more like
natural language when compared to using something like Java. Second Groovy integrates
perfectly with java, which is the language that
Gradle platform is written in. The main thing you need to learn when
using Gradle is the Gradle build language which is where the keywords
like Android and task are coming from. But it also helps a lot to understand
how this build language sits on top of Groovy and Java. The entire build script has
what we call a delegate object, which exposes the Gradle build language
to Groovy scripting language within the build script. If you write a Gradle plugin,
you could write it in any language and use the same delegate object. The Gradle build language is
also called the Gradle DSL, or Domain Specific Language. A domain specific language is
a language that is finely tailored for a specific task. In this case the domain we’re
talking about is an Android build. Note, there’s a big difference
between describing your build and providing explicit instructions on how
to actually make the build happen. The Gradle DSL declared in so
you’re only responsible for describing your build and Gradle
itself knows how to make it happen. That means your build scripts
can be much shorter and much easier to understand. However, within the build script
you have a full blown powerful programming language at your disposal. We do recommend that you keep
build scripts declarative, and try not to pollute it
with low-level logic. That’s what Gradle plugins are for. You can write them in any JVM
language like Java, Groovy or Scala.

Leave a Reply

Your email address will not be published. Required fields are marked *