14

How do i add an external library and nested external libraries to an android project?


My project structure (Not allowed to change)

  • Apps/
    • App1/
      • build.gradle
      • settings.gradle
    • libraries/
      • library1/
        • build.grade
        • settings.gradle
      • library2/
        • build.grade
        • settings.gradle
      • library3/
        • build.grade
        • settings.gradle
      • library4/
        • build.grade
        • settings.gradle

App1

App1/build.gradle

buildscript {
    ...
}

apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':..:libraries:library1')
    compile project(':..:libraries:library2')
    compile project(':..:libraries:library3')
    compile project(':..:libraries:library4')
}

android {
    ...
}

App1 does not directly depend on library3 or library4, however, it will complain if i don't include them in the dependencies in the build.gradle file and the settings.gradle file. So, i have them included just to stop it from complaining.

App1/settings.gradle

include ':'
include '..:libraries:library1'
include '..:libraries:library2'
include '..:libraries:library3'
include '..:libraries:library4'

library1

library1/build.gradle

buildscript {
    ...
}

apply plugin: 'android-library'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':..:library3')
    compile project(':..:library4')
}

android {
    ...
}

library1/settings.gradle

include ':'
include '..:library3'
include '..:library4'

library2..4

library2..4/build.gradle

buildscript {
    ...
}

apply plugin: 'android-library'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    ...
}

library2..4/settings.gradle

include ':'

When trying to gradlew clean build on App1 i get the following error:

FAILURE: Build failed with an exception.

* Where:
Build file '/home/user/projects/branches/branch1/Apps/libraries/library1/build.gradle' line: 15

* What went wrong:
A problem occurred evaluating project ':..:library:library1'.
> Project with path ':..:library3' could not be found in project ':..:library:library1'.

Line 15 is compile project(':..:library3') in the library1/build.gradle file.

How do i add an external library and nested external libraries to an android project?

prolink007
  • 33,872
  • 24
  • 117
  • 185
  • 1
    I'm curious if anyone familiar with the Settings DSL / Groovy could come up with a snippet that parses nested settings.gradles. This is particularly helpful for submodules. Currently, adding a project *anywhere* causes the build to break all the way up the chain. – James Wald Jul 30 '14 at 17:54

3 Answers3

18

In your top level settings.gradle (App1/settings.gradle) file do something like this for each library

include ':library1'   
include ':library2'   
include ':library3'   
include ':library4'   

project(':library1').projectDir = new File(rootProject.projectDir, '../libraries/library1')
project(':library2').projectDir = new File(rootProject.projectDir, '../libraries/library2')
project(':library3').projectDir = new File(rootProject.projectDir, '../libraries/library3')
project(':library4').projectDir = new File(rootProject.projectDir, '../libraries/library4')

Remove the other settings.gradle files, you don't need them

then in each build script you only need to use

compile project (':library1')
compile project (':library2')
etc....

as stated above just use a single settings.gradle file in the root project (App1).

Then from your App1 folder run gradlew clean :library1:build to validate that library1 is building correctly.

As for the issue about App1 complaining about missing libraries 3 & 4, are you sure you have no code in the app directly referencing these libraries, either that or the libraries are not being found when compiling library1. Build each library individually to validate they all build ok.

Phil H
  • 897
  • 4
  • 10
  • I did build each individually and they all built fine. Still having trouble getting your suggestion to work. I keep getting the error that `Evaluation project 'library2' using empty build file.` – prolink007 Jan 09 '14 at 15:50
  • can you confirm the paths are correct for the project definition. Also not sure if there is a missing ':' in from of 'library2'? – Phil H Jan 10 '14 at 10:32
  • Yeah, they are correct. Checked and re-checked several times. Also did copy and pasting to make sure. However, i am actually trying a different method to solve my problem and i am having a lot of success with it so far. I will post my results as soon as i have confirmed it working completely. – prolink007 Jan 10 '14 at 15:00
  • I had to use braces in order to get this to work, i.e. `compile project (':library1')` rather than `compile project ':library1'` – Tim Malseed Nov 16 '15 at 22:41
  • 1
    Updated the answer, thanks @tmalseed for the heads up. – Phil H Nov 17 '15 at 09:09
2

One question. Do you need this dependency tree?

--- App
   |--- Library 2
   |--- Library 1
       |--- Library 3
       |--- Library 4

If yes, your App does not need import the libraries 3 and 4. These dependencies are available over the Library 1.

About settings.gradle files. Why one in each module? This file is only used in the root project (like Eclipse workspace) to reference your modules (App, Library 1, Library 2, etc...)

This help you?

0

I'm doing this for relative paths:

include '..:ambilWarna'
include '..:excel'
include '..:pdfjet'
include '..:commons'
include '..:volley'


include  ':odb2'
include  ':azure'

include ':carBase'
include ':fuelTrackerLib'
include ':comsourcecastlelogbook'
stefan
  • 1,336
  • 3
  • 21
  • 46