AGP 9 rejects extra Kotlin source folders added through kotlin.sourceSets
Your error
Using kotlin.sourceSets DSL to add Kotlin sources is not allowed with built-in Kotlin1 more line it can showFix it fast
Most common cause, 1 of 3Extra folders are added through kotlin.sourceSets
Move the folder into android.sourceSets and use the kotlin set's directories.
// app/build.gradle.kts
android.sourceSets.named("main") {
kotlin.directories += "additionalSourceDirectory/kotlin"
}
// Groovy: kotlin.directories.add("additionalSourceDirectory/kotlin")Short answer
Android Gradle plugin 9 compiles Kotlin itself (built-in Kotlin) and accepts extra Kotlin source folders in one place only: the kotlin set of android.sourceSets. Folders added with kotlin.sourceSets, or put into the java set, are refused. Move each folder to android.sourceSets main kotlin.directories, and register generated folders with the variant API's addGeneratedSourceDirectory.
Which cause do you have?
Find every place the build adds source folders. The kind of block decides which fix below applies.
grep -rnE "kotlin\.sourceSets|sourceSets *\{|srcDir|directories" --include=*.gradle --include=*.gradle.kts .Then find the line you see in this list and open its fix:
- 1Extra folders are added through kotlin.sourceSets
A block like kotlin.sourceSets.named("main") { kotlin.srcDir(...) } in a module that applies an Android plugin - 2Kotlin folders are listed under the java set
android.sourceSets main { java.srcDir("src/main/kotlin-extra") } or java.directories pointing at .kt files - 3The folder is generated by a task or only belongs to one variant
The source path is under build/ or depends on the build type or flavor
What does the error look like?
Using kotlin.sourceSets DSL to add Kotlin sources is not allowed with built-in KotlinSync fails at a kotlin.sourceSets block right after upgrading to AGP 9.0
Cause 1 of 3
Extra folders are added through kotlin.sourceSets
You seeA block like kotlin.sourceSets.named("main") { kotlin.srcDir(...) } in a module that applies an Android plugin
Check
Android's built-in Kotlin migration guide says that with built-in Kotlin the only supported way is the AndroidSourceSet kotlin set, reached through android.sourceSets. The kotlin.sourceSets route belonged to the separate kotlin-android plugin.
grep -rn "kotlin.sourceSets" --include=*.gradle* .Fix
Move the folder into android.sourceSets and use the kotlin set's directories.
// app/build.gradle.kts
android.sourceSets.named("main") {
kotlin.directories += "additionalSourceDirectory/kotlin"
}
// Groovy: kotlin.directories.add("additionalSourceDirectory/kotlin")Verify
Gradle sync completes and classes from that folder resolve in the IDE.
./gradlew :app:compileDebugKotlinConfirmed by developer.android.com
Cause 2 of 3
Kotlin folders are listed under the java set
You seeandroid.sourceSets main { java.srcDir("src/main/kotlin-extra") } or java.directories pointing at .kt files
Check
Before AGP 9 a Kotlin folder could sit in the java set. The same guide marks that as unsupported with built-in Kotlin, so the folder has to move sets, not just blocks.
grep -rnE "java\.(srcDir|directories)" --include=*.gradle* .Fix
Point the folder at the kotlin set. Keep Java-only folders in the java set.
android.sourceSets.named("main") {
// java.directories += "src/main/kotlin-extra" <- remove
kotlin.directories += "src/main/kotlin-extra"
}Verify
A clean build compiles the files in that folder.
./gradlew clean :app:assembleDebugConfirmed by developer.android.com
Cause 3 of 3
The folder is generated by a task or only belongs to one variant
You seeThe source path is under build/ or depends on the build type or flavor
Check
Static sourceSets entries are the wrong tool for generated code. The guide points to the variant API for folders that a task creates or that apply to one variant.
grep -rn "build/generated" --include=*.gradle* .Fix
Register the folder inside androidComponents.onVariants: addStaticSourceDirectory for a fixed folder, addGeneratedSourceDirectory with the task provider and its output property for generated code.
androidComponents.onVariants { variant ->
variant.sources.kotlin!!.addStaticSourceDirectory("additionalSourceDirectory/kotlin")
variant.sources.kotlin!!.addGeneratedSourceDirectory(myGenTask, MyGenTask::outputDir)
}Verify
Building the variant compiles the generated files without a manual step in between.
./gradlew :app:assembleReleaseConfirmed by developer.android.com
How do I stop it coming back?
- Keep extra source folders in android.sourceSets kotlin from the start, even on AGP 8.
- Use the variant API for anything generated, never a hard-coded build/ path.
- Treat android.builtInKotlin=false as a short-term escape; the guide says it cannot be turned off in AGP 10.
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.