Reference another project

In my workspace, I have 2 projects created.

MyWorkspace

  • Project1 (contains source files, build.gradle, settings.gradle)
  • Project2 (contains source files, build.gradle, settings.gradle)

Within the build.gradle in Project1, I want to reference Project2. I have tried many different things but I still get errors when building Project1 because it doesn’t recognize the classes I use from Project2 in Project1.
Currently, I declared the project in settings.gradle in Project1 as such:
include ‘:Project2’
project(‘:Project2’).projectDir = file(‘…/Project2’)

I’m not sure if I have that correct (such as the file path)
Within the build.gradle in Project1, I also added:
compile project(‘:Project2’)

Any help or guidance would be great. Thanks in advance.

You should never include one project in multiple builds.
That is unreliable unpredictable and usually just causes major problems.
You should instead use a composite build where you include the complete Project2 build in the Project1 build as subbuild instead of subproject.
The dependency would then be using the artifact coordinates, not a project(...) one.

I’ve tried looking into using composite builds but still can’t seem to get it to work.
Is there any further help you could give on how I should set this up? Thanks in advance.

What problem do you have specifically?
It’s really not much more up to it than using includeBuild and using the coordinates for the dependency.
And here is the docs about composite builds: Composing builds

ProjectA
|–build.gradle
|–settings.gradle

ProjectB
|–settings.gradle
|–build.gradle

Within ProjectA settings.gradle, I include the following:

rootProject.name = 'ProjectA'
includeBuild('../ProjectB')

Within the build.gradle file, I tried to include the following:

implementation "com.myproject:ProjectB"

However, when I try to build the project A, it produces a lot of errors because it can’t find the objects from ProjectB.
Any further help/guidance would be great. Thanks in advance.

You don’t show any errors and unfortunately my crystal ball is at the repair shop.
Can you maybe provide an MCVE showing your problem?