Compile gradle project with another project as a dependency

12 07 2014

imagesHaving 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:

  |    |--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:


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.

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!




27 responses

4 09 2014

been looking for this for a long time!!

30 10 2014

Awesome ! Thank you :)

17 12 2014

Super helpful, thank you!

19 02 2015

Thank you very much for this article. In fact, it was quite easy to find the line defining the project directory in the gradle documentation, but … I was stuck because I was trying to include the project *after* defining the project directory (it seemed logical to me). Including the project first and then defining its directory did the trick, thank you.

2 03 2015

Thank you! This works for one project depending on another. But when I use it on three project transitive dependency, the build was a failure on the secondary dependency. Is there a way to this?

2 03 2015
Jacek Milewski

I did not use any – try to google the error you get. Also you can try to build each project independently – firs the one the most nested, then the one that uses it and then the one that uses the second one. Just to make sure that all dependencies compile well

9 03 2015

thanks for this, but it’s depressing that gradle doesn’t do this automatically like maven eclipse:eclipse does. if you add a bitcoin or changetip donation link i’ll use it.

9 03 2015
Jacek Milewski

I use PayPal as a donation service. Find it on a sidebar.

Gradle projects should be organized in a tree structure. Maybe then dependency tangling is easier

20 04 2015

Thanks! It help me a lot, greetings from Argentina.

10 05 2015

Is is possible to do that from gradle init script?

29 07 2015

Thanks!! worked for me in eclipse.

28 08 2015

Thanks. Been looking for this for ages.

10 02 2016

Thanks man! It was very helpful I was having some trouble trying to configure my dependencies around here.

8 04 2016

I got option 2 to work, but there’s a slight inconvenience. When i run a task (for example ‘gradle build’) in Project then it runs the same task in Dependency as well. Is there a way to stop that (exclude Dependency subproject task from being executed when executing the same task in Project)?

13 04 2016
Jacek Milewski

you can try this flag:
-a, –no-rebuild Do not rebuild project dependencies.

26 07 2016
Abdul Basit Iqbal

if i have a 3rd party dependency on my dependent project
i.e compile(‘’)

dependency version get changed in Root project
i.e it gets changed to

Is there any way to fix it ?

9 05 2017

compile (project(‘:Dependency’)) {
exclude group: ‘’

1 12 2016

The snowflakes is so annoying, and make reading very difficult. (Eyes keep focusing on moving objects)

1 12 2016
Jacek Milewski

Right, I removed it :)

18 01 2017
Enrique Arizón Benito

Great post!

P.S.: I would like to make a micro-donation, but I see you don’t have a public bitcoin account :( I think it’s the easier way for micro-payments. No need to sign or give a credit-card or whatever.

22 02 2017

This was really very helpful.

10 03 2017

Consider using a composite build (new to Gradle 3.1) for scenario 2.

28 08 2017
abdelilah el ghazal

Thank you !!!!!!

27 10 2017
CocoaPods like solution for Android Studio - ExceptionsHub

[…] you can refer the two post here and here for add dependencies from the App external […]

20 11 2017
Ban Ăn Chơi

Nice tips

8 12 2017
Raveesh Sharma

Thanks a lot .. Needed this badly

2 03 2018
Madhu Sudan Aggarwal

Thanks, It was very helpful.

Is there a way to run “gradle install” command from eclipse (I don’t see it as an option there)

I want to save the jar in my mavenLocal repo

Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: