Gradle cannot find sourceSets in a multi-project build
Could not find property 'sourceSets' on root project 'my-app'.Short answer
sourceSets is not part of every Gradle project. The Java plugin adds it, and an Android module has its own android.sourceSets. If a build script reads sourceSets in a project where that plugin is not applied, Gradle reports the property as missing. Apply the plugin in that project, or move the code into the project that applies it.
Fix it fast
1Find out which cause you have
Open the build file of the project named in the error and look for the plugin that should provide sourceSets. If neither the Java plugin nor an Android plugin is applied there, that project has no sourceSets.
grep -n -E "java|com.android" build.gradle build.gradle.kts */build.gradle */build.gradle.kts2Match what you see, then jump to its fix
- 1
The error names the root project, or a module that has no plugins { id 'java' } lineThe Java plugin is not applied in that projectFix - 2
sourceSets is used inside subprojects { } or allprojects { }, and one module in the build is not a Java moduleA subprojects {} block in the root build file reaches modules without the pluginFix - 3
The failing project applies com.android.application or com.android.libraryAn Android module reads sourceSets at the top levelFix
- 1
What does the error look like?
Could not find property 'sourceSets' on root project 'my-app'.Build script error: sourceSets works in one module but not in another
Cause 1 of 3
The Java plugin is not applied in that project
1Check
The Java plugin adds a read-only sourceSets property with the main and test source sets. A root project that only holds shared settings usually applies no plugin, so the property does not exist there.
grep -n -E "id\(?.java|java-library|plugins" build.gradle build.gradle.kts */build.gradle */build.gradle.kts2Fix
Apply the Java plugin in the project that uses sourceSets, then configure the source set there.
// build.gradle (Groovy)
plugins {
id 'java'
}
sourceSets {
main {
java {
srcDirs = ['src/main/java', 'src/generated/java']
}
}
}3Verify
Run a build of that project.
./gradlew :core:compileJavaConfirmed by docs.gradle.org
Cause 2 of 3
A subprojects {} block in the root build file reaches modules without the plugin
1Check
Gradle calls configuring subprojects from the root build script an improper way to share build logic: the code lands in modules that may not have the plugin, and it is hard to see from the module's own script.
grep -n -E "subprojects|allprojects" build.gradle build.gradle.kts2Fix
Move the shared configuration into a convention plugin (for example a java-conventions plugin) and apply that plugin in each Java module.
// module build.gradle.kts
plugins {
id("java-conventions")
}3Verify
Build the whole project to see that every module configures cleanly.
./gradlew buildConfirmed by docs.gradle.org
Cause 3 of 3
An Android module reads sourceSets at the top level
1Check
In an Android module the Android Gradle plugin owns the source sets, inside the android block. A top-level sourceSets reference is looking for the Java plugin's property instead.
grep -n -E "com.android.(application|library)" */build.gradle */build.gradle.kts2Fix
Configure Android source sets inside android { sourceSets { ... } }. For extra Kotlin directories with built-in Kotlin in AGP 9, the documented place is the kotlin set of the Android source set.
// build.gradle.kts of an Android module
android {
sourceSets.named("main") {
kotlin.directories += "src/extra/kotlin"
}
}3Verify
Sync the project and build the module.
./gradlew :app:assembleDebugConfirmed by developer.android.com
How do I stop it coming back?
- Configure source sets in the module's own build file, next to the plugin that provides them.
- Use convention plugins instead of a big subprojects block in the root build file.
- Keep pure aggregation projects (the root, a bom module) free of Java-specific settings.
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:
Related fixes
Mobile apps
Android app crashes because the Apache HTTP classes are missing
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
From Android 9 the Apache HTTP client is no longer on the default classpath, so code or a library that still uses org.apache.http fails at runtime.
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
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
Gradle cannot apply the Android application or library plugin
Failed to apply plugin 'com.android.internal.application'.
Gradle loaded the Android Gradle plugin but could not run it.
2 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