Gradle is a bit confusing to me, and also for any new Android developer. Can anyone explain what Gradle in Android Studio is and what its purpose is? Why is it included in Android Studio?
Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.
Gradle is a build system (open source) that is used to automate building, testing, deployment, etc. “build. gradle” are scripts where one can automate the tasks.
Gradle is one type of build tool that builds the source code of the program. So it's an important part of Android Studio, and needs to be installed before starting developing your application. We do not have to install it separately, because the Android Studio does it for us, when we make our first project.
It is popular for its ability to build automation in languages like Java, Scala, Android, C/C++, and Groovy. The tool supports groovy based Domain Specific Language over XML. Gradle provides building, testing, and deploying software on several platforms. The tool is popular for building any software and large projects.
It's the new build tool that Google wants to use for Android. It's being used due to it being more extensible, and useful than Ant. It is meant to enhance developer experience.
You can view a talk by Xavier Ducrohet from the Android Developer Team at Google I/O here.
There is also another talk on Android Studio by Xavier and Tor Norbye, also during Google I/O here.
Gradle is a build system.
Before Android Studio you were using Eclipse for your development purposes, and, chances are, you didn't know how to build your Android APK without Eclipse.
You can do this on the command line, but you have to learn what each tool (dx and AAPT) does in the SDK. Eclipse saved us all from these low-level, but important, fundamental details by giving us their own build system.
Now, have you ever wondered why the res
folder is in the same directory as your src
folder?
This is where the build system enters the picture. The build system automatically takes all the source files (.java
or .xml
), then applies the appropriate tool (e.g., takes .java
class files and converts them to .dex
files), and groups all of them into one compressed file - our beloved APK.
This build system uses some conventions: an example of one is to specify the directory containing the source files (in Eclipse it is \src
folder) or resources files (in Eclipse it is \res
folder).
Now, in order to automate all these tasks, there has to be a script; you can write your own build system using shell scripting in Linux or batch files syntax in Windows. Got it?
Gradle is another build system that takes the best features from other build systems and combines them into one. It is improved based off of their shortcomings. It is a JVM-based build system. That means you can write your own script in Java, which Android Studio makes use of.
One cool thing about Gradle is that it is a plugin-based system. This means if you have your own programming language and you want to automate the task of building some package (output like a JAR file for Java) from sources, then you can write a complete plugin in Java or Groovy (or Kotlin, see here), and distribute it to the rest of the world.
Google saw one of the most advanced build systems on the market and realized that you could write scripts of your own with little-to-no learning curve, and without learning Groovy or any other new language. So they wrote the Android plugin for Gradle.
You must have seen build.gradle
file(s) in your project. That is where you can write scripts to automate your tasks. The code you saw in these files is Groovy code. If you write System.out.println("Hello Gradle!");
then it will print on your console.
A simple example is that you have to copy some files from one directory to another before the actual build process happens. A Gradle build script can do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With