What is Gradle in Android Studio?

androidcodehunter 09/03/2017. 21 answers, 233.344 views
android android android-studio android-studio gradle build-automation

Gradle is bit confusing to me and also for new Android developer. Can anyone explain what gradle in Android Studio is and what its purpose is? Why is Gradle included in Android Studio?

5 Comments
3 Eng.Fouad 05/25/2013
10 Adam S 05/25/2013
To answer why you would want a build tool such as Gradle, check out this post from Jeff Atwood.
9 Twisted 04/06/2014
Searching for "gradle" turned up this as 1st result
523 Noni A. 05/19/2014
When someone asks a question on here that yes, does have a lot of links in Google about it, what they are really asking is for someone to answer the question with plain language and to put it in context, such as why do we need it, who will use it most, do we need to know it to build Android apps in Android Studio, etc. Because most often the links you find about it on Google are in "tech-speak" and for a beginner this is confusing.
8 KK_07k11A0585 07/04/2014
@NoniA. I totally agree with you.

21 Answers


Gagandeep Singh 06/19/2017.

Short Answer

Gradle is a build system.

Long Answer

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, 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, what that means is that 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 for Java) from sources then you can write a complete plugin in Java or Groovy(or Kotlin, see here), and distribute it to rest of world.

Why did Google use it?

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.

What can you do in a build script?

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.

5 comments
5 jfmessier 01/19/2015
I have to say that Gradle was giving me issues when taking an AndroidStudio project from one location to another. Mismatches of versions, missing modules, whatnot... it was giving me hard time. I ended up re-creating the project skeleton and cutting/pasting the parts of code that were from the old project to the new one. And this is under Windows for both sources and destination. I am going to move it now under a Linux-based lab.
4 AaA 03/04/2015
What I don't understand is why gradle tries to connect to Internet for every single project that I make or even every single time I want to compile
20 user1700737 03/08/2015
I just want to add my projects to GITHUB and be able to restore them from there and make a apk file. I DON*T need an awesome enterprise tool with a learning curve steeper than flying the space shuttle and wasting my time with all kinds of gradle errors. Bad move Google
4 Arne Evertsson 08/18/2015
I thought IntelliJ already knew how to build Android projects without the need for another build system.
8 Stan Kurdziel 10/28/2015
Another key advantage of gradle over the pre Android studio build system(s) is dependency management. Previously to use an Android library in a project, one would have to download the source code, add it to your project and compile it with your project. Instead, with gradle one can add a single line to the build.gradle and gradle will then download the compiled library from a public repository and add it to your project. Some examples: blog.teamtreehouse.com/android-libraries-use-every-project This also works for standard Java jar files instead of putting them in the lib directory.

daniel_c05 05/26/2013.

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 on Google I/O here.

There is also another talk on Android Studio by Xavier and Tor Norbye, also during Google I/O here.

2 comments
1 Arne Evertsson 08/18/2015
I thought IntelliJ already knew how to build Android projects without the need for another build system.
user5562706 01/24/2016
So is gradle just the background process that compiles the code I have written? Also, after looking at lots of other answers, they mention dependencies. What are those? THanks so much daniel!

JIM 03/12/2015.

1 - Gradle Background Information.

2 - Gradle concepts.

3 - The Initialization Phase.

4 - The Initialization Phase(Cont.).

5 - What is Gradle ?.

6 - The Configuration Phase .

Gradle

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

See this LINK


phi 08/21/2015.

Gradle is a build system running on Android Studio.

In other languages for example:


Chavan Parmeshwar 12/12/2015.

Here is Detailed explanation about what is Gradle and how to use it in android studio.

Exploring the Gradle Files

  1. Whenever you create a project in Android Studio, the build system automatically generates all the necessary Gradle build files.

Gradle Build Files

  1. Gradle build files use a Domain Specific Language or DSL to define custom build logic and to interact with the Android-specific elements of the Android plugin for Gradle.

  2. Android Studio projects consist of one or more modules, which are components that you can build, test, and debug independently. Each module has its own build file, so every Android Studio project contains two kinds of Gradle build files:

  3. Top-Level Build File: This is where you'll find the configuration options that are common to all the modules that make up your project.

  4. Module-Level Build File: Each module has its own Gradle build file that contains module-specific build settings. You'll spend most of your time editing module-level build file(s) rather than your project's top-level build file.

