How to add a dependency to another project properly using gradle?

You should have a structure like this:

ProjectA
|--projectA1
|----build.gradle
|--projectA2
|----build.gradle
|--settings.gradle
|--build.gradle

ProjectB
|--projectB1
|----build.gradle
|--projectB2
|----build.gradle
|--settings.gradle
|--build.gradle

You can link an external module in your project.

1) In your project projectA/settings.gradle

include ':projectA1',':projectA2',':projectB1' 
project(':projectB1').projectDir = new File("/workspace/projectB/projectB1")

2) Add dependency in build.gradle of projectA1 module

dependencies {
    compile project(':projectB1')
}

Unless if projects A1 and B1 live in the same source repository and are checked-out and checked-in together, you really should depend on project B1 as an external dependency.

in Project A1 build.gradle:

dependencies{
    compile 'projectB1group:projectB1module:projectB1version'
}

Of course, for this to work, you will have had to publish B1 binaries to a repository that is accessible from Project A1 first. This can either be a external nexus/artifactory type maven repository, but can also be your local maven .m2 cache, or even a plain old file system. For maven publishing see maven or 'maven-publish` plugins.


If both projects live in the same source repo, you should organize ProjectA and ProjectB as subprojects under a root "container" project. The root project does not need to have source code of its own.

Read about organizing multi-project builds in gradle here.

If the root project has a settings.gradle with include lines that includes project B1, you can refer to any project under the root project like this:

project(':B1')

so, to add B1 as a dependency to A1, in A1's build.gradle:

compile project('B1')