解决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
    评论
在 Spring 使用 SLF4J 和 Log4j2,需要在 Gradle 添加以下依赖: ``` dependencies { implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-log4j2' implementation 'org.slf4j:slf4j-api:1.7.30' implementation 'org.slf4j:jul-to-slf4j:1.7.30' implementation 'org.slf4j:log4j-over-slf4j:1.7.30' implementation 'org.apache.logging.log4j:log4j-core:2.14.1' implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.14.1' } ``` 其,`spring-boot-starter-log4j2` 包含了 Log4j2 的依赖,`slf4j-api` 是 SLF4J 的接口,`jul-to-slf4j` 和 `log4j-over-slf4j` 是将 JUL 和 Log4j 转换为 SLF4J 的实现,`log4j-core` 和 `log4j-slf4j-impl` 是 Log4j2 的实现。 在 Spring 配置 Log4j2,需要在 `src/main/resources` 目录下创建一个 `log4j2.xml` 文件,配置如下: ``` <?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration> ``` 这里使用了一个控制台日志输出的 Appender,配置了日志输出的格式和级别。最后将 Appender 添加到 Root Logger 即可。 在 Spring 使用日志,可以直接使用 SLF4J 的接口,例如: ``` import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyClass { private static final Logger logger = LoggerFactory.getLogger(MyClass.class); public void myMethod() { logger.debug("Debug message"); logger.info("Info message"); logger.warn("Warn message"); logger.error("Error message"); } } ``` 这里使用了 `LoggerFactory.getLogger` 方法创建了一个 Logger 实例,然后使用不同级别的日志输出信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值