Gradle依赖冲突:添加含Kotlin Compiler的自定义SDK后出现重复类问题求助
解决自定义SDK引入后的重复类冲突问题
针对你遇到的ListenableFuture和Nls注解类重复冲突问题,试试下面几个方案:
方案1:排除SDK传递依赖中的冲突库
在引入自定义SDK时,直接排除掉它带进来的冲突依赖,让项目只保留单一版本的类文件。修改依赖配置如下:
dependencies { implementation "conflicting_custom_sdk_that_contains_kotlin_compiler_etc:0.0.1" { // 排除SDK传递的listenablefuture依赖 exclude group: 'com.google.guava', module: 'listenablefuture' // 排除SDK传递的jetbrains annotations依赖 exclude group: 'org.jetbrains', module: 'annotations' } // 其他依赖保持不变 implementation 'androidx.core:core-ktx:1.12.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0' implementation 'androidx.activity:activity-compose:1.8.2' implementation platform('androidx.compose:compose-bom:2023.10.01') implementation 'androidx.compose.ui:ui' implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-tooling-preview' implementation 'androidx.compose.material3:material3' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation platform('androidx.compose:compose-bom:2023.10.01') androidTestImplementation 'androidx.compose.ui:ui-test-junit4' debugImplementation 'androidx.compose.ui:ui-tooling' debugImplementation 'androidx.compose.ui:ui-test-manifest' }
方案2:强制统一冲突依赖的版本
如果排除依赖后出现兼容性问题,可以强制让Gradle使用指定版本的冲突库,确保全项目只有一份类文件。在dependencies块外添加配置:
configurations.all { resolutionStrategy { // 强制使用应用侧的annotations版本 force 'org.jetbrains:annotations:23.0.0' // 强制使用指定版本的listenablefuture force 'com.google.guava:listenablefuture:1.0' } } dependencies { // 原依赖配置保持不变 }
方案3:修改SDK的依赖配置(若有权限)
如果能修改自定义SDK的构建脚本,建议将kotlin-compiler这类仅SDK内部使用的依赖设置为compileOnly,避免将它的传递依赖暴露给应用项目。比如SDK的build.gradle中:
dependencies { compileOnly 'org.jetbrains.kotlin:kotlin-compiler:1.9.22' // 其他SDK依赖按需调整 }
这样SDK编译时会用到kotlin-compiler,但不会把它和它的依赖打包或传递给应用,从根源避免冲突。
内容的提问来源于stack exchange,提问作者Doublementi
相关产品推荐
相关产品推荐

