引入com.googlecode.json-simple后出现Duplicate class类重复错误如何解决?
问题原因
- 你的Gradle配置中首先存在两处重复的JUnit依赖声明:
testImplementation 'junit:junit:4.+'与testImplementation 'junit:junit:4.12',同时androidTestImplementation 'androidx.test.ext:junit:4.10'内部也集成了hamcrest相关类 com.googlecode.json-simple:json-simple:1.1.1会传递依赖org.hamcrest:hamcrest-core:1.1,和JUnit携带的hamcrest类产生重合,因此只有在引入json-simple时才会触发类重复报错
解决方案
按以下步骤修改即可解决冲突:
- 清理重复的JUnit依赖:删除其中一条testImplementation的JUnit声明,建议统一保留稳定版本,比如
testImplementation 'junit:junit:4.13.2' - 引入json-simple时排除自带的hamcrest依赖,将原有的json-simple依赖语句替换为:
implementation ('com.googlecode.json-simple:json-simple:1.1.1') { exclude group: 'org.hamcrest', module: 'hamcrest-core' }
- 同步Gradle配置后重新构建项目即可正常编译
如果不需要使用json-simple,也可以直接替换为Gson、Jackson等其他无冲突的JSON处理库,同样可以解决该问题。
内容的提问来源于stack exchange,提问作者remov
相关产品推荐
相关产品推荐

