duplicate entry: com/google/gson/FieldNamingPolicy$5.class

duplicate entry: com/google/gson/FieldNamingPolicy$5.class

1.问题说明

在使用gson中的时候,出现如下错误:

Execution failed for task ‘:app:transformClassesWithJarMergingForRelease’.
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/google/gson/FieldNamingPolicy$5.class

原因是导入的包有所重复,有可能是jar包和modules中的重复,我这里就是这种情况,在多次重新编译时,还有可能会产生下面的问题:

What went wrong:
Execution failed for task ‘:app:transformClassesWithMultidexlistForDebug’.
java.io.IOException: Can’t write [/Users/jk/WebStormProject/MYProjectf/android/app/build/intermediates/multi-dex/debug/componentClasses.jar] (Can’t read [/Users/jk/.gradle/caches/modules-2/files-2.1/com.google.code.gson/gson/2.8.5/f645ed69d595b24d4cf8b3fbb64cc505bede8829/gson-2.8.5.jar(;;;;;;**.class)] (Duplicate zip entry [gson-2.8.5.jar:com/google/gson/annotations/SerializedName.class]))

但是解决思路都是一样的,就是移除掉多余的gson包,再解决的过程中,我查了很多文章,但是,其中包括这篇文章duplicate entry: com/google/gson/annotations/Expose.class报错解决 ,上面这种用法可能对于之前版本的有效,在我使用时,会提示如下错误:

A problem occurred evaluating project ':app'.  
> Could not find method com.google.code.gson:gson:2.8.5() for arguments [build_c79q8dkccdz0f2ed8wg8e6062$_run_closure2$_closure18@4fd99d88] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.  

所以没有很好的解决掉我的问题,在我的版本中,我使用了如下方法进行移除多余的gson.

2 .解决方案:

在github上找到了一个解决方案:
1.issue-1
2.issue-2
在路径android/app/build.gradle android中添加如下代码:

   configurations{
             all*.exclude module:'gson'  //Remove duplicate dependencies: gson
     }

同样的,在移除support-v4的时候,也可以使用下面的方法

        configurations{
            all*.exclude module:'support-v4'  //Remove duplicate dependencies: support-v4
        }
3. 下面也是一种做法

使用 Multidex support library 开启 Multidex 功能
步骤 1:更改 build.grade

defaultConfig {
    ...
    // Enabling multidex support.
    multiDexEnabled true
}

编辑日期:2018-10-28
感谢omkn对我问题的解答

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

suwu150

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值