Gradle In Android

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

Gradle is a 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), 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.

Exploring the Gradle Files
  1. Whenever you create a project in Android Studio, the build system automatically generates all the necessary Gradle build files.
  2. 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.
  3. 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:
  4. 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.
  5. 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 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.

This file references all the modules that make up your project. (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. (SDK Location)
This file tells the Android Gradle plugin where it can find your Android SDK installation.

Note that 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.

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.
  1. Minimal Configuration Required for New Projects because Gradle has defaults configurations for your android studio projects.
  2. Dependancy Declaration. You can declare dependency jar files or library files that is hosted in local or remote server.
  3. Gradle automatically generates a test directory and a test APK from your project's source.
  4. 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.
  5. Gradle can generate multiple APKs with different package and build configurations from a single module.

Popular posts from this blog

Shake effect in Android

Async Task in Android

Example For ListView Using BaseAdapter