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对我问题的解答