To take a look at these build.gradle files, open Android Studio's Project panel (by selecting the Project tab) and expand the Gradle Scripts folder. The first two items in the Gradle Scripts folder are the project-level and module-level Gradle build files

Top-Level Gradle Build File

Every Android Studio project contains a single, top-level Gradle build file. This build.gradle file is the first item that appears in the Gradle Scripts folder and is clearly marked Project.

Most of the time, you won't need to make any changes to this file, but it's still useful to understand its contents and the role it plays within your project.

Module-Level Gradle Build Files

In addition to the project-level Gradle build file, each module has a Gradle build file of its own. Below is an annotated version of a basic, module-level Gradle build file.

Other Gradle Files

In addition to the build.gradle files, your Gradle Scripts folder contains some other Gradle files. Most of the time you won't have to manually edit these files as they'll update automatically when you make any relevant changes to your project. However, it's a good idea to understand the role these files play within your project.

gradle-wrapper.properties (Gradle Version)

This file allows other people to build your code, even if they don't have Gradle installed on their machine. This file checks whether the correct version of Gradle is installed and downloads the necessary version if necessary.

settings.gradle

This file references all the modules that make up your project.

gradle.properties (Project Properties)

This file contains configuration information for your entire project. It's empty by default, but you can apply a wide range of properties to your project by adding them to this file.

local.properties (SDK Location)

This file tells the Android Gradle plugin where it can find your Android SDK installation.

Note that local.properties contains information that's specific to the local installation of the Android SDK. This means that you shouldn't keep this file under source control.

For more details go through -

read:http://code.tutsplus.com/tutorials/the-ins-and-outs-of-gradle--cms-22978

I got clear understanding of gradle from this.

1 comments
1 kleopatra 12/12/2015
Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference.

karan 11/16/2015.

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.

3 comments
2 committedandroider 12/02/2014
What do you mean by building the source code of the program? Isn't that the job of a developer?
AutomEng 05/31/2015
This answer is completely wrong in the way it is worded. Gradle is the packaging tool that comes bundled with Android Studio, so what it takes care of is building the apk. The source code is always being built by the compiler and nothing else.
Michael 06/30/2015
Yet from another question it sounds like Gradle won't work properly without a reliable internet conneciton, does this mean I have to throw away Android Studio until that is fixed?

penguin 08/09/2016.

Gradle is a build system. Build systems are software tools designed to automate the process of program compilation. Build systems come in various forms, and are used for a variety of software build tasks. While their primary goal is to efficiently create executables.

Another related term is Build automation which is the process of automating the creation of a software build and the associated processes including: compiling computer source code into binary code, packaging binary code, and running automated tests.

Few similar build system for other languages are (see complete list here):

  1. Apache Ant & Apache Maven - Java
  2. sbt (Simple Build Tool) - for Scala (Play framework etc)
  3. A-A-P - Python based build tool
  4. Rake (Apache Builder) - Ruby
  5. Leiningen for Clojure

Mahdi Rashidi 11/16/2015.

You can find everything you need to know about Gradle here:Gradle Plugin User Guide

Goals of the new Build System

The goals of the new build system are:

  • Make it easy to reuse code and resources
  • Make it easy to create several variants of an application, either for multi-apk distribution or for different flavors of an application
  • Make it easy to configure, extend and customize the build process
  • Good IDE integration

Why Gradle?

Gradle is an advanced build system as well as an advanced build toolkit allowing to create custom build logic through plugins.

Here are some of its features that made us choose Gradle:

  • Domain Specific Language (DSL) to describe and manipulate the build logic
  • Build files are Groovy based and allow mixing of declarative elements through the DSL and using code to manipulate the DSL elements to provide custom logic.
  • Built-in dependency management through Maven and/or Ivy.
  • Very flexible. Allows using best practices but doesn’t force its own way of doing things.
  • Plugins can expose their own DSL and their own API for build files to use.
  • Good Tooling API allowing IDE integration

interstar 08/21/2016.

At the risk of being discursive I think behind this is the question of why the Android Studio / Gradle experience is so bad.

Typical Clojure experience :

  • download project with dependencies listed in project.clj.
  • Leiningen gets the dependencies thanks to Clojars and Maven.
  • Project compiles.

