22

I have a complex, but interesting situation. This is a tree diagram of my folder structure:

root
|___ settings.gradle
|___ p1
|___ p2  // depends on p3/sp1
|___ p3
|____|___sp1
|____|___sp2

I hope that explains the situation.

Now how would I add sp1 as a dependency of p2?

So far in my root setting.gradle, I have

rootProject.name = root
include 'p1'
include 'p2'
include 'p3'

In p2 build.gradle, I have:

dependencies {
    compile project (':p3:sp1')
}

But doing this still does not resolve the dependencies in p2; I still get errors about missing definition.

How do I fix this?

Just an aside, how would I resolve other dependencies sp1 might have. Like if it depends on sp2, do I need to declare this somehow even though it is already resolved within p3?

smac89
  • 39,374
  • 15
  • 132
  • 179
  • Hi! Have you solve this problem? I am finding for example for resolve similar problem – iamthevoid Sep 12 '19 at 14:35
  • @iamthevoid I think the accepted answer should work. Did you try it? – smac89 Sep 12 '19 at 18:59
  • Yep, I've already did same. – iamthevoid Sep 13 '19 at 05:09
  • By the way, I have the question. By default modules of my submodule use settings.gradle, build.gradle of parent project, but not the same files of submodel root project. Is there way to force using build.gradle and settings gradle of submodel root project? – iamthevoid Sep 13 '19 at 05:11
  • 1
    @iamthevoid Gradle does not support having multiple `settings.gradle` in one gradle project (regardless of subprojects). You can use [`composite build`](https://docs.gradle.org/current/userguide/composite_builds.html) projects to accomplish this though – smac89 Sep 13 '19 at 05:15
  • @iamthevoid if your subprojects are not composite builds, then they should include their own `build.gradle`, but the parent project can configure each subproject by using the `subproject` closure – smac89 Sep 13 '19 at 05:22

2 Answers2

29

Assuming project sp1 and sp2 are subprojects of project p3, if you want to do:

dependencies {
    compile project(':p3:sp1')
}

Then you need to change your settings.gradle to:

rootProject.name = root

include ':p1'
include ':p2'
include ':p3'    // Keep this if this folder contains a build.gradle
include ':p3:sp1'
include ':p3:sp2'
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • 10
    It's a bloody shame that Gradle has not documented this more properly. I find the tutorial at https://guides.gradle.org/creating-multi-project-builds/ misleading and the official documentation at https://docs.gradle.org/current/userguide/multi_project_builds.html missing some crucial information. With Maven multi-module builds are trivial (and recursive child modules are simple); with Gradle it's most definitely not. – Frans Jan 29 '20 at 04:38
7

Just to add the answer for those who might be looking, in your settings.gradle you will have

include ':p1',':p2',':p3:sp1',':p3:sp2'

if sp1 depends on sp2

then add dependency on sp1's gradle as

dependency {
   compile project(":p3:sp2")
}
andylamax
  • 1,858
  • 1
  • 14
  • 31