update - where is build.gradle file in android studio




What is Gradle in Android Studio? (15)

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?


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.


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

Advantages:

  • 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

  1. 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.

  1. 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.


Gradle is a build system running on Android Studio.

In other languages for example:


I refer two tutorial to write the Answer one,two

Gradel is a general purpose, declarative build tool. It is general purpose because it can be used to build pretty much anything you care to implement in the build script. It is declarative since you don't want to see lots of code in the build file, which is not readable and less maintainable. So, while Gradle provides the idea of conventions and a simple and declarative build, it also makes the tool adaptable and developers the ability to extend. It also provides an easy way to customize the default behavior and different hooks to add any third-party features.

Gradle combines the good parts of both tools and provides additional features and uses Groovy as a Domain Specific Language (DSL). It has power and flexibility of Ant tool with Maven features such as build life cycle and ease of use.

Why Gradle? Why Now?

The build tool's response is to add scripting functionality through nonstandard extension mechanisms. You end up mixing scripting code with XML or invoking external scripts from your build logic. It's easy to imagine that you'll need to add more and more custom code over time. As a result, you inevitably introduce accidental complexity, and maintainability goes out the window.

Let's say you want to copy a file to a specific location when you're building the release version of your project. To identify the version, you check a string in the metadata describing your project. If it matches a specific numbering scheme (for example, 1.0-RELEASE), you copy the file from point A to point B. From an outside perspective, this may sound like a trivial task. If you have to rely on XML, the build language of many traditional tools, expressing this simple logic becomes fairly difficult.

Evolution of Java Build Tools

Java build logic has to be described in XML. XML is great for describing hierarchical data but falls short on expressing program flow and conditional logic. As a build script grows in complexity, maintaining the building code becomes a nightmare.

In Ant, you make the JAR target depend on the compile target. Ant doesn't give any guidance on how to structure your project. Though it allows for maximum flexibility, Ant makes each build script unique and hard to understand. External libraries required by your project are usually checked into version control because there is no automated mechanism to pull them from a central location.

Maven 1, released in July 2004, tried to ease that process. It provided a standardized project and directory structure, as well as dependency management. Unfortunately, custom logic is hard to implement

Gradle fits right into that generation of build tools and satisfies many requirements of modern build tools (Figure 1). It provides an expressive DSL, a convention over configuration approach, and powerful dependency management. It makes the right move to abandon XML and introduce the dynamic language Groovy to define your build logic. Sounds compelling, doesn't it?

Gradle combines the best features of other build tools.

Gradle's Compelling Feature Set

Why Build Your Java Projects with Gradle Rather than Ant or Maven?

The default build tool for Android (and the new star of build tools on the JVM) is designed to ease scripting of complex, multi-language builds. Should you change to it, though, if you're using Ant or Maven?

The key to unlocking Gradle's power features within your build script lies in discovering and applying its domain model, as shown in below image.

Gradle can't know all the requirements specific to your enterprise build. By exposing hooks into lifecycle phases, Gradle allows for monitoring and configuring the build script's execution behavior.

Gradle establishes a vocabulary for its model by exposing a DSL implemented in Groovy. When dealing with a complex problem domain, in this case, the task of building software, being able to use a common language to express your logic can be a powerful tool.

Another example is the way you can express dependencies to external libraries, a very common problem solved by build tools. Out-of-the-box Gradle provides you with two configuration blocks for your build script that allow you to define the dependencies and repositories that you want to retrieve them from. If the standard DSL elements don't fit your needs, you can even introduce your own vocabulary through Gradle's extension mechanism.

Integration with Other Build Tools

Gradle plays well with its predecessors' Ant, Maven, and Ivy, as shown in the image below.

Automating Your Project from Build to Deployment

In image: Stages of a deployment pipeline.

  • Compiling the code

  • Running unit and integration tests

  • Performing static code analysis and generating test coverage

  • Creating the distribution

  • Provisioning the target environment

  • Deploying the deliverable

  • Performing smoke and automated functional tests

I refer two tutorial to write the Answer one,two


Gradle = Groovy + Cradle Hans Dockter forum comment

The confusion is a bit unnecessary when it could have just been called "Build" or something in Android Studio.

We like to make things difficult for ourselves in the Development community.


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

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


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.


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.


Gradle is what makes it possible to automate the building of complex Android projects that involve 10s of thousands of lines of code from multiple sources, projects, libraries etc. It can conditionally generate multiple optimised APKs based on a plethora of configuration specifications - if you are interested, the other answers provide more details of this aspect of Gradle.

However, if you're new to Android developing, Gradle in 99% of cases is what stops your project from building. It is an inscrutable, complex system that effectively obfuscates the Android build process and essentially renders it unavailable to inexperienced developers, ie in order to build a simple entry level Android App the unsuspecting newbie might need to study and understand many things that they didnt bargain for such as:

  • Android APK structure and ecosystem
  • Android Studio
  • Java Classpaths and dependencies
  • Groovy
  • Gradle build scripts
  • Many other complex and interesting technologies

All these things are interesting and useful for Android developers to know, but they are far from easy and present a formidable barrier to entry. I suspect that what inspired the OP to ask this question is the feeling of frustration that inevitably hits the neophyte developer after spending way too long trying to get a simple app to build and being continually thwarted by Gradle. The problem is perversely exacerbated by the overwhelming quantity of highly technical documentation that is available for all these technologies. Also for a large amount of development needs Gradle is overkill.

An alternative is to write a shell script that builds your project by automating the tools available in the android SDK. The virtues of this approach are many, for starters its probably the best way to study and understand the build process and the Android ecosystem, and it allows you to completely control how your app is built. However this approach is more suitable for deeply irredeemable tech-heads than it is to inexperienced noobs trying out android.

What is conspicuous by its absence (please inform me if there is such a thing) is an entry level, lightweight IDE with a reduced feature set that simultaneously simplifies the build process while not obscuring it (so not netbeans or eclipse) it could possibly still use Gradle (what was wrong with Ant). It should make it easy to generate APKs that conform to a few common configurations and use a project structure that can evolve to a full Android Studio project should you decide to take it that way.


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.


In plain terms, Gradle is a tool provided by Android Studio in order to implement two important processes:

  1. Build our projects
  2. Package AndroidManifest.xml,res folder,and binary code into a specially formatted zip file called APK

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.


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


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




build-automation