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

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


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

  • 应用/
    • 应用1/
      • build.gradle
      • settings.gradle
    • 库/
      • 图书馆1/
        • 构建级
        • settings.gradle
      • 库2/
        • 构建级
        • settings.gradle
      • 库3/
        • 构建级
        • settings.gradle
      • 图书馆4/
        • 构建级
        • settings.gradle

应用1

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不直接依赖于或,但是,如果我没有将它们包含在文件和文件的依赖项中,它将抱怨。所以,我把它们包括在内,只是为了阻止它抱怨。library3library4build.gradlesettings.gradle

App1/settings.gradle

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

图书馆1

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'

库2..4

库2..4/build.gradle

buildscript {
    ...
}

apply plugin: 'android-library'

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

android {
    ...
}

库2..4/设置

include ':'

当尝试在 i 得到以下错误:gradlew clean buildApp1

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

第 15 行位于文件中。compile project(':..:library3')library1/build.gradle

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


答案 1

在顶级设置中。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')

移除其他设置。gradle 文件,您不需要它们

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

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

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

然后从 App1 文件夹中运行以验证库 1 是否正在正确生成。gradlew clean :library1:build

至于 App1 抱怨缺少库 3 和 4 的问题,你确定应用中没有直接引用这些库的代码,或者编译库 1 时找不到这些库。单独构建每个库以验证它们是否构建正常。


答案 2

一个问题。是否需要此依赖关系树?

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

如果是,则你的应用不需要导入库 3 和 4。这些依赖项在库 1 上可用。

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

这对你有帮助吗?


推荐