Android开发经验谈Java、Android技术栈数据结构和算法分析

Android Studio Gradle 解决依赖冲突

2019-04-09  本文已影响7人  魏树鑫

1. 查看依赖树

./gradlew dependencies
image.png

2. 解决依赖冲突

一旦在构建中存在依赖冲突,开发人员需要决定哪个版本的库最终包含在构建中,有许多解决冲突的方法。

1. 逐个排除

compile('junit:junit:4.12'){
  exclude group : 'org.hamcrest',module:'hamcrest-core'
}
//最终,如果我们向包含1.3版本到构建中,我们可以从“mockito"中排除他
androidTestCompile('org.mockito:mockito-core:1.10.19'){
  exclude group : 'org.hamcrest',module:'hamcrest-core'
}

2. 显式依赖

compile 'junit:junit:4.12'
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'org.hamcrest:hamcrest-core:1.3'

如果多个依赖具有冲突版本的依赖或传递依赖的话,则不是从每个依赖性中排除模块,而是可以简单的使用期望的版本号来定义冲突依赖。

这种是一种更清洁的解决冲突的方法,但缺点是,当更新实际的依赖关系的时候,开发人员需要更新冲突的库。

3. 强制依赖

//在app.gradle 中的Android闭包中使用
android{
      configurations.all {
           resolutionStrategy.force 'com.android.support:appcompat-v7:28.0.0'
           resolutionStrategy.force 'com.android.support:support-v4:28.0.0'
           resolutionStrategy.force 'com.android.support:animated-vector-drawable:28.0.0'
           resolutionStrategy.force 'com.android.support:support-media-compat:28.0.0'
      }
}

//在build.gradle 中设置工程全局强制依赖
allprojects{
      configurations.all {
           resolutionStrategy.force 'com.android.support:appcompat-v7:28.0.0'
           resolutionStrategy.force 'com.android.support:support-v4:28.0.0'
           resolutionStrategy.force 'com.android.support:animated-vector-drawable:28.0.0'
           resolutionStrategy.force 'com.android.support:support-media-compat:28.0.0'
      }
}

上一篇下一篇

猜你喜欢

热点阅读