Typical Android Studio / Gradle experience :

  • "Import my Eclipse project".
  • OK project imported.
  • Gradle is doing it's thang ... wait ... wait ... wait ... Gradle has finished.
  • Compile ... can't compile because I don't know what an X is / can't find Y library.

I'm not sure this is Gradle's fault exactly. But the "import from Eclipse project" seems pretty flaky. For all of Gradle's alleged sophistication and the virtues of a build-system, Android Studio just doesn't seem to import the build dependencies or build-process from Eclipse very well.

It doesn't tell you when it's failed to import a complete dependency graph. The Android Studio gives no useful help or tips as to how to solve the problem. It doesn't tell you where you can manually look in the Eclipse folders. It doesn't tell you which library seems to be missing. Or help you search Maven etc. for them.

In 2016 things like Leiningen / Clojars, or node's npm, or Python's pip, or the Debian apkg (and I'm sure many similar package managers for other languages and systems) all work beautifully ... missing dependencies are thing of the past.

Except with Android. Android Studio is now the only place where I still seem to experience missing-dependency hell.

I'm inclined to say this is Google's fault. They broke the Android ecosystem (and thousands of existing Android projects / online tutorials) when they cavalierly decided to shift from Eclipse to Android Studio / Gradle without producing a robust conversion process. People whose projects work in Eclipse aren't adapting them to AS (presumably because it's a pain for them). And people trying to use those projects in AS are hitting the same issues.

And anyway, if Gradle is this super-powerful build system, why am I still managing a whole lot of other dependencies in the sdk manager? Why can't a project that needs, say, the ndk specify this in its Gradle file so that it gets automatically installed and built-against when needed? Why is NDK special? Similarly for target platforms? Why am I installing them explicitly in the IDE rather than just checking my project against them and having this all sorted for me behind the scenes?

1 comments
2 csmith 10/31/2016
I fully agree that Gradle is a terrible build system. It is the component that makes Android Studio far worse than X-Code or Visual Studio. I realise these must "build" the system behind the scenes but I'm not really required to know/care/tinker with their internals. Gradle on the other hand seems to break with every Android Studio update and bogs down the system even just adding a class. No other IDE does that.

SKallmann 11/17/2016.

Gradle is an advanced build system as well as an advanced build toolkit allowing to create custom build logic through plugins!

Dsl – domain specific language , based on groovy

DAG- directed acyclic graph

Incremental builds

Extensible domain model Gradle is always up to date!

Before a task is being execute, Gradle takes a snapshot of its task’s input and output.

In case the snapshot has changed or it doesn’t exists, Gralde will re- execute this task.

manifest entries Through the DSL it is possible to configure the following manifest entries:

Build variant- By default, the Android plugin automatically sets up the project to build both a debug and a release version of the application.

Dependencies-

Local Dependencies:

If you have binary archives in your local filesystem that a module depends on, such as JAR files, you can declare these dependencies in the build file for that module.

Remote Dependencies:

First the repository must be added to the list, and then the dependency must be declared in a way that Maven or Ivy declare their artifacts.


Devrath 11/26/2016.

DEFINITION:: Gradle can be described a structured building mechanism where it provides a developer the tools and flexibility to manage the resources of a project to create builds that are smaller in size, targeting specific requirements for certain devices of certain configurations


BASIC CONFIGURATIONS

  1. minimumSdk
  2. maximumSdk
  3. targettedSdk
  4. versionCode
  5. versionName

LIBRARIES::We can add android libraries or any other third party libraries in addition as per requirements easy which was a tedious task earlier. If the library does not fit for the existing project, The developer is shown a log where the person can find a appropriate solution to make changes to the project so that the library can be added. Its just one line of dependency


GENERATING VARIETIES OF BUILDS

Combining build types with build flavours to get varities of build varients

 ====================                         ====================
|     BuildTypes     |                       |   ProductFlavours  |
 --------------------  ====================== --------------------
|  Debug,Production  |      ||       ||      | Paid,Free,Demo,Mock|
 ====================       ||       ||       ==================== 
                            ||       ||
                            VV       VV
 =================================================================
