Gradle subprojects won't compile with dependency on other subproject
Asked Answered
U

2

6

I have a Multi project gradle project with the following structure.

Project A: The top level project that all sub projects reside in.
  SubProject B: 
  SubProject C:
  SubProject D:
  SubProject E:
  SubProject Shared: Every 'SubProject' has a dependency to this project "compile project(':Shared')"

Everything works correctly when running Gradle tasks from the top level 'Project A'

However, when I try to run an individual task on a subproject such as 'SubProject C' I get the following error.

FAILURE: Build failed with an exception

What went wrong:

A problem occurred evaluating root project 'SubProject C'.

Project with path ':Shared' could not be found in root project 'SubProject C'.

I think I see the problem here, Gradle thinks my sub project is a root project? However I do not know how to resolve this.

My top level settings.gradle file looks like this

rootProject.name = 'Project A'

include 'SubProject B'
include 'SubProject C'
include 'SubProject D'
include 'SubProject E'
include 'SubProject Shared'

This is my build.gradle file for 'SubProject C'

dependencies {
    compile project(':Shared')
}

Bonus question... Do the sub projects need a settings.gradle file with the 'rootProject.name' set as the sub projects name?

Urania answered 8/8, 2016 at 2:28 Comment(3)
how are you running individual task on your sub-project? Right way to do it is gradle :project:taskDotson
@RaviVasamsetty Right now i'm using the eclipse gradle plugin to run them.Urania
I am using buildship gradle plugin and it is not having any issue running subproject task. Which plugin are you using?Dotson
T
2

I think the issue is with your settings.gradle file.

Replace the your settings.gradle file with below code.

rootProject.name = 'Project A'

include 'SubProject B',':SubProject C',':SubProject D',':SubProject E'
include ':SubProject Shared'

Try running the build script of 'SubProjectC'. Hope it will work.

And answer for your second question.

We don't need seperate settings.gradle for each subprojects.
Timbal answered 9/8, 2016 at 14:3 Comment(1)
This did not work for me, using Gradle 5.3 :-( BTW docs.gradle.org/current/userguide/multi_project_builds.html does not use include ":..."?!Dictatorship
H
5

In my case the problem was that my subproject had settings.gradle with

rootProject.name = 'MySubProject'

removing the file fixed it. (It seems you must not have the file at all, it is not enough to remove the property.)

Heroic answered 28/5, 2019 at 11:4 Comment(1)
By far, this is the smoothest and easiest answer found after many many searches. Thank youArtisan
T
2

I think the issue is with your settings.gradle file.

Replace the your settings.gradle file with below code.

rootProject.name = 'Project A'

include 'SubProject B',':SubProject C',':SubProject D',':SubProject E'
include ':SubProject Shared'

Try running the build script of 'SubProjectC'. Hope it will work.

And answer for your second question.

We don't need seperate settings.gradle for each subprojects.
Timbal answered 9/8, 2016 at 14:3 Comment(1)
This did not work for me, using Gradle 5.3 :-( BTW docs.gradle.org/current/userguide/multi_project_builds.html does not use include ":..."?!Dictatorship

© 2022 - 2025 — McMap. All rights reserved.