# Gradle 依賴&解決依賴沖突
- 如何定義一個(gè)依賴。
- DependencyHandler挽荠,Dependency克胳,Dependencies,Configuration圈匆,ConfigurationContainer 的關(guān)系漠另。
- 什么是傳遞依賴?
- 如何定位依賴沖突跃赚?
- 如何解決依賴沖突笆搓?
- provided ,runtime 和 compile 三者區(qū)別?
# 如何定義依賴
在 Gradle 中常見的依賴類型有幾種有 3 種纬傲,下面一一列舉:
- 方式1:依賴一個(gè) project(或者說依賴一個(gè) module)
- 方式2:依賴一個(gè) jar 包
- 方式2:擴(kuò)展:通過 fileTree 依賴 dir 文件夾下所有的 jar 包
- 方式3:依賴遠(yuǎn)程倉庫
dependencies {
//方式1: 依賴一個(gè)名字為 "common" 的 project
compile project(":common")
//方式2: 依賴一個(gè)本地 jar 包
//依賴當(dāng)前 module/libs/aliyun-vod-croe-android-sdk-1.0.0.jar
compile files('libs/aliyun-vod-croe-android-sdk-1.0.0.jar')
//方式2 擴(kuò)展:通過 fileTree 指定 dir 依賴所有的 jar 包
compile fileTree(dir: 'libs', include: ['*.jar'])
//方式3: 依賴一個(gè)遠(yuǎn)程倉庫的包
compile 'com.android.support:appcompat-v7:26.1.0'
}
# DependencyHandler
當(dāng)我們點(diǎn)擊 build.gradle 中的
compile
時(shí)满败,發(fā)現(xiàn)跟不進(jìn)去源碼。那么是不是表示compile
本不是類屬性或者方法呢叹括?
compile 'com.android.support:appcompat-v7:26.1.0'
下面我們來分析一下算墨,compile
是什么東西。
dependencies {
compile 'com.android.support:appcompat-v7:26.1.0'
}
compile 是定義在 dependencies 塊中的汁雷,而 dependencies 是什么呢净嘀?下面來看看相關(guān)的源碼:
- project 的 dependencies 源代碼
通過閱讀注釋报咳,可以知道
dependencies
接受的是一個(gè) closure ,并且參數(shù)類型為DependencyHandler
挖藏。那么要關(guān)注閉包具備的功能暑刃,那么就要關(guān)注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);
- 一組依賴的是如何表示的膜眠?
我們可以在 DependencyHandler 中找到下面這樣的定義方式稍走,那么就可以明白,我們上面寫的
compile
對應(yīng)于 configurationName 柴底,而值就是對應(yīng)于 dependencyNotation,我們可以將其看做是 key-value的關(guān)系粱胜,一組(configurationName,dependencyNotation)就表示一個(gè) Configuration 對象柄驻。
dependencies {
configurationName dependencyNotation1,dependencyNotation2
}
- 依賴底層的調(diào)用代碼
其實(shí) DependencyHandler 源碼中有很多相關(guān)的方法,我這里列舉一個(gè) add 方法焙压,我們通過
compile 'com.android.support:appcompat-v7:26.1.0'
其實(shí)底層應(yīng)該調(diào)用就是 add 方法鸿脓,對應(yīng)的 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);
- 通過
add
方法實(shí)現(xiàn)依賴
- 通過
該方法的第三參數(shù)是一個(gè)閉包,它是對 Configuration 的一個(gè)配置涯曲,閉包類型是 ModuleDependency 野哭,你可以在這里做一些你想要的配置,例如是支持傳遞依賴等幻件,詳細(xì)可以查看
ModuleDependency
的源碼拨黔。
add('compile', 'org.hibernate:hibernate-core:3.6.3.Final')
{
ModuleDependency dependency ->
//(1). 指定傳遞依賴
//dependency.transitive = true
//(2)exclude 對某一個(gè)庫排除傳遞依賴
//exclude group: 'org.slf4j', module: 'slf4j-api'
}
- 幾個(gè)相關(guān)類的關(guān)系圖
以下圖表示的是 Dependency,Dependencies绰沥,DependencyHandler篱蝇,Configuration,ConfigurationContainer 的關(guān)系徽曲。
# 什么是傳遞依賴零截?
- 圖示傳遞依賴
下面通過一個(gè)圖來表示傳遞依賴,如果 transitive 為 true 表示支持傳遞依賴,那么 projectC 將可以依賴 projectA 的內(nèi)容秃臣,其中傳遞依賴是不好的涧衙,因?yàn)槟銦o法確定被依賴的庫什么時(shí)候發(fā)生了更新,可能會因?yàn)楦掳麓耍瑢?dǎo)致依賴失敗等問題弧哎,不過 Gradle 默認(rèn)是傳遞依賴的。
# 如何定位依賴沖突稚虎?
- 了解如何定位依賴沖突問題之前傻铣,我們先手動制造一個(gè)依賴沖突。
我們在 build.gradle 引入兩個(gè)依賴庫:
compile 'org.hibernate:hibernate-core:3.6.3.Final'
compile 'org.slf4j:slf4j-api:1.7.22'
執(zhí)行一下命令查看依賴報(bào)告:
./gradlew :module:dependencies --configuration compile
Gradle 執(zhí)行結(jié)果:
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
從上面的執(zhí)行結(jié)果可以看出:Gradle 在構(gòu)建時(shí)祥绞,默認(rèn)會使用最高版本的庫非洲,例如依賴 slf4j 最終都會以最高的版本為主鸭限。
- Configuration 配置依賴問題
我們前面介紹過,每一個(gè) dependency 依賴都是 Configuration 两踏,那么我們可以遍歷ConfigurationContainer败京,獲取 每一個(gè) Configuration 對象,然后處理對應(yīng)的依賴沖突問題梦染。
下面我們配置赡麦,當(dāng) Gradle 構(gòu)建遇到依賴沖突時(shí),就立即構(gòu)建失斉潦丁:
configurations.all() {
Configuration configuration ->
//當(dāng)遇到版本沖突時(shí)直接構(gòu)建失敗
configuration.resolutionStrategy.failOnVersionConflict()
}
在點(diǎn)擊 build 時(shí)泛粹,會出現(xiàn)如下錯(cuò)誤:
# 如何解決依賴沖突?
在上面依賴的兩個(gè)庫導(dǎo)致的依賴沖突是因?yàn)?slf4j 的版本不同導(dǎo)致的肮疗,那么我們知道原因之后就可以在依賴時(shí)指定不要傳入依賴某一個(gè)庫即可晶姊,下面來演示一下如何操作:
方式1:
- 排除傳遞依賴
compile 'org.slf4j:slf4j-api:1.7.22'
compile ('org.hibernate:hibernate-core:3.6.3.Final'){
//排除某一個(gè)庫(slf4j)依賴
exclude group: 'org.slf4j',module: 'slf4j-api'
}
- 下面就是排除傳遞依賴后的結(jié)果:
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'
- 查看依賴報(bào)告
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:
- 當(dāng)遇到依賴沖突時(shí),指定一個(gè)版本號
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 三者區(qū)別伪货?
compile : 依賴的包们衙,編譯并打包到最終的 apk 文件中。
provided : 依賴的包只參與編譯而不會打包到最終的 apk 文件中碱呼。
runtime : 適用于依賴的包只作用在運(yùn)行時(shí)而不需要在編譯時(shí)蒙挑。
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')
}
為應(yīng)用生成 application 類,那么這個(gè)是一個(gè) java 文件的生成愚臀,因此只需要在編譯階段去生成即可忆蚀,而在運(yùn)行時(shí)已經(jīng)有這個(gè)生成的類了。
# 總結(jié)
上面是我學(xué)習(xí)了 Grade 依賴相關(guān)知識后的一點(diǎn)點(diǎn)總結(jié)姑裂,通過了解 Gradle 的依賴蜓谋,從而能正確的處理每一個(gè)庫的依賴關(guān)系。
記錄于 2018-08-13 晚