Java Android Gradle 向项目添加外部库和嵌套外部库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21001232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:51:10  来源:igfitidea点击:

Android Gradle adding external library and nested external libraries to a project

javaandroidbuildgradleandroid-gradle-plugin

提问by prolink007

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

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



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
  • 应用/
    • 应用1/
      • 构建.gradle
      • 设置.gradle
    • 图书馆/
      • 图书馆1/
        • 建造等级
        • 设置.gradle
      • 图书馆2/
        • 建造等级
        • 设置.gradle
      • 图书馆3/
        • 建造等级
        • 设置.gradle
      • 图书馆4/
        • 建造等级
        • 设置.gradle


App1

应用程序1

App1/build.gradle

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 library3or library4, however, it will complain if i don't include them in the dependencies in the build.gradlefile and the settings.gradlefile. So, i have them included just to stop it from complaining.

App1 不直接依赖library3or library4,但是,如果我不将它们包含在build.gradle文件和settings.gradle文件的依赖项中,它会抱怨。所以,我把它们包括在内只是为了阻止它抱怨。

App1/settings.gradle

App1/settings.gradle

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


library1

图书馆1

library1/build.gradle

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

library1/settings.gradle

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


library2..4

图书馆2..4

library2..4/build.gradle

library2..4/build.gradle

buildscript {
    ...
}

apply plugin: 'android-library'

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

android {
    ...
}

library2..4/settings.gradle

library2..4/settings.gradle

include ':'


When trying to gradlew clean buildon App1i get the following error:

尝试gradlew clean build打开时App1,出现以下错误:

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

线15compile project(':..:library3')library1/build.gradle的文件。

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

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

采纳答案by Phil H

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

在你的顶级 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')

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

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

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

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

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

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

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.

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

回答by Luis E. Fernandez

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.

如果是,则您的 App 不需要导入库 3 和 4。这些依赖项在库 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...)

关于 settings.gradle 文件。为什么每个模块都有一个?此文件仅用于根项目(如 Eclipse 工作区)以引用您的模块(应用程序、库 1、库 2 等...)

This help you?

这对你有帮助吗?

回答by stefan

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'