Add local project dependency

I have two local gradle projects, ProjectA and ProjectB. ProjectA is a multi module project which has modules Module1 and Module2. Module2 depends on Module1.

root/
  ProjectA/
    Module1/build.gradle
    Module2/build.gradle
    settings.gradle
    build.gradle
  ProjectB/
    settings.gradle
    build.gradle

I want to make ProjectB depend on Module2. I attempted to do this with the follow changes to ProjectB/settings.gradle:

include 'Module2'

project(':Module2').projectDir = file('../ProjectA/Module2')

and to ProjectB/build.gradle:

dependencies {
    compile project(":Module2")
}

This results in the following error on build:

FAILURE: Build failed with an exception.

* Where:
Build file 'root\ProjectA\Module2\build.gradle' line: 2

* What went wrong:
A problem occurred evaluating project ':Module2'.
> Project with path ':Module1' could not be found in project ':Module2'.

I attempted to also include and depend on Module1 in ProjectB, however this resulted in properties from ProjectA/build.gradle allProjects not being resolved. I assume this is related to the root of the problem, but I am unsure of how to continue debugging.

Can I achieve this behavior?

Need to registe module 1 in settings.gradle of Project B…

include 'Module1'

project(':Module1').projectDir = file('../ProjectA/Module1')

you can build project A with

./gradlew :projectA:build

build B with

./gradlew :projectB:build

Hi, as I said, the suggested changes to ProjectB/settings.gradle do not work. Specficially, they result in the following error:

* Where:
Build file 'root\ProjectA\Module2\build.gradle' line: 2

* What went wrong:
A problem occurred evaluating project ':Module2'.
> Could not find method compile() for arguments [project ':Module1'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

I speculated that this is because the apply plugin: 'java' from ProjectA/build.gradle allProjects is not being applied.

Any other ideas regarding this issue? I feel I am missing something obvious.