1.正常的module引用aar文件需要配置如下:
repositories {
flatDir {
dirs 'libs'
}
} // 此在android标签下添加
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile(name: 'aar名字', ext: 'aar')
}
2.当一个library类型的module需要引用aar文件时,也要在所在模块的build.gradle文件中加入上面的话,否则会报找不到aar的错误。
但是当一个A Module引用此library的module时,也需要在A的build.gradle中加入如下配置:
repositories {
flatDir {
dirs 'libs', '../模块名/libs'
}
}
3.但若是在多个model中引入aar文件,除了和正常的aar的引入方式外,我们还需要在application所在模块的build.gradle文件中加入如下一段:
repositories {
flatDir {
dirs 'libs', '../模块名/libs'
}
}
4.引入新的 jar 包后,有时会伴随着 duplicate class 引入问题,比如 gson 包重复的报错如下:
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
因为在项目的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。如图:
可以发现确实是有两个重复了,那么现在就来解决这个问题。
解决方法:
在项目中找到External Libraries里面的converter-gson-2.1.0。如图:
打开pom.xml,查看里面gson的groupId。如图:
找到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。
完成之后项目就可以正常运行了!
参考链接:
http://blog.csdn.net/qq_20872573/article/details/72818758
http://blog.csdn.net/mvpstevenlin/article/details/57075112