有时候引入了新的sdk后,build会出现如下问题:
Caused by: com.android.dex.DexException: Multiple dex files define Lcom/google/gson/internal/bind/TypeAdapters; at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:661) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:616) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:598) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171) at com.android.dx.merge.DexMerger.merge(DexMerger.java:198) at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:61) ... 1 more * Get more help at https://help.gradle.org BUILD FAILED in 10s 81 actionable tasks: 8 executed, 73 up-to-date
这里的意思就是说,重复的dex文件出现在了TypeAdapters这个类了。
简单的说就是重复依赖或者依赖冲突或者Jar包冲突了。
解决方案
除了删除冲突包外,我们还可以用Gradle的 exclude group 将指定的包名排除到编译范围外,如下所示:
implementation ('cn.bmob.android:bmob-sdk:3.5.5'){ // gson-2.6.2 exclude group: 'com.squareup.okhttp3' exclude group: 'com.squareup.okio' exclude group: 'com.google.code.gson' //exclude(module: 'gson') // 防止版本冲突 }
Android获取所有依赖库的几种方式
方式一:通过dependencies命令
./gradlew :app:dependencies
该task会显示如下所示的输出:
输出列表展示了所有configuration下的依赖树,依赖关系明显,层次清晰。如果觉得输出的结果太冗长(通常情况下包含几十个configuration),可以通过指定configuration来显示特定的依赖树:
./gradlew :app:dependencies --configuration releaseCompileClasspath
该命令只会显示release模式下编译过程中的依赖树。
方式二: 通过androidDependencies命令
./gradlew :app:androidDependencies
输出结果如下:
如图所示,该task会平铺展示依赖树,并且只展示几个主要的variant,看起来较为清爽,但是缺点是不能像方式一那样指定configuration。
方式三:自定义task获取
project.afterEvaluate { project.android.applicationVariants.all { variant -> tasks.create(name: "showDependencies${variant.name.capitalize()}", description: "展示所有依赖") { doLast { Configuration configuration try { // 3.x configuration = project.configurations."${variant.name}CompileClasspath" } catch (Exception e) { // 2.x configuration = project.configurations."_${variant.name}Compile" } configuration.resolvedConfiguration.lenientConfiguration.allModuleDependencies.each { def identifier = it.module.id println("${identifier.group}:${identifier.name}:${identifier.version}") } } } } }
如上,通过这种自定义task的方式,可以选择打印依赖,也可以选择保存到文件中,灵活度最高。
总结:
方式一:通用task,按层次展示依赖树,可以通过指定configuration来过滤输出。
方式二:android项目特有的task,平铺展示依赖树,不能过滤输出结果。
方式三:自定义task获取依赖,灵活度最高,但是需要对gradle有较深的理解。
解决重复依赖的方法
1.Program type already present: android.support.design.widget.CoordinatorLayout$1
需要将所有support包中的design模块移除
implementation('com.android.support:appcompat-v7:27.1.0', { exclude group: 'com.android.support', module: 'design' }) implementation('com.android.support:recyclerview-v7:27.1.0', { exclude group: 'com.android.support', module: 'design' }) implementation('com.android.support:cardview-v7:27.1.0', { exclude group: 'com.android.support', module: 'design' }) implementation('com.android.support:customtabs:27.1.0', { exclude group: 'com.android.support', module: 'design' })
统一design包的版本,与以上support包版本一致
implementation 'com.android.support:design:27.1.0'
接着Sync → Clean → Build apk 即可。
解决依赖主要有两种方式
exclude方式
特点:
配置较为麻烦,需要在引起冲突的每个依赖上进行exclude操作
配置繁琐,不美观
下面的方式也是可以的。
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2' implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') { exclude(group: 'com.google.android', module: 'support-v4') } /* 或者粗暴点,就没有上面的坑了 implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') { exclude module: 'support-v4' } */
通过configurations方式
特点:
在configurations中,统一指定要配置的方式
配置简单,较为整洁
找到依赖的问题根源后进行排除,按提示报错的来灵活处理冲突问题!下面的方式也是可以的。
configurations { all*.exclude group: 'com.google.android', module: 'support-v4' //或者粗暴点,就没有上面的坑了 all*.exclude module: 'support-v4' }
通过configurations.all同一版本
configurations.all { resolutionStrategy { force "com.android.support:support-v4:$android_support_version" force "com.android.support:appcompat-v7:$android_support_version" force "com.android.support:constraint-layout:$android_support_version" force "com.android.support:support-core-utils:$android_support_version" force "com.android.support:exifinterface:$android_support_version" force "com.google.code.gson:gson:2.7" } }
发表评论 取消回复