Open In App

Android | build.gradle

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

  • 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:  

Java




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

Java




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:  

Java




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



Last Updated : 14 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads