47

is it possible to specify a dependency in Gradle (in android studio) to another gradle project outside of the current project boundaries? For example with a relative path something like this:

dependencies {
  compile project('../../stdlib/dagger')
}

So what I trie is something like this:

I have an Android Application. The structure looks like this:

  • MyApp (path is /Users/foo/workspace/MyApp)
    • app (path is /Users/foo/workspace/MyApp/app)

And I have a gradle android library project containing 3 submodules:

  • stdlib (path is /Users/foo/workspace/stdlib)
    • dagger (path is /Users/foo/workspace/stdlib/dagger)
    • utils (path is /Users/foo/workspace/stdlib/utils)
    • http (path is /Users/foo/workspace/stdlib/http)

What I want is to compile the dagger, utils, http module into MyApp project.

The stdlib libraries modules are under heavy development and will grow as MyApp grow. Hence I do not want to push them into a maven repository everytime I make a little change.

So is there a possibility to link other gradle projects somehow? Im looking for a temporarly solution. I will push the std library into maven repository once the source is stable.

Also, as workaround, a solution with sourceSet would be possible. I have also considered to make a libraries folder in MyApp who is a symlink to stdlib, but I didnt get it to work as expected:

  • MyApp (path is /Users/foo/workspace/MyApp)
    • app (path is /Users/foo/workspace/MyApp/app)
    • libraries (symlink to /Users/foo/workspace/stdlib)
 dependencies {
      compile project(':libraries:dagger')
    }

Any idea how to solve such a dependency in gradle?

1 Answer 1

104

You can include an outside root project module using 'settings.gradle' file from your main project. It must to be a gradle project too and in the specific Android building situation, you must configure every module as an "android-library" plugin project.

For example, in 'MyApp' project settings.gradle you can try this:

include 'app'
include 'dagger'
project(':dagger').projectDir = new File('/Users/foo/workspace/stdlib/dagger')

Your 'MyApp' build.gradle must reflect the need of the 'dagger' module in a relative path Gradle way:

dependencies {
  compile project(':dagger')
}

And that's it. Repeat this step with every external module you need and you'll have a proper Gradle multi-project configuration.

5
  • when I try this I get a A problem occurred evaluating project ':account'. > Cannot get property 'compileSdkVersion' on extra properties extension as it does not exist account is my outside project
    – T_C
    Jun 24, 2016 at 0:04
  • 7
    it works perfect, but i am wondering why include 'dagger'doesnot have a colon, while project(':dagger').projectDir has a colon?
    – howerknea
    Jul 11, 2016 at 6:32
  • @howerknea It is because in build.gradle you are using an specific Gradle Project API which has this different sintaxis method call than the constructor of the"Settings" object. The "Settings" object is used to find and execute every Project API config: It can be one or many. So, don't bother too much about it, it's just a sintaxis differentiation between Gradle Settings object and Gradle Project API interface. Oct 7, 2016 at 13:42
  • Dead thread but with reference to a part of the answer that Martin gave, >It must to be a gradle project too and in the specific Android building situation, you must configure every module as an "android-library" plugin project What does that mean? Do I need to add "com.android-library" in the build gradle for the referenced project?
    – pingOfDoom
    Feb 1, 2019 at 21:26
  • Hi @pingOfDoom! Yes, in the module's build.gradle you need to specify "com.android.library". Notice the point instead the dash, because recently I had problems trying to compile with the dash. Feb 3, 2019 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.