Gradle 依赖&解决依赖冲突

# Gradle 依赖&解决依赖冲突

  • 如何定义一个依赖。
  • DependencyHandler,Dependency,Dependencies,Configuration,ConfigurationContainer 的关系。
  • 什么是传递依赖?
  • 如何定位依赖冲突?
  • 如何解决依赖冲突?
  • provided ,runtime 和 compile 三者区别?

# 如何定义依赖

在 Gradle 中常见的依赖类型有几种有 3 种,下面一一列举:

  • 方式1:依赖一个 project(或者说依赖一个 module)
  • 方式2:依赖一个 jar 包
  • 方式2:扩展:通过 fileTree 依赖 dir 文件夹下所有的 jar 包
  • 方式3:依赖远程仓库
dependencies {
    //方式1: 依赖一个名字为 "common" 的 project 
    compile project(":common")
    
    //方式2: 依赖一个本地 jar 包
    //依赖当前 module/libs/aliyun-vod-croe-android-sdk-1.0.0.jar
    compile files('libs/aliyun-vod-croe-android-sdk-1.0.0.jar')
    
    //方式2 扩展:通过 fileTree 指定 dir 依赖所有的 jar 包
    compile fileTree(dir: 'libs', include: ['*.jar'])
    
    //方式3: 依赖一个远程仓库的包
    compile 'com.android.support:appcompat-v7:26.1.0'
}

# DependencyHandler

当我们点击 build.gradle 中的 compile 时,发现跟不进去源码。那么是不是表示 compile 本不是类属性或者方法呢?

compile 'com.android.support:appcompat-v7:26.1.0'

下面我们来分析一下,compile 是什么东西。

dependencies {
  compile 'com.android.support:appcompat-v7:26.1.0'
}

compile 是定义在 dependencies 块中的,而 dependencies 是什么呢?下面来看看相关的源码:

    1. project 的 dependencies 源代码

通过阅读注释,可以知道 dependencies 接受的是一个 closure ,并且参数类型为 DependencyHandler 。那么要关注闭包具备的功能,那么就要关注 DependencyHandler 可以做什么事?

/**
 * <p>Configures the dependencies for this project.
 *
 * <p>This method executes the given closure against the {@link DependencyHand
 * DependencyHandler} is passed to the closure as the closure's delegate.
 *
 * <h3>Examples:</h3>
 * See docs for {@link DependencyHandler}
 *
 * @param configureClosure the closure to use to configure the dependencies.
 */
void dependencies(Closure configureClosure);
    1. 一组依赖的是如何表示的?

我们可以在 DependencyHandler 中找到下面这样的定义方式,那么就可以明白,我们上面写的 compile 对应于 configurationName ,而值就是对应于 dependencyNotation,我们可以将其看做是 key-value的关系,一组(configurationName,dependencyNotation)就表示一个 Configuration 对象。

dependencies {
    configurationName dependencyNotation1,dependencyNotation2
}
    1. 依赖底层的调用代码

其实 DependencyHandler 源码中有很多相关的方法,我这里列举一个 add 方法,我们通过
compile 'com.android.support:appcompat-v7:26.1.0'其实底层应该调用就是 add 方法,对应的 configurationName 为 compile 而 dependencyNotation 就是 'com.android.support:appcompat-v7:26.1.0'

/**
 * Adds a dependency to the given configuration.
 *
 * @param configurationName The name of the configuration.
 * @param dependencyNotation
 *
 * The dependency notation, in one of the notations described above.
 * @return The dependency.
 */
Dependency add(String configurationName, Object dependencyNotation);

/**
 * Adds a dependency to the given configuration, and configures the dependency using the given closure.
 *
 * @param configurationName The name of the configuration.
 * @param dependencyNotation The dependency notation, in one of the notations described above.
 * @param configureClosure The closure to use to configure the dependency.
 * @return The dependency.
 */
Dependency add(String configurationName, Object dependencyNotation, Closure configureClosure);
    1. 通过add方法实现依赖

该方法的第三参数是一个闭包,它是对 Configuration 的一个配置,闭包类型是 ModuleDependency ,你可以在这里做一些你想要的配置,例如是支持传递依赖等,详细可以查看ModuleDependency的源码。

add('compile', 'org.hibernate:hibernate-core:3.6.3.Final')
        {
            ModuleDependency dependency ->
                //(1). 指定传递依赖
                //dependency.transitive = true
                
                //(2)exclude 对某一个库排除传递依赖
                //exclude group: 'org.slf4j', module: 'slf4j-api'
        }
    1. 几个相关类的关系图

以下图表示的是 Dependency,Dependencies,DependencyHandler,Configuration,ConfigurationContainer 的关系。

依赖关系

# 什么是传递依赖?

  • 图示传递依赖

下面通过一个图来表示传递依赖,如果 transitive 为 true 表示支持传递依赖,那么 projectC 将可以依赖 projectA 的内容,其中传递依赖是不好的,因为你无法确定被依赖的库什么时候发生了更新,可能会因为更新,导致依赖失败等问题,不过 Gradle 默认是传递依赖的。

传递依赖

