gradle 使用 exclude 解决jar包冲突

1. 查看冲突的jar包;一眼能看出来的跳到第二步; 遇到隐藏比较深的就比较难受了,肉眼完全看不出来,此时使用以下命令查看:

gradlew -q app:dependencies 查看依赖树;

比如: 

Duplicate class androidx.dynamicanimation.animation.DynamicAnimation$OnAnimationUpdateListener found in modules classes.jar (androidx.dynamicanimation:dynamicanimation:1.0.0) and classes.jar (dynamicanimation-1.1.0-alpha04.aar)
Duplicate class androidx.dynamicanimation.animation.DynamicAnimation$ViewProperty found in modules classes.jar (androidx.dynamicanimation:dynamicanimation:1.0.0) and classes.jar (dynamicanimation-1.1.0-alpha04.aar)

gradlew -q app:dependencies:

。。。

+--- com.airbnb.android:lottie:3.3.1
|    +--- androidx.appcompat:appcompat:1.0.0 -> 1.3.0-rc01 (*)
|    \--- com.squareup.okio:okio:1.17.4
+--- com.google.android.material:material:1.3.0
|    +--- androidx.annotation:annotation:1.0.1 -> 1.2.0
|    +--- androidx.appcompat:appcompat:1.1.0 -> 1.3.0-rc01 (*)
|    +--- androidx.cardview:cardview:1.0.0
|    |    \--- androidx.annotation:annotation:1.0.0 -> 1.2.0
|    +--- androidx.coordinatorlayout:coordinatorlayout:1.1.0 (*)
|    +--- androidx.constraintlayout:constraintlayout:2.0.1 -> 2.1.0-beta01 (*)
|    +--- androidx.core:core:1.2.0 -> 1.6.0-alpha01 (*)
|    +--- androidx.dynamicanimation:dynamicanimation:1.0.0
|    |    +--- androidx.core:core:1.0.0 -> 1.6.0-alpha01 (*)
|    |    +--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
|    |    \--- androidx.legacy:legacy-support-core-utils:1.0.0 (*)
|    +--- androidx.annotation:annotation-experimental:1.0.0
|    +--- androidx.fragment:fragment:1.0.0 -> 1.3.2 (*)
|    +--- androidx.lifecycle:lifecycle-runtime:2.0.0 -> 2.3.1 (*)
|    +--- androidx.recyclerview:recyclerview:1.0.0 -> 1.2.0-rc01 (*)

右键find, 找到dynamicanimation:dynamicanimation,可以看到com.google.android.material:material:1.3.0 引入了它;

2.使用exclude过滤掉;

implementation ('com.google.android.material:material:1.3.0') {
    exclude group: "androidx.dynamicanimation", module: "dynamicanimation"
}
当两个或多个 JAR 包中存在相同的类时,就会发生冲突。在你的情况下,weblogic.jar 和 mqjar 包中都可能包含相同的类,这导致了冲突。 为了解决这个问题,你可以尝试以下几个步骤: 1. 了解冲突的原因:你需要找出哪些类是在这两个包中重复的。你可以使用命令行工具 `jar -tf jarfilename.jar` 来查看 JAR 包中的内容。如果你看到重复的类,这就是冲突的原因。 2. 排除重复的类:你可以使用 `exclude` 属性来排除重复的类。在 Maven 中,你可以在依赖项中添加 `<exclude>` 标签来排除重复的类。例如: ``` <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0</version> <exclusions> <exclusion> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </exclusion> </exclusions> </dependency> ``` 在 Gradle 中,你可以使用 `exclude` 方法来排除重复的类。例如: ``` dependencies { compile('com.example:example:1.0') { exclude group: 'org.apache.commons', module: 'commons-lang3' } } ``` 3. 使用 ClassLoader 进行隔离:你可以使用不同的 ClassLoader 来加载不同的 JAR 包,从而避免冲突。例如,对于 Web 应用程序,你可以将 JAR 包放在 WEB-INF/lib 目录下,并使用 Web 应用程序的 ClassLoader 来加载它们。 ``` // 获取 Web 应用程序的 ClassLoader ClassLoader webAppClassLoader = Thread.currentThread().getContextClassLoader(); // 加载 JAR 包 URL jarUrl = new URL("file:/path/to/jarfile.jar"); URLClassLoader jarClassLoader = new URLClassLoader(new URL[] { jarUrl }, webAppClassLoader); Class<?> clazz = jarClassLoader.loadClass("com.example.MyClass"); // 使用加载的类 Object obj = clazz.newInstance(); ``` 希望这些方法可以帮助你解决这个问题。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值