Android Gradle将外部库和嵌套外部库添加到项目中

时间:2014-01-08 16:30:06

标签: java android build gradle android-gradle

如何将外部库和嵌套外部库添加到android项目中?


我的项目结构(不允许更改)

  • 应用程序/
    • App1的/
      • 的build.gradle
      • settings.gradle
    • 库/
      • 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并不直接依赖library3library4,但如果我不将它们包含在build.gradle文件和{{1}的依赖项中,它会抱怨文件。所以,我把它们包括在内只是为了阻止它抱怨。

App1的/ settings.gradle

settings.gradle

LIBRARY1

LIBRARY1 /的build.gradle

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

LIBRARY1 / settings.gradle

buildscript {
    ...
}

apply plugin: 'android-library'

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

android {
    ...
}

library2..4

library2..4 /的build.gradle

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

library2..4 / settings.gradle

buildscript {
    ...
}

apply plugin: 'android-library'

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

android {
    ...
}

include ':' 上尝试gradlew clean build时,我收到以下错误:

App1

第15行是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'. 文件中的compile project(':..:library3')

如何将外部库和嵌套外部库添加到android项目中?

3 个答案:

答案 0 :(得分:15)

在顶级settings.gradle(App1 / settings.gradle)文件中为每个库执行类似的操作

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')

删除其他settings.gradle文件,您不需要它们

然后在每个构建脚本中,您只需要使用

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

如上所述,只需在根项目(App1)中使用一个settings.gradle文件。

然后从App1文件夹运行gradlew clean :library1:build以验证library1是否正确构建。

关于App1抱怨丢失库3& 4,您确定应用程序中没有直接引用这些库的代码,或者在编译library1时找不到库。单独构建每个库以验证它们都构建正常。

答案 1 :(得分:2)

一个问题。 你需要这个依赖树吗?

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

如果是,您的应用程序不需要导入库3和4.这些依赖项可通过库1获得。

关于settings.gradle文件。为什么每个模块中有一个?此文件仅用于根项目(如Eclipse工作区)以引用您的模块(App,Library 1,Library 2等)

这对你有帮助吗?

答案 2 :(得分:0)

我正在为相对路径做这件事:

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


include  ':odb2'
include  ':azure'

include ':carBase'
include ':fuelTrackerLib'
include ':comsourcecastlelogbook'