# 如何定位依赖冲突?

  • 了解如何定位依赖冲突问题之前,我们先手动制造一个依赖冲突。

我们在 build.gradle 引入两个依赖库:

compile 'org.hibernate:hibernate-core:3.6.3.Final'
compile 'org.slf4j:slf4j-api:1.7.22'

执行一下命令查看依赖报告:

./gradlew :module:dependencies --configuration compile

Gradle 执行结果:

compile - Compile dependencies for 'main' sources (deprecated: use 'implementation' instead).
+--- org.hibernate:hibernate-core:3.6.3.Final
|    +--- antlr:antlr:2.7.6
|    +--- commons-collections:commons-collections:3.1
|    +--- dom4j:dom4j:1.6.1
|    +--- org.hibernate:hibernate-commons-annotations:3.2.0.Final
|    |    \--- org.slf4j:slf4j-api:1.5.8 -> 1.7.22(版本自动提升)
|    +--- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final
|    +--- javax.transaction:jta:1.1
|    \--- org.slf4j:slf4j-api:1.6.1 -> 1.7.22(版本自动提升)
\--- org.slf4j:slf4j-api:1.7.22

从上面的执行结果可以看出:Gradle 在构建时,默认会使用最高版本的库,例如依赖 slf4j 最终都会以最高的版本为主。

  • Configuration 配置依赖问题

我们前面介绍过,每一个 dependency 依赖都是 Configuration ,那么我们可以遍历ConfigurationContainer,获取 每一个 Configuration 对象,然后处理对应的依赖冲突问题。

下面我们配置,当 Gradle 构建遇到依赖冲突时,就立即构建失败:

configurations.all() {
    Configuration configuration ->
        //当遇到版本冲突时直接构建失败
        configuration.resolutionStrategy.failOnVersionConflict()
}

在点击 build 时,会出现如下错误:

Gradle依赖冲突

# 如何解决依赖冲突?

在上面依赖的两个库导致的依赖冲突是因为 slf4j 的版本不同导致的,那么我们知道原因之后就可以在依赖时指定不要传入依赖某一个库即可,下面来演示一下如何操作:

方式1:

  • 排除传递依赖
compile 'org.slf4j:slf4j-api:1.7.22'
compile ('org.hibernate:hibernate-core:3.6.3.Final'){
    //排除某一个库(slf4j)依赖
    exclude group: 'org.slf4j',module: 'slf4j-api'
}
  • 下面就是排除传递依赖后的结果:
compile - Compile dependencies for 'main' sources (deprecated: use 'implementation' instead).
+--- org.slf4j:slf4j-api:1.7.22
\--- org.hibernate:hibernate-core:3.6.3.Final
     +--- antlr:antlr:2.7.6
     +--- commons-collections:commons-collections:3.1
     +--- dom4j:dom4j:1.6.1
     +--- org.hibernate:hibernate-commons-annotations:3.2.0.Final
     +--- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final
     \--- javax.transaction:jta:1.1

  • 指定禁止传递依赖
compile('org.hibernate:hibernate-core:3.6.3.Final') {
    //指定禁止传递依赖
    transitive false
}
compile 'org.slf4j:slf4j-api:1.7.22'
  • 查看依赖报告
compile - Compile dependencies for 'main' sources (deprecated: use 'implementation' instead).
+--- org.hibernate:hibernate-core:3.6.3.Final
\--- org.slf4j:slf4j-api:1.7.22

方式2:

  • 当遇到依赖冲突时,指定一个版本号
configurations.all() {
    Configuration configuration ->
        configuration.resolutionStrategy.force(['org.slf4j:slf4j-api:1.6.1'])
        
        //或者这样写
        resolutionStrategy.setForcedModules(['org.slf4j:slf4j-api:1.6.1'])
}

# provided ,runtime 和 compile 三者区别?

compile : 依赖的包,编译并打包到最终的 apk 文件中。

provided : 依赖的包只参与编译而不会打包到最终的 apk 文件中。

runtime : 适用于依赖的包只作用在运行时而不需要在编译时。

dependencies {
    //optional, help to generate the final application 
    provided('com.tencent.tinker:tinker-android-anno:1.9.1')
    //tinker's main Android lib
    compile('com.tencent.tinker:tinker-android-lib:1.9.1')
}

为应用生成 application 类,那么这个是一个 java 文件的生成,因此只需要在编译阶段去生成即可,而在运行时已经有这个生成的类了。

tinker依赖

# 总结

上面是我学习了 Grade 依赖相关知识后的一点点总结,通过了解 Gradle 的依赖,从而能正确的处理每一个库的依赖关系。

记录于 2018-08-13 晚

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,494评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,678评论 6 342
  • 说明 本文主要介绍和Gradle关系密切、相对不容易理解的配置,偏重概念介绍。部分内容是Android特有的(例如...
    jzj1993阅读 15,507评论 1 62
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,049评论 25 707
  • 腊月廿八中午,父亲载着我从火车北站回家,到小镇的时候已经三点多了。 路过镇上大桥的时候,却看见桥已经没了,夏天的洪...
    岳麦阅读 498评论 0 1