Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle subprojects won't compile with dependency on other subproject

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?

like image 960
Nick H Avatar asked Aug 08 '16 02:08

Nick H


People also ask

How do you add dependency from one project to another in Gradle?

Dependency types To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

What is subprojects in Gradle?

The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency.

What is transitive dependency in Gradle?

Transitive dependencyA variant of a component can have dependencies on other modules to work properly, so-called transitive dependencies. Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically.

How do I manage Gradle dependencies?

At runtime, Gradle will locate the declared dependencies if needed for operating a specific task. The dependencies might need to be downloaded from a remote repository, retrieved from a local directory or requires another project to be built in a multi-project setting. This process is called dependency resolution.


1 Answers

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

like image 60
Jakub Holý Avatar answered Sep 19 '22 01:09

Jakub Holý