Introduction to Gradle and build.gradle files

Introduction-to-Gradle-and-build.gradle-files.png

14th December, 2022

The Gradle build system is used to build Android applications in Android Studio. Android projects are handled by the Gradle system which is an automated build system.

An Android Studio project contains several Gradle files that are used to define the build configuration. The Gradle build system is responsible for Android app code compilation, testing, deployment, and converting the code into .dex files.

To use the Gradle build system in Android studio, you don’t need to download any external software or other tool. Android Studio comes with the Gradle build system pre-installed. When you run an app in Android studio, a Gradle task is automatically triggered and starts building the app. After the Gradle task completes, the application will be launched in your device or AVD.

In short, Gradle is :
→ an advanced build toolkit
→ used to build Android packages (APK files)
→ based on the concepts of Apache Maven and Apache Ant
→ prevents many common build errors
→ distributed as Free Software under the Apache License 2.0

Android APK Build Process

The build process is shown in the figure below.

In this process, the compiler takes the Androidmanifest.xml file, source code, resource files and jar files and converts them into .dex(Dalvik Executable) files. APK Packager then converts the .dex files and all other compiled resources in a single .apk file. If you have provided a keystore, APK Packager signs the Debug or Release apk.

build.gradle Files

An Android Studio project has two main build.gradle files.

1. Top/Project level build.gradle file

The top level build.gradle file is located in the root project directory. The top level build.gradle file defines the build configurations for all modules and sub-projects of the project.

<pre class="brush:php; title:build.gradle; notranslate" title="build.gradle">
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.3.0' apply false
    id 'com.android.library' version '7.3.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}
</pre>

2. Module level build.gradle file

The module level build.gradle file is located in the Project/Module/ directory. This file contains the Android application project’s Application ID, CompileSdk, TargetSdk, VersionCode, VersionName, BuildTypes, CompileOptions, and Dependencies. The module level build.gradle file is used to define the build configuration of specific module where it is located.

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    namespace 'com.example.myapplication1' // used to access app resources
    compileSdk 32 // android sdk version that used to compile 

    defaultConfig {
        applicationId "com.example.myapplication1" // unique id of the app
        minSdk 22 // define minimum android sdk version that app is required to run
        targetSdk 32 // the android sdk version that is used to test the app
        versionCode 1 // app version code
        versionName "1.0" // app version name

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes { // it defines how the application is packaged
        release {
            minifyEnabled false //this will enable code shrinking for release build
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' //progaurd settings
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8 //Java version compatibility to use when compiling Java source
        targetCompatibility JavaVersion.VERSION_1_8 //Java version to generate classes for
    }
    kotlinOptions {
        jvmTarget = '1.8' //target version of the generated JVM bytecode
    }
    buildFeatures {
        viewBinding true //view binding
    }
}

dependencies {//the things that supports in building your project
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Happy coding!

Leave a Reply