What is Gradle?

What It Does

  • Builds your app automatically
  • Manages project configuration
  • Handles dependencies
  • Creates different versions of your app

When you create a new Android Studio project, it starts with about 80 files. When you click the Run button, Gradle:

  • Generates additional files
  • Compiles your code
  • Downloads needed libraries
  • Creates the final app package
  • Ends up with about 700 files in your project

Project vs App: Understanding the Structure

Project (The House)

  • Contains everything needed to build your app
  • Includes multiple modules (like rooms in a house)
  • Has one project-level build file (the house's blueprint)
  • Manages project-wide settings and plugins
  • Can contain multiple apps or libraries

App Module (The Room)

  • Is one specific module in your project
  • Has its own build file (the room's blueprint)
  • Contains the actual app code and resources
  • Manages app-specific settings and dependencies
  • Is what users will install on their devices

Key Gradle Features

Sensible Defaults: The Smart Assistant

Gradle comes with smart default settings, so you don't have to configure everything yourself. It's like having a chef who knows the basic recipe - you only need to tell them if you want something different.

Dependencies: The Shopping List

Dependencies are like ingredients your app needs to work. For example, if your app needs to show a map, it needs the Google Maps library. Gradle can:

  • Find libraries your app needs
  • Download them automatically
  • Make sure they work together

Build Variants: Different Versions

Build variants let you create different versions of your app. For example:

  • A free version and a paid version
  • A phone version and a tablet version
  • A test version and a release version

Understanding Gradle Files

Settings File

The settings.gradle.kts file is like the project's address book - it tells Gradle where to find libraries and what to call your project:

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "ThemeDemo"
include(":app")

Project Build File

This is like the master recipe for your entire project. It's usually simple:

plugins {
    alias(libs.plugins.androidApplication) apply false
    alias(libs.plugins.jetbrainsKotlinAndroid) apply false
    alias(libs.plugins.kotlin.compose) apply false
}

Module Build File

This is like the specific recipe for your app. It tells Gradle:

  • What tools to use (plugins)
  • What your app is called (namespace)
  • What Android versions it works with (SDK versions)
  • What libraries it needs (dependencies)

Here's a breakdown of the important parts:

plugins {
    alias(libs.plugins.android.application)  // For building Android apps
    alias(libs.plugins.kotlin.android)       // For using Kotlin
    alias(libs.plugins.kotlin.compose)       // For using Jetpack Compose
}

android {
    namespace = "com.example.conversion"     // Your app's unique name
    compileSdk = 35                         // Android version to build with
    
    defaultConfig {
        applicationId = "com.example.conversion"  // Your app's ID
        minSdk = 28                            // Minimum Android version
        targetSdk = 35                         // Target Android version
        versionCode = 1                        // Version number
        versionName = "1.0"                    // Version name
    }
}

dependencies {
    // Core Android libraries
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    
    // Compose libraries
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.material3)
    
    // Testing libraries
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
}

Version Catalog

The version catalog is like a master ingredient list that keeps track of all the versions. You can find it at Gradle Scripts → libs.versions.toml:

[versions]
agp = "8.3.0"
kotlin = "1.9.0"
coreKtx = "1.12.0"
junit = "4.13.2"
espressoCore = "3.5.1"
appcompat = "1.6.1"
material = "1.11.0"
activity = "1.8.0"
constraintlayout = "2.1.4"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }