解决Android打包过程中出现的slf4j依赖冲突问题

解决Android打包过程中出现的slf4j依赖冲突问题

背景交代:

昨天同事说xxx项目Jenkins构建失败,并给我发了一个构建失败的日志文件

2021-12-21T10:03:29.090Z]   java.lang.RuntimeException: Duplicate class org.slf4j.ILoggerFactory found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.090Z]   Duplicate class org.slf4j.IMarkerFactory found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.090Z]   Duplicate class org.slf4j.Logger found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.090Z]   Duplicate class org.slf4j.LoggerFactory found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.090Z]   Duplicate class org.slf4j.MDC found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.MDC$1 found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.MDC$MDCCloseable found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.Marker found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.MarkerFactory found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.event.EventConstants found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.event.EventRecodingLogger found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.event.Level found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.event.LoggingEvent found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.event.SubstituteLoggingEvent found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.BasicMDCAdapter found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.BasicMDCAdapter$1 found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.BasicMarker found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.BasicMarkerFactory found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.FormattingTuple found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.MarkerIgnoringBase found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.MessageFormatter found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.NOPLogger found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.NOPLoggerFactory found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.NOPMDCAdapter found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.NamedLoggerBase found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.SubstituteLogger found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.SubstituteLoggerFactory found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.Util found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.Util$1 found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.helpers.Util$ClassContextSecurityManager found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.spi.LocationAwareLogger found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.spi.LoggerFactoryBinder found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.spi.MDCAdapter found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
[2021-12-21T10:03:29.091Z]   Duplicate class org.slf4j.spi.MarkerFactoryBinder found in modules lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar (com.xxxx.lib:lib_xxxx:0.1.xxx-SNAPSHOT:20211221.094703-1) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)

lib_xxxx-0.1.xxx-xxx-SNAPSHOT-runtime.jar与slf4j-api-1.7.21.jar中都引入了以上这些类,导致出现冲突。

开始分析

找到罪魁祸首
  1. 通过查看最近的lib库提交记录,果然发现昨天在lib库中增加了slf4j的包image-20211222115643726

  2. 接着找到第二个包,在打包的apk项目根目录下,使用命令

    ./gradlew :***:***:dependencies
    

    在一个danikula的库下发现了其踪迹

    image-20211222115959324

制定解决方案

这种问题解决方案一般有两种策略

  1. 两方引用该库时统一版本号,比如都用比较新的1.7.25,但是由于danikula属于第三方库,并且已经三年不更新了,要改也是我们自己这边改成1.7.21

  2. 打包时移除指定引用,该方案也是我们最终采用的方案

        implementation (rootProject.ext.dependencies["videocache"]){
            exclude group: 'org.slf4j'
        }
    

当然,除了以上方案,还有强制使用一个固定版本等,

参考文章:

https://www.zhyea.com/2018/02/08/gradle-exclude-dependencies.html

https://tomgregory.com/how-to-exclude-gradle-dependencies/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值