Maaz Zindani

Loading the index…

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

Gradle cannot find sourceSets in a multi-project build

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

  • Applies to Gradle 8.x and 9.x, Java plugin, Android Gradle plugin 8.x and 9.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.kts
  2. 2Match what you see, then jump to its fix

    1. 1The error names the root project, or a module that has no plugins { id 'java' } lineThe Java plugin is not applied in that projectFix
    2. 2sourceSets 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. 3The failing project applies com.android.application or com.android.libraryAn Android module reads sourceSets at the top levelFix

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

2Fix

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:compileJava

Confirmed by docs.gradle.org

Did this fix it?

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

2Fix

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 build

Confirmed by docs.gradle.org

Did this fix it?

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

2Fix

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:assembleDebug

Confirmed by developer.android.com

Did this fix it?

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 fix

Next step

Want this handled for good?

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