Android Studio Gradle 解决依赖冲突

作者: 魏树鑫 | 来源:发表于2019-04-09 21:06 被阅读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. 显式依赖

  • 在build.gradle中显示定义冲突的库,这是解决冲突的一种方式,在这种情况下,我们需要明确提出我们想要包含在任何一个配置的最终构建中的库的版本。
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'
      }
}

相关文章

网友评论

    本文标题:Android Studio Gradle 解决依赖冲突

    本文链接:https://www.haomeiwen.com/subject/jwcwiqtx.html