Wednesday, April 23, 2014

Gradle Android : Multiple project setup

For a Multi-project setup, the Android documentation for gradle states :
"A multi-project setup usually works by having all the projects as sub folders of a given root project."

MyProject/
 + app/
 + libraries/
    + lib1/

But I have a different situation where my library project and the actual project is not under the same root.

So, my library project is inside its own directory. Then I have my main project in some other directory wanting to refer to this library project. I had a hard time to figure this out with gradle. So, hopefully this helps someone.

So basically, the structure is as follows:

~/Dev/Libraries
    |---- foolib-root
                |---- foolib
                           |---- build.gradle
                           |---- src
                           |---- libs
                |---- build.gradle
                |---- settings.gradle


~/Dev/Projects
    |---- project1-root
                |---- project1
                           |---- build.gradle
                           |---- src
                           |---- libs
                |---- build.gradle
                |---- settings.gradle


Just, FYI, the build.gradle inside foolib-root/foolib contains:

apply plugin: 'android-library'

indicating that it is a library module.

Now, the task is to refer to library project foolib from project1.

I have found there are 2 things you need to do to achieve this:

1. The settings.gradle in project1-root must define the library project in its scope and provide a path for it. You can do this by having the following line in it:

 project(':foolib').projectDir = new File(settingsDir, '../Libraries/foolib-root/foolib')

2. Add this library project into the dependencies of project1 by adding the following to its build.gradle:

dependencies {
    compile project(':foolib')
    ......
}

This did the trick for me. Not sure if this is a right way to do it. But serves my purpose.