The Wayback Machine - https://web.archive.org/web/20240608081604/https://www.geeksforgeeks.org/android-build-gradle/
Open In App

Android | build.gradle

Last Updated : 24 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Gradle is a build system (open source) that is used to automate building, testing, deployment, etc. “build.gradle” are script where one can automate the tasks. For example, the simple task of copying some files from one directory to another can be performed by the Gradle build script before the actual build process happens. 

Why is Gradle Needed?

Every Android project needs a Gradle for generating an apk from the .java and .xml files in the project. Simply put, a Gradle takes all the source files (java and XML) and applies appropriate tools, e.g., converts the java files into dex files and compresses all of them into a single file known as apk that is actually used. There are two types of build.gradle scripts  

  • Top-level build.gradle
  • Module-level build.gradle

Top-level build.gradle:

It is located in the root project directory and its main function is to define the build configurations that will be applied to all the modules in the project. It is implemented as:  

Gradle
// Top-level build file where you can add configuration
// options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        mavenCentral()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.3'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

The top-level build.gradle supports various build configurations like:  

buildscript: This block is used to configure the repositories and dependencies for Gradle. 

Note: Don’t include dependencies here. (those will be included in the module-level build.gradle)

dependencies: This block in buildscript is used to configure dependencies that the Gradle needs to build during the project. 

Gradle
classpath 'com.android.tools.build:gradle:3.0.1'       

This line adds the plugins as a classpath dependency for gradle 3.0.1. 

  1. allprojects: This is the block where one can configure the third-party plugins or libraries. For freshly created projects android studio includes mavenCentral() and Google’s maven repository by default. 
  2. task clean(type:Delete): This block is used to delete the directory every time the project is run. This way the projects keep clean when someone modifies some config files like, during settings.gradle which requires a complete clean. 

Module-level build.gradle:

Located in the project/module directory of the project this Gradle script is where all the dependencies are defined and where the SDK versions are declared. This script has many functions in the project which include additional build types and override settings in the main/app manifest or top-level build.gradle file. It is implemented as:  

Gradle
// The first line in this file indicates
// the Android plugin is applied for Gradle to
// this build

apply plugin : 'com.android.application'

android
{
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
    {
        applicationId "example.mehakmeet.geeksforgeeks"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
    }
    buildTypes
    {
        release
        {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies
{
    implementation fileTree(include
                            : [ '*.jar' ], dir
                            : 'libs')
        implementation 'com.android.support:appcompat-v7:26.1.0'
}

The Module-level build.gradle supports various build configurations like:  

  1. android: This block is used for configuring the specific android build options. 
    • compileSdkVersion – This is used to define the API level of the app and the app can use the features of this and lower level. 
  2. defaultConfig: 
    • applicationId– This is used for identifying unique id for publishing of the app.
    • minSdkVersion– This defines the minimum API level required to run the application.
    • targetSdkVersion– This defines the API level used to test the app.
    • versionCode– This defines the version code of the app. Every time an update needs to be of the app, the version code needs to be increased by 1 or more.
    • versionName– This defines the version name for the app. this could be increased by much while creating an update.
  3. buildTypes(release): 
    • minifyEnabled– this will enable code shrinking for release build.
    • proguardFiles– this will specify the progaurd settings file.
  4. dependencies: This specifies the dependencies that are needed to build the project.

Both the top-level and module-level build.gradle files are the main script files for automating the tasks in an android project and are used by Gradle for generating the APK from the source files.



Previous Article
Next Article

Similar Reads

How to Speed Up Gradle build in Android Studio?
Gradle is one of the most important files or extensions that are present in the Android Project. Gradle handles all the libraries and application IDs and many important components of any Android application. Every time when we run our application or we build our apk we will get to see the message as Gradle build running and many more. In some devic
4 min read
How to Build Android Applications with Gradle?
There are several contemporary build automation technologies available nowadays that may be utilized to create a quicker build when creating a project. Gradle is one of these modern-day automation technologies. Gradle is now used by Android to automate and control the build process while also defining flexible custom build settings. As a result, it
8 min read
Different Ways to fix "Error running android: Gradle project sync failed" in Android Studio
Gradle is one of the most important components of Android apps. It handles all the backend tasks and manages all the external dependencies which we will be going to use in building our Android Application. Sometimes due to any issue or after formatting of your pc. Some of the Gradle files may get deleted unexpectedly. So when you will start buildin
3 min read
Fix "Error running android: Gradle project sync failed. Please fix your project and try again" in Android Studio
In this article, we will see how to fix the error: "Gradle project sync failed. Please fix your project and try again" in Android Studio". Before getting into the solution part, let's discuss Gradle. What is Gradle? Gradle is an open-source build automation tool that automates the creation of applications. The various steps involved in creating an
5 min read
How to Update Gradle in Android Studio?
The Android Studio build system is based on Gradle, and the Android Gradle plugin adds several features that are specific to building Android apps. Although the Android plugin is typically updated in lock-step with Android Studio, the plugin (and the rest of the Gradle system) can run independent of Android Studio and be updated separately. For the
2 min read
How to Fix Gradle: Execution failed for task ':processDebugManifest' Error in Android Studio?
Gradle assemble-info provided me an indication that the manifestations do not have various versions of SDK and can not be integrated. And after hours of working on some important project you suddenly face such an issue: Well, getting rid of this is easy, just follow the below-mentioned methods, and your issue will be fixed in a breeze! Method #1: C
3 min read
Different Ways to Set Gradle Home While Importing Existing Project in Android Studio
Gradle has its own Groovy library, thus it is not necessary to install Groovy. Gradle ignores any current Groovy installation. Gradle will utilize the first JDK it finds in your path. Alternatively, you may change the JAVA HOME environment variable to refer to the appropriate JDK's installation location. But sometimes, as software is unpredictable,
2 min read
Gradle Tips and Tricks For Android
Gradle is a leading built automation toolkit for Android that manages to build almost any type of software. It's open-source and supports multi-language development and with the assistance of Gradle, you can do things like Compiling source code, packaging into binary code, manage dependencies, running automated tests, and deploying. Gradle Tips and
3 min read
Gradle For Android Developers
As developers, we spend most of our time writing clean, memory-efficient code, but the end-user only sees the final application build that is installed on the device. So, how come our code and XML files are used to generate a build? Gradle in Android Studio allows for this. So, instead of the usual coding standards, we discuss as developers, let's
7 min read
How to Build a Number Shapes Android App in Android Studio?
Android is a vast field to learn and explore and to make this learning journey more interesting and productive we should keep trying new problems with new logic. So, today we are going to develop an application in android studio which tells us about Numbers that have shapes. We will be covering two types of numbers i.e. triangular and square. So, f
4 min read
Article Tags :