Gradle依赖冲突求助:升级OkHttp3至4.1.0后构建失败
解决OkHttp3升级到4.1.0后的重复类冲突问题
我来帮你搞定这个头疼的重复类问题!咱们先把根源理清楚:OkHttp 4.x版本开始默认依赖了org.jetbrains:annotations,而你在依赖里显式声明了com.intellij:annotations:+——这俩其实是同一套JetBrains注解类,只是后来JetBrains把groupId从com.intellij改成了org.jetbrains,直接导致了类重复的冲突。
接下来按这几步修改就能解决问题:
1. 移除多余的显式annotations依赖
直接删掉你dependencies里的这一行:
implementation 'com.intellij:annotations:+@jar'
OkHttp 4.1.0已经自带了兼容的org.jetbrains:annotations版本,完全不需要自己额外声明。
2. 统一OkHttp相关依赖的版本
你现在OkHttp核心库是4.1.0,但logging-interceptor和okhttp-urlconnection还停留在3.9.1,版本不一致很容易引发隐藏问题,把它们也同步升级到4.1.0:
implementation 'com.squareup.okhttp3:okhttp:4.1.0' implementation 'com.squareup.okhttp3:logging-interceptor:4.1.0' implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.1.0'
3. 排查其他可能引入旧annotations的依赖
如果做完前两步还是有冲突,就用Gradle的依赖分析命令定位问题:
- Mac/Linux终端运行:
./gradlew app:dependencyInsight --dependency com.intellij:annotations - Windows运行:
gradlew.bat app:dependencyInsight --dependency com.intellij:annotations
找到对应的依赖后,在该依赖里排除掉旧的annotations,比如:
implementation('第三方库的依赖坐标') { exclude group: 'com.intellij', module: 'annotations' }
修改后的核心dependencies片段
dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', { exclude group: 'com.android.support', module: 'support-annotations' }) // 其他原有依赖保持不变... // 统一OkHttp全系列版本 implementation 'com.squareup.okhttp3:okhttp:4.1.0' implementation 'com.squareup.okhttp3:logging-interceptor:4.1.0' implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.1.0' // 其他原有依赖保持不变... // 已移除com.intellij:annotations:+@jar }
最后同步Gradle,重新构建项目,重复类的问题应该就能彻底解决了。
内容的提问来源于stack exchange,提问作者Abdul Rahman Shamair
相关产品推荐
相关产品推荐

