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.

2 comments:

  1. Hi,

    Very Nice Blog.
    Thanks for sharing sunch an amazing Information here.Please continue,keep posting More updates.
    android app development course
    android course online

    android training

    android online training

    ReplyDelete
  2. Step into the world of possibilities with free local classified ads in India. Elevate your buying and selling experience effortlessly. Post free classified ads and unlock a marketplace where community meets convenience. It's your chance to seamlessly connect, trade, and discover treasures in your neighborhood. Explore, engage, and empower your local connections today.

    ReplyDelete