19

Hello I am new to gradle and it is a little bit confusing for me. How should I add a dependency in my gradle configuration to have access to B1.java in projectA1? Project B is gradle project and project A is just a folder with another gradle projects.

Here is my structure:

  1. Workspace:
    • ProjectA
      • projectA1
        • ...
        • here I want to have access to B1.java
        • build.gradle
      • projectA2
        • ...
        • build.gradle
    • ProjectB
      • projectB1
        • B1.java
        • ...
        • build.gradle
      • projectB2
        • ...
        • build.gradle
      • build.gradle

I tried to read gradle documentation, but it is not clear for me. Any help appreciated. Thanks!

Peters_
  • 597
  • 2
  • 8
  • 28
  • 2
    Take a look at [this](https://looksok.wordpress.com/2014/07/12/compile-gradle-project-with-another-project-as-a-dependency/) post. – António Ribeiro Feb 24 '16 at 16:04

3 Answers3

22

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')
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • I followed the above approach, the exception was that projectB1 had a dependency on projectB2, something like ```evaluationDependsOn(':projectB2')``` and thus in my projectA1 if I include projectB1, it throws an error like :projectB2 does not exist. Is there a smart way to fix this other than adding projectB2 also as a subproject in ProjectA? – Kumar Shubham Sep 16 '19 at 10:44
  • If you are using `evaluationDependsOn(':projectB2')`, you have to declare `:projectB2` otherwise gradle doesn't know what is projectB2. – Gabriele Mariotti Sep 16 '19 at 10:49
  • But lets say if ProjectB1 depends on other, say 5 subprojects in ProjectB. Then in that case, I will have to declare all the subprojects in projectA right? – Kumar Shubham Sep 16 '19 at 10:50
  • @KumarShubham yes, the alternative is to change some gradle script if it is possible and try to centralize some tasks in order to reuse them. – Gabriele Mariotti Sep 16 '19 at 10:52
  • 2
    Exactly what I needed! However, two things had to be changed for me to make this work: `implementation` instead of `compile` for `dependencies{}` (suggested by Android Studio) and `.setProjectDir()` in `settings.gradle`. – sebkraemer Aug 13 '21 at 13:59
  • My `ProjectA` doesn't recognize `ProjectB.buildSrc` for some reason, do I need to add it explicitly somehow? – Phil Dukhov Oct 22 '21 at 12:40
  • I'll add that this answer is coded in groovy, you'll need to add parenthesis around the arguments to `include` and `implementation`. Additionally, you can be more sure of your file directory by calling `File(settingsDir, "path/from/settings/file")` – Keverly Nov 14 '22 at 23:52
5

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')
RaGe
  • 22,696
  • 11
  • 72
  • 104
2
  1. Edit setting.gradle in root path and include your sub projects, and then subproject will be able to see and depend on each other.
root
 |-- build.gradle << very simple bulid.grade , no special thing.
 |-- setting.gradle <<
rootProject.name = 'root'

file("${rootDir}/ProjectA").eachDirMatch(~/.*/) {
    include "ProjectA:${it.name}"
}

file("${rootDir}/ProjectB").eachDirMatch(~/.*/) {
    include "ProjectB:${it.name}"
}
  1. build.gradle in project
root
 |-- ProjectA
        |-- projectA1
            |-- build.gradle  <<
implementation project(':ProjectB:projectB1')

Ref:

Jess Chen
  • 3,136
  • 1
  • 26
  • 35
  • 1
    point 2: it should be 'build.gradle' and not 'build.setting'. Also could you please explain what does it include "orm:${it.name}" mean and for project B why its web? – mux032 Dec 14 '20 at 18:58