Gradle in Kotlin DSL

Android Studio 2: The New UI

Course 2 ยท Chapter 4 ยท Reading the Build Files

Every Android project is built by Gradle, a build tool that reads a handful of script files, downloads the libraries you ask for and turns your code into an app. You will never get far in Android without opening those files, and they are the part of the IDE that has changed most since older tutorials were written: the scripts are now Kotlin files (.kts), versions live in a separate version catalog, and Android's Gradle plugin is at version 9. This chapter reads a freshly generated Compose project's build files line by line, then shows the two dialogs that show the same information graphically: Project Structure and Run/Debug Configurations.

What was checked, and how
Checked on a real install. The files below are those of a project generated by Android Studio Rabbit 2 (stable, 2026.2.2) from the Empty Activity template, read from disk on 2026-11-21, and the screenshots are of the Gradle Scripts group, the Project Structure dialog (every page except Suggestions) and the Run/Debug Configurations dialog. I replaced the project's own name and package with MyApp and com.example.myapp; the version numbers are exactly as generated.
Checked in the docs. One web search against Google's Android Gradle plugin 9.0 release notes (see the Not Yet Verified box).
Not run by me. I did not edit a build file or sync a project myself; the exercises are untested.

What Gradle, AGP and Sync Are

Three names appear everywhere in this chapter, so fix them now:

NameWhat it is
GradleThe general build tool. It runs tasks (compile, test, package) described in build scripts. The version used is set by the wrapper (below): 9.6.0 in the project checked.
AGP (Android Gradle Plugin)A plugin for Gradle that knows how to build an Android app: resources, manifest, packaging, signing. Its version in this project is 9.5.1.
SyncAndroid Studio asking Gradle to read the build files and report the project's shape (modules, dependencies, source folders), so the IDE can offer correct code completion and error checking. Press it after you edit a build file: File > Sync Project with Gradle Files, Ctrl+Shift+O in Chapter 1's File menu list. Its progress shows on the Sync tab of the Build tool window.

The Gradle Scripts Group

In the Android view (Chapter 3) the bottom entry is Gradle Scripts. It collects seven files, each with a plain-English label in brackets, which is the quickest way to learn which is which:

FileLabel shownIts job
build.gradle.kts(Project: MyApp)The top-level build file. In a new project it only declares which plugins the modules may use.
build.gradle.kts(Module :app)The app's own build file: SDK versions, app ID, build types and dependencies. This is the file you edit most.
gradle.properties(Project Properties)Project-wide switches and the Gradle process's memory.
gradle-wrapper.properties(Gradle Version)Which Gradle version the project downloads and uses.
libs.versions.toml(Version Catalog "libs")The one list of library and plugin versions.
local.properties(SDK Location)Where the Android SDK is on this computer. Machine-specific, so it is normally left out of version control.
settings.gradle.kts(Project Settings)The project's name, its modules and where Gradle looks for plugins and libraries.
Compared with the Classic UI
Android Studio: The IDE Itself 2 shows Groovy files named build.gradle with plugin versions such as 8.1.0 written straight into the project-level file, and library lines in quotes. On a new project today the files are Kotlin (.kts), plugin and library versions are in libs.versions.toml, and the project-level file contains only alias(...) apply false lines. The old lesson of "project-level or module-level file?" has a third answer: the catalog.

The Version Catalog

gradle/libs.versions.toml has three tables. This is the whole file as generated:

# gradle/libs.versions.toml [versions] agp = "9.5.1" coreKtx = "1.19.1" junit = "4.13.2" junitVersion = "1.3.0" espressoCore = "3.7.0" lifecycleRuntimeKtx = "2.11.1" activityCompose = "1.13.0" kotlin = "2.2.10" composeBom = "2026.02.01" [libraries] androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } junit = { group = "junit", name = "junit", version.ref = "junit" } androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" } androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" } androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" } androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" } androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" } androidx-compose-ui = { group = "androidx.compose.ui", name = "ui" } androidx-compose-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" } androidx-compose-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" } androidx-compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" } androidx-compose-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" } androidx-compose-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" } androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3" } [plugins] android-application = { id = "com.android.application", version.ref = "agp" } kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }

Read it in three steps:

  1. [versions] gives each version a name. Change a number here and every library or plugin that refers to it follows.
  2. [libraries] names each library: a group and a name (together, its Maven address) plus version.ref, which points at a line in [versions].
  3. [plugins] does the same for Gradle plugins, using an id.

