# 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 是什么呢?下面來看看相關的源碼:
- 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);
- 一組依賴的是如何表示的?
我們可以在 DependencyHandler 中找到下面這樣的定義方式,那么就可以明白,我們上面寫的
compile
對應于 configurationName ,而值就是對應于 dependencyNotation,我們可以將其看做是 key-value的關系,一組(configurationName,dependencyNotation)就表示一個 Configuration 對象。
dependencies {
configurationName dependencyNotation1,dependencyNotation2
}
- 依賴底層的調用代碼
其實 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);
- 通過
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'
}
- 幾個相關類的關系圖
以下圖表示的是 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 時,會出現如下錯誤:
# 如何解決依賴沖突?
在上面依賴的兩個庫導致的依賴沖突是因為 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 文件的生成,因此只需要在編譯階段去生成即可,而在運行時已經有這個生成的類了。
# 總結
上面是我學習了 Grade 依賴相關知識后的一點點總結,通過了解 Gradle 的依賴,從而能正確的處理每一個庫的依賴關系。
記錄于 2018-08-13 晚