接入第三方的时候,经常出现冲突。由于自学,只能百度找办法,然后总节一下经验
一、(重要+重要+重要)查看依赖
CD到当前目录后,使用命令:
gradlew app:dependencies
输出如下:
依赖的结构很明显了,可以针对地进行排除
二、根据冲突报错解决问题
报错大概有两种
1、重复的类,这种
1 exception was raised by workers:
java.lang.RuntimeException: Duplicate class android.support.compat.BuildConfig found in modules android-support-v4.jar (android-support-v4.jar) and support-compat-27.0.2-runtime.jar (com.android.support:support-compat:27.0.2)
Duplicate class android.support.coreui.BuildConfig found in modules android-support-v4.jar (android-support-v4.jar) and support-core-ui-27.0.2-runtime.jar (com.android.support:support-core-ui:27.0.2)
Duplicate class android.support.coreutils.BuildConfig found in modules android-support-v4.jar (android-support-v4.jar) and support-core-utils-27.0.2-runtime.jar (com.android.support:support-core-utils:27.0.2)
Duplicate class android.support.fragment.BuildConfig found in modules android-support-v4.jar (android-support-v4.jar) and support-fragment-27.0.2-runtime.jar (com.android.support:support-fragment:27.0.2)
Duplicate class android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat found in modules android-support-v4.jar (android-support-v4.jar) and support-compat-27.0.2-runtime.jar (com.android.support:support-compat:27.0.2)
Duplicate class android.support.v4.app.ActionBarDrawerToggle found in modules android-support-v4.jar (android-support-v4.jar) and support-core-ui-27.0.2-runtime.jar (com.android.support:support-core-ui:27.0.2)
Duplicate class android.support.v4.app.ActionBarDrawerToggle$Delegate found in modules android-support-v4.jar (android-support-v4.jar) and support-core-ui-27.0.2-runtime.jar (com.android.support:support-core-ui:27.0.2)
2、类型已经存在
Program type already present: MTT.ThirdAppInfoNew
这种就是编译module时,已经在另一个Module已经编译了这个类型了.
两个module都引用了同一个第三方库,如上面这个报错就是都引用了 x5内核
三、解决办法
总的来说,就是冲突时,选择保留一个。
考虑有 app 引用了模块A 和 模块B , A,B两个模块中有冲突
1、合理使用 api implementation 和 compileOnly
api 通俗的说,就是 模块引用的第三方库,可以给app用
implementation 通俗的说,就是 模块引用的第三方库,app不能用
compileOnly 通俗的说,就是 模块引用的第三方库,只编译,不打包
2、使用exclude进行依赖排除
api (project(': eduhdsdk')){
exclude group: 'com.umeng.umsdk', module: 'common'
}
implementation("io.socket:socket.io-client:1.0.0") {
exclude group: 'org.json', module: 'json'
}
排除的用法 可以参考 http://www.qb5200.com/article/547002.html
介绍得很清楚
3、全局指定依赖版本
在app的build.gradle文件中 android{ .... }添加下面
configurations.all {
resolutionStrategy {
// 强制使用 28.0.2 版本的 support 包
force 'com.android.support:support-v4:28.0.2'
// 强制使用 4.9.0 版本的 glide 库
force 'com.github.bumptech.glide:glide:4.9.0'
failOnVersionConflict()
}
}
四、其它情况,搜到的解决办法,没使用过。记录一下
要移除的jar包在aar包中的classes.jar中
https://www.jianshu.com/p/4fd0e50cd335
因为混淆导致的冲突
https://www.jianshu.com/p/96c7832eb068