Android | build.gradle

Gradle is a build system (open source) which 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 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 apply 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Top-level build file where you can add configuration
// options common to all sub-projects/modules.
  
buildscript
{
  
    repositories
    {
        google()
            jcenter()
    }
    dependencies
    {
        classpath 'com.android.tools.build:gradle:3.0.1'
  
        // Do not place the application dependencies here; 
        // they belong to individual module build.gradle files
    }
}
  
allprojects
{
    repositories
    {
        google()
            jcenter()
    }
}
  
task clean(type
           : Delete)
{
    delete rootProject.buildDir
}

chevron_right


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

  1. buildcript: 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.
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

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

  2. allprojects: This is the block where one can configure the third-party plugins or libraries. For freshly created projects android studio includes JCenter and Google’s maven repository by default.
  3. task clean(type:Delete): This block is used to delete the directory everytime the project is run. This way the projects keep clean when someone modify some config files like, during settings.gradle which require 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 includes additional build types and override settings in the main/app manifest or top-level build.gradle file. It is implemented as:

filter_none

edit
close

play_arrow

link
brightness_4
code

// The first line in this file indicates
// the Android plugin is applied for Gradle to
// this build
  
apply plugin : 'com.android.application'
  
android
{
    compileSdkVersion 26 defaultConfig
    {
        applicationId "example.mehakmeet.geeksforgeeks"
        minSdkVersion 19
        targetSdkVersion 26
        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'
}

chevron_right


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. Everytime 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 any 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 specify 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 the Gradle for generating the APK from the source files.

References: https://developer.android.com/studio/build/



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.