1. 程式人生 > >gradle一個專案引用另一個專案的解決方法

gradle一個專案引用另一個專案的解決方法

Compile gradle project with another project as a dependency

Having project dependant on another project is common situation. How to configure gradle so that it will include your dependency project in build process?

There are two cases:

1. Your project is a root project and dependency is under its root

When dependand project is under root in a directory structure (dependency is not being shared with any other project except this one) you are doing it in recommended gradle strategy :)

To wrap up: you have directory structure like this:

Project
  |--build.gradle
  |--settings.gradle
  |--Dependency
  |    |--build.gradle

Then to add Dependency to Project, you need to have Project/settings.gradle content like this:

include ':Dependency'

and in a Project/build.gradle dependencies section you need to compile the dependent project by adding:

dependencies {
   compile project(':Dependency')
}

2. You have two independent projects and you need to use one of them as a dependency

When both project are on the same level in a directory structure you are not doing it as gradle team wish, but it is more real world situation in my opinion :) This is because you have your library project that can be easily used in several other projects as a dependency.

So you have directory structure like this:

Project
  |--build.gradle
  |--settings.gradle
Dependency
  |--build.gradle

To add Dependency to the Project, you need to include it, and show Dependency path manually. So the Project/settings.gradle content should be like this:

include ':Dependency'
project(':Dependency').projectDir = new File(settingsDir, '../Dependency')

and in a Project/build.gradle dependencies section you need to compile the dependent project by adding:

dependencies {
   compile project(':Dependency')
}

Notice that tis is build.gradle is exactly the same as in previous section.