Saturday, September 7, 2024

Gradle in Android Application : App level, Project Level, and Gradle Wrapper

In an Android project, Gradle is the build system used to compile, test, and package the application. The project contains several Gradle files, each serving different purposes:


1. App-level Gradle File (build.gradle in the app module)

Location: app/build.gradle

This file is specific to the app module (or any module you have in your project) and contains configuration related to the app itself. It includes dependencies, compile options, and settings for how the app should be built.

Common sections:

android {}: Defines compile SDK version, build types (debug, release), signing configurations, etc.

dependencies {}: Lists external libraries and other dependencies required for the app.

Flavors and Build Types: If you have product flavors or build types (e.g., debug, release), they are defined here.


Example is as below 


android {

    compileSdkVersion 33

    defaultConfig {

        applicationId "com.example.myapp"

        minSdkVersion 21

        targetSdkVersion 33

        versionCode 1

        versionName "1.0"

    }


    buildTypes {

        release {

            minifyEnabled false

            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

        }

    }

}


dependencies {

    implementation 'com.android.support:appcompat-v7:28.0.0'

    implementation 'com.google.code.gson:gson:2.8.8'

}


2. Project-level Gradle File (build.gradle in the root of the project)


Location: build.gradle (root project directory)

This file applies to the entire project and defines configuration options that apply to all modules (app module, library modules, etc.). It’s responsible for managing global settings and plugin versions.

Common sections:


buildscript {}: Specifies the repositories and dependencies required for the build process, including the Android Gradle plugin.

allprojects {} or subprojects {}: Define repositories and settings that apply to all or certain modules in the project.


Example 


buildscript {

    repositories {

        google()

        mavenCentral()

    }

    dependencies {

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

        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10'

    }

}


allprojects {

    repositories {

        google()

        mavenCentral()

    }

}


task clean(type: Delete) {

    delete rootProject.buildDir

}



3. Gradle Wrapper (gradle-wrapper.properties and related files)


Location: gradle/wrapper/gradle-wrapper.properties and gradlew, gradlew.bat files

The Gradle Wrapper is a tool that allows you to execute Gradle builds without requiring users to install Gradle manually. It ensures that the project uses a specific version of Gradle, creating a consistent build environment for all developers and CI/CD systems.

Key Components:


gradle-wrapper.properties: This file specifies the version of Gradle to use and the location where it should be downloaded from if not present locally.

gradlew and gradlew.bat: These are shell and batch scripts (for Unix/Linux and Windows, respectively) that can be used to run the Gradle build without requiring Gradle to be installed globally.


Example 

distributionBase=GRADLE_USER_HOME

distributionPath=wrapper/dists

distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip

zipStoreBase=GRADLE_USER_HOME

zipStorePath=wrapper/dists


Summary:

App-level Gradle (app/build.gradle): Manages app-specific settings, dependencies, and build configurations.

Project-level Gradle (build.gradle in root): Contains global project settings, repositories, and plugin definitions.

Gradle Wrapper: Ensures that all developers and CI systems use a consistent version of Gradle, allowing the project to build in a stable and repeatable way. It includes gradlew, gradlew.bat, and the gradle-wrapper.properties file.


 

No comments:

Post a Comment