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?

Community
  • 1
  • 1
Nick H
  • 8,897
  • 9
  • 41
  • 64

2 Answers2

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

Jakub Holý
  • 6,019
  • 3
  • 33
  • 33
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.
Jince Martin
  • 301
  • 1
  • 9
  • 18
  • 1
    This did not work for me, using Gradle 5.3 :-( BTW https://docs.gradle.org/current/userguide/multi_project_builds.html does not use `include ":..."`?! – Jakub Holý May 28 '19 at 10:40