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

Peters_ picture Peters_ · Feb 24, 2016 · Viewed 11.8k times · Source

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!

Answer

Gabriele Mariotti picture Gabriele Mariotti · Feb 24, 2016

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')
}