Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4133 pages
Flutter and app store releases

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 show

Fix it fast

Most common cause, 1 of 3

Extra folders are added through kotlin.sourceSets

Move the folder into android.sourceSets and use the kotlin set's directories.

Fix
// app/build.gradle.kts
android.sourceSets.named("main") {
    kotlin.directories += "additionalSourceDirectory/kotlin"
}

// Groovy: kotlin.directories.add("additionalSourceDirectory/kotlin")
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Android Gradle Plugin 9.x, Gradle 9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Find every place the build adds source folders. The kind of block decides which fix below applies.

Run this first
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:

  1. 1Extra folders are added through kotlin.sourceSetsA block like kotlin.sourceSets.named("main") { kotlin.srcDir(...) } in a module that applies an Android plugin
  2. 2Kotlin folders are listed under the java setandroid.sourceSets main { java.srcDir("src/main/kotlin-extra") } or java.directories pointing at .kt files
  3. 3The folder is generated by a task or only belongs to one variantThe 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 Kotlin
  • Sync 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.

Check
grep -rn "kotlin.sourceSets" --include=*.gradle* .

Fix

Move the folder into android.sourceSets and use the kotlin set's directories.

Fix
// 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.

Verify
./gradlew :app:compileDebugKotlin

Confirmed by developer.android.com

Did this fix it?

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.

Check
grep -rnE "java\.(srcDir|directories)" --include=*.gradle* .

Fix

Point the folder at the kotlin set. Keep Java-only folders in the java set.

Fix
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.

Verify
./gradlew clean :app:assembleDebug

Confirmed by developer.android.com

Did this fix it?

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.

Check
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.

Fix
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.

Verify
./gradlew :app:assembleRelease

Confirmed by developer.android.com

Did this fix it?

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:

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.