ext {} and ext properties fail in build.gradle.kts
ext in buildscript cannot be recognised by Gradle Kotlin DSLShort answer
ext is the Groovy way to hold extra properties. In build.gradle.kts the same data lives behind the extra accessor, a map you read and write with extra["name"]. Subprojects reach the root's values through rootProject.extra. For shared versions, a version catalog replaces ext altogether.
Fix it fast
1Find out which cause you have
Check whether the failing file is a .kts script. If it is, any ext { } block or bare ext variable is Groovy syntax and must be rewritten.
grep -rn "ext" --include=*.gradle.kts .2Match what you see, then jump to its fix
- 1
ext { kotlin_version = '1.9.0' } or ext.kotlin_version is red in build.gradle.ktsGroovy ext syntax copied into a .kts fileFix - 2
The root script works, but app/build.gradle.kts cannot find the propertyA subproject reads the root project's value without rootProjectFix - 3
You use ext only to share library or plugin versions between filesShared versions are better kept in a version catalogFix
- 1
What does the error look like?
ext in buildscript cannot be recognised by Gradle Kotlin DSLThe ext { } block is marked as an error in build.gradle.kts
Cause 1 of 3
Groovy ext syntax copied into a .kts file
1Check
Extra properties in the Kotlin DSL come from the extra accessor, a map. The Groovy ext block has no counterpart.
grep -rn "ext\b" --include=*.gradle.kts .2Fix
Set the value with extra and read it with a cast.
// root build.gradle.kts
extra["myVersion"] = "1.0.0"
val myVersion = extra["myVersion"] as String3Verify
Sync or run any task and confirm the script compiles.
./gradlew helpConfirmed by docs.gradle.org
Cause 2 of 3
A subproject reads the root project's value without rootProject
1Check
extra belongs to the project whose script uses it. A subproject must ask the root project for it.
grep -rn "extra\[" --include=*.gradle.kts .2Fix
Read the value through rootProject.extra in the subproject script.
// app/build.gradle.kts
val myVersion = rootProject.extra["myVersion"] as String3Verify
Run a task on the subproject.
./gradlew :app:helpConfirmed by docs.gradle.org
Cause 3 of 3
Shared versions are better kept in a version catalog
1Check
A catalog file gradle/libs.versions.toml is imported automatically as libs, so no ext is needed.
ls gradle/libs.versions.toml2Fix
Declare the version once in the catalog and read it in Kotlin DSL scripts.
# gradle/libs.versions.toml
[versions]
groovy = "3.0.5"
// build.gradle.kts
val groovyVersion = libs.versions.groovy.get()3Verify
Build with the catalog entry in place.
./gradlew helpConfirmed by docs.gradle.org
How do I stop it coming back?
- Prefer a version catalog over extra properties for dependency versions.
- When converting a Groovy script, rewrite ext blocks and single quotes in the same pass.
- Keep the values in one root file and read them elsewhere through rootProject.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Nuclear Home VideoA video-streaming Android app built from scratch, engineered for smooth playback.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Mobile apps
Android build fails because a product flavor has no flavor dimension
The flavor 'flavor_name' is not assigned to a flavor dimension.
Android Gradle plugin refuses to build when a product flavor is not assigned to a flavor dimension.
2 causes, each with a fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
Android build fails because two libraries ship the same META-INF file
More than one file was found with OS independent path
Two dependencies put a file at the same path inside the APK and the plugin will not guess which one wins.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fix