天天看点

【完美解决系列】duplicate entry: com/google/gson/annotations/Expose.class

项目在引入Retrofit2时,运行项目时会报出以下错误:

Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/google/gson/annotations/Expose.class
           

仔细看了下错误报告,确定是gson这个库有重复导致的问题。

因为在项目的build.gradle中新加入

compile 'com.squareup.retrofit2:retrofit:2.1.0'
 compile 'com.squareup.retrofit2:converter-gson:2.1.0'
           

那么问题就确定了,因为引入了converter-gson:2.1.0所以出现的错误。

我们先来查找一下Expose.class这个类是有哪些重复使用的类库,在Android Studio中双击Shift键,打开搜索窗口,输入Expose.class。如图:

【完美解决系列】duplicate entry: com/google/gson/annotations/Expose.class

可以发现确实是有两个重复了,那么现在就来解决这个问题。

解决方法:

在项目中找到External Libraries里面的converter-gson-2.1.0。如图:

【完美解决系列】duplicate entry: com/google/gson/annotations/Expose.class
【完美解决系列】duplicate entry: com/google/gson/annotations/Expose.class

打开pom.xml,查看里面gson的groupId。如图:

【完美解决系列】duplicate entry: com/google/gson/annotations/Expose.class

找到gson的groupId后,我们就要把它去掉就可以了,现在就来把它去掉,步骤如下:

打开项目的build.gradle,把原来的compile ‘com.squareup.retrofit2:converter-gson:2.1.0’ 改成这样:

compile ('com.squareup.retrofit2:converter-gson:2.1.0'){
        exclude group : 'com.google.code.gson'
    }
           

其中com.google.code.gson就是gson的groupId。

完成之后项目就可以正常运行了!

继续阅读