栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

一次解决依赖库冲突的历程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一次解决依赖库冲突的历程

最近在一次gradle同步后,突然编译报错,看一下错误原因,是引用库冲突了,截取信息如下。

> 1 exception was raised by workers:
  java.lang.RuntimeException: Duplicate class net.lingala.zip4j.core.HeaderReader found in modules classes.jar (com.tsing.croods.components:croods-cmpt-zip:1.3.8) and zip4j-1.3.2.jar (net.lingala.zip4j:zip4j:1.3.2)

从上面的信息可以看出来,是引用net.lingala.zip4j:zip4j冲突了。

针对这种情况我们可以在gradle中implementation时使用exclude关键词来排除net.lingala.zip4j:zip4j ,但是第一步需要知道到底是哪个库引用到这个库了。

项目中引用的库很多,不可能人工一个一个去排查,好在gradle有为我们提供了这样的指令来查看依赖库。

以下指令就是来查找项目中哪些库有依赖到net.lingala.zip4j:zip4j。

 ./gradlew :app:dependencyInsight --configuration debugCompileClasspath --dependency net.lingala.zip4j:zip4j

输入指令回车后就会得到以下信息:

net.lingala.zip4j:zip4j:1.3.2
   variant "compile" [
      org.gradle.status                                            = release (not requested)
      org.gradle.usage                                             = java-api
      org.gradle.category                                          = library (not requested)

      Requested attributes not found in the selected variant:
         com.android.build.api.attributes.BuildTypeAttr               = debug
         com.android.build.gradle.internal.dependency.AndroidTypeAttr = Aar
   ]
   Selection reasons:
      - By constraint : debugRuntimeClasspath uses version 1.3.2

net.lingala.zip4j:zip4j:{strictly 1.3.2} -> 1.3.2
--- debugCompileClasspath

net.lingala.zip4j:zip4j:1.3.2
--- com.tsing.syncdata:syncdata:1.0.1.1-SNAPSHOT:20210825.112636-153
     --- debugCompileClasspath

从上面的信息就很直观的发现是com.tsing.syncdata:syncdata这个库有依赖net.lingala.zip4j:zip4j。那我们就可以使用exclude关键词了。具体如下:

implementation('com.tsing.syncdata:syncdata:1.0.1.1-SNAPSHOT', {
    exclude group: 'net.lingala.zip4j', module: 'zip4j'
})

PS:

如果你想查看所有的依赖树,则需要使用dependencies指令:
./gradlew :app:dependencies --configuration debugCompileClasspath指令前的:app是你的module名configuration 参数用于指定 variant,例如你还可以传入 releaseCompileClasspath

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/782701.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号