本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
Android 查看并解决重复依赖
iamitnan · 2724浏览 · 发布于2019-08-13 +关注

有时候引入了新的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会显示如下所示的输出:

2019073118021464.png



输出列表展示了所有configuration下的依赖树,依赖关系明显,层次清晰。如果觉得输出的结果太冗长(通常情况下包含几十个configuration),可以通过指定configuration来显示特定的依赖树:


./gradlew :app:dependencies --configuration releaseCompileClasspath


该命令只会显示release模式下编译过程中的依赖树。


方式二: 通过androidDependencies命令


./gradlew :app:androidDependencies 


输出结果如下:

20190731180313271.png


如图所示,该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操作

  • 配置繁琐,不美观


20190802141353548.png

下面的方式也是可以的。


 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中,统一指定要配置的方式

  • 配置简单,较为整洁


20190802141543867.png

找到依赖的问题根源后进行排除,按提示报错的来灵活处理冲突问题!下面的方式也是可以的。


configurations {
   all*.exclude group: 'com.google.android', module: 'support-v4'
   //或者粗暴点,就没有上面的坑了  all*.exclude module: 'support-v4'
}

通过configurations.all同一版本

20190802141559112.png


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"
        }
    }

 


相关推荐

android下vulkan与opengles纹理互通

talkchan · 1174浏览 · 2020-11-23 10:37:39
Android 使用RecyclerView实现轮播图

奔跑的男人 · 2173浏览 · 2019-05-09 17:11:13
微软发布新命令行工具 Windows Terminal

吴振华 · 867浏览 · 2019-05-09 17:15:04
Facebook 停止屏蔽部分区块链广告

· 753浏览 · 2019-05-09 17:20:08
加载中

0评论

评论
分类专栏
小鸟云服务器
扫码进入手机网页