|           DebugPaid, DebugFree, DebugDemo, DebugMock            |
|  ProductionPaid, ProductionFree, ProductionDemo, ProductionMock |
 =================================================================

REDUCING SIZE

Gradle helps in reducing the size of the generated build by removing the unused resources also unused things from integrated libraries


MANAGING PERMISSIONS

We can Specify certain permissions for certain builds by adding certain permissions in certain scenarios based on requirements


BUILDS FOR CERTAIN DEVICES

We can manage generating build for certain devices that include certain densities and certain api levels. This helps in product deployments in app store according to requirements across multiple types of devices


GOOD REFERENCE

Vogella Tutorials

1 comments
GhostCat 08/28/2017
Just "compensating" here for all the downvotes that come in on your other question. I do hope you consider to keep that question around ;-)

asok Buzz 02/21/2015.

One Advantage of Gradle that I found is:

Gradle was designed for multi-project builds which can grow to be quite large, and supports incremental builds by intelligently determining which parts of the build tree are up-to-date, so that any task dependent upon those parts will not need to be re-executed.


user500123 05/26/2013.

Gradle is to the Groovy JVM language what ant is to Java. Basically, it's Groovy's build tool. Unlike Ant, it's based on the full Groovy language. You can, for example, write Groovy script code in the Gradle script to do something rather than relying on a specific domain language.

I don't know IntelliJ's specific integration, but imagine you could "extend" Groovy such that you could write specific "build" language primitives and they just became part of the Groovy language. (Groovy's metaprogramming is a whole discussion unto itself.) IntelliJ/Google could use Gradle to build a very high-level build language, yet, it's a language build on an expandable, open standard.

1 comments
2 martyglaubitz 12/19/2013
it's not "Groovys build tool" - it's a multi purpose build tool whose DSL is build on top of groovy

Akash Chandwani 09/18/2015.

In Android Studio, Gradle is a custom build tool used to build android packages (apk files) by managing dependencies and providing custom build logic.

APK file (Android Application package) is a specially formatted zip file which contains

  • Byte code
  • Resources (images, UI, xml etc)
  • Manifest file

An apk file gets signed and pushed to the device using ADB(Android Debug Bridge) where it gets executed.


AndEngine 05/23/2014.

Gradle is an advanced build toolkit for android that manages dependencies and allows you to define custom build logic. features are like 1. Customize, configure, and extend the build process. 2. Create multiple APKs for your app with different features using the same project. 3. Reuse code and resources. refer this link http://developer.android.com/sdk/installing/studio-build.html


Biswajit Karmakar 03/29/2016.

The Gradle build system is designed to support complex scenarios in creating Android applications:

Multi-distribution: the same application must be customized for several clients or companies

Multi-apk: supporting the creation of multiple apk for different device types while reusing parts of the code


Zumry Mohamed 03/20/2017.

Gradle is an automated build toolkit that can integrate into lots of different environments not only for Android projects.

Here are few things that you can do with gradle.

  • Minimal Configuration Required for New Projects because Gradle has defaults configurations for your android studio projects.

  • Dependancy Declaration. You can declare dependency jar files or library files that is hosted in local or remote server.

  • Gradle automatically generates a test directory and a test APK from your project's source.

  • If you add all the necessary information, such as keyPassword and keyAlias, to your Gradle build file, you can use Gradle to generate signed APKs.

  • Gradle can generate multiple APKs with different package and build configurations from a single module.


Dhaval Jivani 04/23/2016.

by @Brian Gardner:

Gradle is an extensive build tool and dependency manager for programming projects. It has a domain specific language based on Groovy. Gradle also provides build-by-convention support for many types of projects including Java, Android and Scala.

Feature of Gradle:

  1. Dependency Management
  2. Using Ant from Gradle
  3. Gradle Plugins
  4. Java Plugin
  5. Android Plugin
  6. Multi-Project Builds

Samarth Shah 07/24/2016.

Gradle is a build tool custom and used for building APK or known as application package kit.


Siva Prakash 12/13/2016.

For more fundamental and thorough understanding of gradle and build system checkout this udacity course created by Gradle engineers themselves.

Gradle for Android and Java


Paul Man 07/18/2017.

a gradle is something to to keep you awake at night, as opposed to a "cradle" which is to help you sleep

Related questions

Hot questions

Language

Popular Tags