android studio 引用Module或者第三方librar引起的jar冲突

前几天使用git共同开发项目。同事跟我分别依赖了Module,合并后我的没问题,对方各种报错,主要以jar冲突为主。

FAILURE: Build failed with an exception.* What went wrong:Execution failed for task':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: android/support/annotation/IdRes.class

其中主要是引用module 导致的v4包冲突,常用的okHttp,FastJson,GSON冲突。
先收集一下网上盛行的方法,总有一款适合你。

将任意一个Module中的jar依赖为compile files(‘your jar name’),其他需要依赖的地方改为provided files(‘your jar name’)并且删除compile fileTree(include: [‘*.jar’], dir: ‘libs)。即可。
这种方法部分人可行。
还有一种解决方式

all*.exclude group: 'com.google.android', module: 'android'
        all*.exclude group: 'com.google.android', module:'support-v4'
        all*.exclude group: 'asm', module: 'asm'
        all*.exclude group: 'com.google.code.gson'

使用 Multidex support library 开启 Multidex 功能

步骤 1:更改 build.grade

defaultConfig {

// Enabling multidex support.
multiDexEnabled true
}

dependencies {

compile ‘com.android.support:multidex:1.0.1’
}
步骤 2:设置 Application 类

public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
MultiDex.install(this);
}
}
步骤 3:更改 grade.properties

org.gradle.jvmargs=-XX:MaxHeapSize\=2048m -Xmx2048m

第三种方式引用Chauncey_1993
http://blog.csdn.net/cx1229/article/details/52786168
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
1
2
1
2
这时候我们可以发现

com/google/gson/annotations/Expose.class
1
1
最后面有一个Expose.class
我们可以通过Search Everywhere去定位这个类(默认快捷键是双击shift)。

全局搜索相同类

看,通过搜索的确发现了两个一模一样的类,而且来自不同的依赖,而重复的类库正是gson。此时再回想起我刚刚是不是添加过一个

compile (‘com.squareup.retrofit2:converter-gson:2.1.0’)
1
1
好了,找到问题的根源的。那我们现在如何去除相同的类库呢,很简单:
在AS中,选择以project显示项目,找到最下面的External Libraries,然后继续找,找到相对应的类库:

gson

如图依次点开之后发现了一个pom.xml,这里面就是关于这个jar的一些配置文件,点开之后长这个样子

pom.xml

往下找,发现了一个gson的引用

gson引用

没错,这个就是导致报错的直接原因。复制里面groupId,到你的app的build.gradle里,找到那个依赖,添加{exclude group: ‘com.google.code.gson’}
把这个groudId的引用去除掉。如下图所示

去除重复依赖

好了,到此问题就解决了,如果你有多个重复依赖的话,就按照本文所给出的方法,一个一个去重。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值