Notice that the Compose libraries (ui, ui-graphics, material3 and the rest) have no version at all. Their versions are supplied by androidx-compose-bom, the Compose BOM ("bill of materials"): one version number (composeBom = "2026.02.01") that fixes a matching version for every Compose library, so they always fit together.

The naming rule. A dash in a catalog name becomes a dot in the build file. androidx-core-ktx is written libs.androidx.core.ktx, and the plugin kotlin-compose is libs.plugins.kotlin.compose. Android Studio completes these names for you once you type libs..

The Three Kotlin Build Files

settings.gradle.kts

// settings.gradle.kts pluginManagement { repositories { google { content { includeGroupByRegex("com\\.android.*") includeGroupByRegex("com\\.google.*") includeGroupByRegex("androidx.*") } } mavenCentral() gradlePluginPortal() } } plugins { id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0" } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } } rootProject.name = "MyApp" include(":app")

pluginManagement says where Gradle finds plugins (Google's repository, but only for groups starting com.android, com.google or androidx, then Maven Central and the Gradle plugin portal). The foojay-resolver-convention plugin lets Gradle download a Java toolchain it is missing. dependencyResolutionManagement says where modules find libraries (Google and Maven Central), and FAIL_ON_PROJECT_REPOS makes the build fail if a module tries to declare its own repositories, which keeps every module on the same list. rootProject.name is the project name and include(":app") registers the one module.

build.gradle.kts (Project)

// build.gradle.kts (Project: MyApp) plugins { alias(libs.plugins.android.application) apply false alias(libs.plugins.kotlin.compose) apply false }

Two lines. alias(...) refers to a plugin in the catalog, and apply false means "make this plugin available to the modules, but do not apply it to the root project itself". The versions are not here any more: they are in the catalog.

build.gradle.kts (Module :app)

// app/build.gradle.kts plugins { alias(libs.plugins.android.application) alias(libs.plugins.kotlin.compose) } android { namespace = "com.example.myapp" compileSdk { version = release(37) } defaultConfig { applicationId = "com.example.myapp" minSdk = 24 targetSdk = 37 versionCode = 1 versionName = "1.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { optimization { enable = true packageScope = setOf("androidx.**", "kotlin.**", "kotlinx.**") } } } compileOptions { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } buildFeatures { compose = true } } dependencies { implementation(platform(libs.androidx.compose.bom)) implementation(libs.androidx.activity.compose) implementation(libs.androidx.compose.material3) implementation(libs.androidx.compose.ui) implementation(libs.androidx.compose.ui.graphics) implementation(libs.androidx.compose.ui.tooling.preview) implementation(libs.androidx.core.ktx) implementation(libs.androidx.lifecycle.runtime.ktx) testImplementation(libs.junit) androidTestImplementation(platform(libs.androidx.compose.bom)) androidTestImplementation(libs.androidx.compose.ui.test.junit4) androidTestImplementation(libs.androidx.espresso.core) androidTestImplementation(libs.androidx.junit) debugImplementation(libs.androidx.compose.ui.test.manifest) debugImplementation(libs.androidx.compose.ui.tooling) }
Block or lineWhat it does
plugins { alias(...) }Applies the two plugins to this module. There is no kotlin-android plugin: see the next section.
namespaceThe package name used for the generated R class and the code.
compileSdk { version = release(37) }The Android version the code is compiled against. A new form: it replaces the older compileSdk = 34.
applicationId, minSdk, targetSdkThe app's identity on a device (what you chose in the wizard), the oldest Android it supports (24) and the version it is tested for (37).
versionCode, versionNameThe number the store compares and the label users see. You raise these for each release.
buildTypes { release { optimization { ... } } }Settings for the release build: enable = true turns on code shrinking and optimisation for the listed packages. This replaces the older isMinifyEnabled line plus ProGuard file names.
compileOptionsJava source and target level: Java 11.
buildFeatures { compose = true }Switches on Compose for this module.
implementation(platform(libs.androidx.compose.bom))Brings in the Compose BOM so the Compose libraries below it need no versions.
implementation / testImplementation / androidTestImplementation / debugImplementationThe configuration: implementation for the app, test for tests that run on your computer, androidTest for tests that run on a device, debug only in debug builds (the Compose tooling lives here).

AGP 9: Kotlin Is Built In

Older Android projects applied a second plugin, org.jetbrains.kotlin.android, to compile Kotlin. Look back at the files: it is not there. Google's AGP 9.0 release notes say that Kotlin support is now built into AGP and on by default, so an Android app module no longer applies the Kotlin Android plugin. AGP 9.0 brings Kotlin Gradle plugin 2.2.10 with it as a runtime dependency (the same number as kotlin = "2.2.10" in the catalog above), and the notes describe a switch, android.builtInKotlin=false, to opt out. The one Kotlin plugin the project still lists is kotlin-compose, which Compose needs. If you open an older tutorial that adds id("org.jetbrains.kotlin.android"), that line is for the pre-9 way of working.

The odd new-looking forms in the module file (compileSdk { version = release(37) } and the optimization { } block) appear to belong to the new DSL that AGP 9 introduced. The docs summary I found says the old Kotlin plugin is not compatible with it; I did not read the DSL page itself, so treat that link between the forms and the new DSL as my inference.

gradle.properties and the Wrapper

gradle.properties in the generated project had four live lines (everything else was comments):

# gradle.properties (comments removed) org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 # org.gradle.parallel=true org.gradle.configuration-cache=true kotlin.code.style=official
LineMeaning
org.gradle.jvmargs=-Xmx2048m ...The memory limit (2048 MB) and text encoding of the Gradle daemon, the background process that does the building. This is not the memory of the IDE itself, which is a separate setting.
org.gradle.parallel=trueCommented out (a leading #), so off. It lets independent modules build at the same time; the generated comment says to use it only with decoupled projects.
org.gradle.configuration-cache=trueOn. Gradle can skip its configuration phase when nothing that affects the build configuration has changed, which makes repeat builds faster.
kotlin.code.style=officialTells the IDE to use Kotlin's official code style for this project.

The wrapper file, gradle/wrapper/gradle-wrapper.properties, names the exact Gradle version. In this project: distributionUrl=https\://services.gradle.org/distributions/gradle-9.6.0-bin.zip, with a checksum line and validateDistributionUrl=true. The wrapper means anyone who opens the project, on any computer, downloads and uses the same Gradle, so you do not have to install Gradle yourself. local.properties, the seventh file, holds only the path to the Android SDK on your machine.

The Project Structure Dialog

Everything above can be edited as text, but Android Studio also offers a form view of the same settings: File > Project Structure..., Ctrl+Alt+Shift+S. It has three groups of pages in a list on the left, with Suggestions below them (a number badge on it showed 2 items in the project checked):

GroupPageWhat you see
1ProjectAndroid Gradle Plugin Version, shown as $versions.agp : 9.5.1 (it reads the catalog entry, not a loose number), with a note to use the AGP Upgrade Assistant to change it, and Gradle Version, here 9.6.0.
1SDK LocationThe Android SDK location with a folder button. The text says the location is used for new projects and for existing ones with no local.properties containing an sdk.dir line.
1VariablesA tree of variables by source: the build script, the Default version catalog: libs (libs.versions.toml) with every [versions] entry and its value, the project, and :app. A +New catalog version row adds one.
2ModulesThe app module with tabs Properties, Default Config and Signing Configs. Properties lists Compile Sdk Version (37), Build Tools Version (blank), NDK Version (blank), Source and Target Compatibility ($JavaVersion.VERSION_11 : 11 (Java 11)), Enable View Binding (blank) and two "Retain information about dependencies" options.
2DependenciesDeclared Dependencies for <All Modules> or app, each with its Configuration (implementation, androidTestImplementation and so on), a Details pane showing the group, artifact and requested version (/*not specified*/ for BOM-supplied ones), and on the right Resolved Dependencies grouped by variant: debug, debugAndroidTest, debugUnitTest, release.
2Build VariantsTabs Build Types (release and debug) and Flavors; for a build type, fields such as Application ID Suffix, Version Name Suffix, Debuggable, Signing Config, Minify Enabled, ProGuard Files, Manifest Placeholders, Multi Dex Enabled and Matching Fallbacks.
3SuggestionsNot photographed: the badge showed that two suggestions were waiting.
One thing that does not match
The Build Variants page showed Minify Enabled: false for the release build type, while the module file has optimization { enable = true } for release. The dialog's older field names (Minify Enabled, ProGuard Files, Renderscript) look like the pre-9 DSL, so I suspect it does not yet reflect the new optimization block. I have not confirmed that. When the dialog and the file disagree, believe the file, and be careful about editing a setting in the dialog that your build file now sets in a different way.

The Run Configuration

In the main toolbar (Chapter 1) are a device selector (on the install checked, Medium Phone API 37.0), a run configuration drop-down reading app, and the green Run and Debug buttons. A run configuration is a saved recipe for starting something: which module, how to install it, which screen to launch. Open the drop-down and look for Edit Configurations... (I did not photograph that menu; the dialog it opens is titled Run/Debug Configurations) to see them. The dialog has a tree on the left with buttons to add, remove, copy, make a folder and sort, and an Edit configuration templates... link. The project checked had an Android App group containing four entries: androidTest, app, main and unitTest.

The app configuration, the one Run uses, showed:

FieldValue seen
Nameapp, with Allow multiple instances ticked and Store as project file unticked
TabsGeneral, Miscellaneous, Debugger
Modulethe app's module
DeployDefault APK
Install options (all unticked)Install for all users (if already installed, will only update for existing users); Always install with package manager (disables deploy optimizations on Android 11 and later); Clear app storage before deployment
Install Flagsempty: Options to 'pm install' command
LaunchDefault Activity
Launch Flagsempty: Options to 'am start' command

A Before launch section sits below, listing steps to do first (normally a Gradle build). The defaults are right for almost every project: you change a run configuration when you need to launch a different screen, clear stored data on each run or pass flags to the installer.

A broken configuration
The unitTest entry showed a red error marker and the message Error: Module not specified: its Module field was empty. It was listed under Android App, not a test group, so it was probably created by accident. A run configuration like that shows up in the toolbar drop-down and does nothing useful: delete it with the minus button, or select it and set a module. How it was created is not known.

Try It Yourself

Exercise 1

In a project (the scratch project from Chapter 3 will do) open each of the seven files in Gradle Scripts and write down its name, the label in brackets and its job in your own words. Then open libs.versions.toml and say how many tables it has, which libraries have no version, and why.

๐Ÿ“„ View solution
Exercise 2

Open Project Structure (Ctrl+Alt+Shift+S). From the Project page read the AGP and Gradle versions; from Variables read the value of composeBom; from Dependencies pick one dependency and read its configuration. Then find the same three facts in the files. Change nothing, and press Cancel.

๐Ÿ“„ View solution
Exercise 3

Add a dependency the catalog way: put a new library line in libs.versions.toml, use it in the module file, sync, and find it on the Dependencies page. Then remove it again and sync. Use the Compose extended icons library (a good candidate because the Compose BOM supplies its version).

๐Ÿ“„ View solution
Not Yet Verified
Checked on the install (Android Studio Rabbit 2, files read from disk on 2026-11-21, screenshots on 2026-11-20 and 2026-11-21): all file contents and version numbers shown; the Gradle Scripts group and its labels; the Project Structure pages Project, SDK Location, Variables, Modules, Dependencies and Build Variants; the Run/Debug Configurations dialog for app; the unitTest error; the device selector and run configuration in the toolbar.
Checked in the docs (one web search): Google's Android Gradle plugin 9.0 release notes say Kotlin support is built in and on by default so the Kotlin Android plugin is no longer applied, that AGP 9.0 brings Kotlin Gradle plugin 2.2.10 as a runtime dependency, that android.builtInKotlin=false opts out, and that the Kotlin Android plugin is not compatible with the new DSL. I read search summaries, not the full pages, and the search said nothing about version catalogs.
Not verified: that the compileSdk { ... } and optimization { ... } forms are the new DSL (my inference); why the Build Variants page shows Minify Enabled false; the Suggestions page; what the two Suggestions are; the Debugger and Miscellaneous tabs of the run configuration; the main and androidTest configurations; how the unitTest configuration was created; the Compose extended icons coordinates in the exercise (general knowledge); the exercises (not run by me); and the IDE's own memory setting, which is separate from org.gradle.jvmargs and not covered. Versions in these files change with every Android Studio release, so trust your own project over the numbers here.
Sources: Android Gradle plugin 9.0 release notes (developer.android.com).
Coming up
Chapter 5 turns to Compose Preview and the Compose-first editor: @Preview, the preview pane, interactive and on-device previews, and what replaced the Layout Editor.