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.
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:
| Name | What it is |
|---|---|
| Gradle | The 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. |
| Sync | Android 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:
| File | Label shown | Its 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. |
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:
Read it in three steps:
[versions]gives each version a name. Change a number here and every library or plugin that refers to it follows.[libraries]names each library: agroupand aname(together, its Maven address) plusversion.ref, which points at a line in[versions].[plugins]does the same for Gradle plugins, using anid.
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
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)
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)
| Block or line | What it does |
|---|---|
plugins { alias(...) } | Applies the two plugins to this module. There is no kotlin-android plugin: see the next section. |
namespace | The 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, targetSdk | The 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, versionName | The 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. |
compileOptions | Java 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 / debugImplementation | The 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):
| Line | Meaning |
|---|---|
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=true | Commented 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=true | On. Gradle can skip its configuration phase when nothing that affects the build configuration has changed, which makes repeat builds faster. |
kotlin.code.style=official | Tells 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):
| Group | Page | What you see |
|---|---|---|
| 1 | Project | Android 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. |
| 1 | SDK Location | The 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. |
| 1 | Variables | A 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. |
| 2 | Modules | The 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. |
| 2 | Dependencies | Declared 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. |
| 2 | Build Variants | Tabs 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. |
| 3 | Suggestions | Not photographed: the badge showed that two suggestions were waiting. |
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:
| Field | Value seen |
|---|---|
| Name | app, with Allow multiple instances ticked and Store as project file unticked |
| Tabs | General, Miscellaneous, Debugger |
| Module | the app's module |
| Deploy | Default 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 Flags | empty: Options to 'pm install' command |
| Launch | Default Activity |
| Launch Flags | empty: 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.
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
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.
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.
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).
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).
@Preview, the preview pane, interactive and on-device previews, and what replaced the Layout Editor.