Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Android | build.gradle

  • Difficulty Level : Medium
  • Last Updated : 14 Sep, 2021

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. For example, the simple task to copy 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  

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
  • 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:  


// Top-level build file where you can add configuration
// options common to all sub-projects/modules.
buildscript {
    repositories {
    dependencies {
        classpath ''
allprojects {
    repositories {
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. 


classpath ''

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:  


// The first line in this file indicates
// the Android plugin is applied for Gradle to
// this build
apply plugin : ''
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
        applicationId "example.mehakmeet.geeksforgeeks"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''
    implementation fileTree(include
                            : [ '*.jar' ], dir
                            : 'libs')
        implementation ''

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!