如何查看Gradle项目的依赖树情况-排查 Unable to merge dex问题

一、问题描述
今天在编写代码过程中,引入了一个组内新的封装库,导致编译冲突。如下所示:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':demo:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 8s
51 actionable tasks: 13 executed, 38 up-to-date
1
2
3
4
5
6
7
8
9
10
11
12
13
点击 【Run with --stacktrace】 ,重新编译查看详细信息,如下:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':demo:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

* Try:
Run with --info or --debug option to get more log output.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':demo:transformDexArchiveWithExternalLibsDexMergerForDebug'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
    at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
    at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:60)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)
    at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)
    at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:626)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:581)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
    at com.android.builder.dexing.DxDexArchiveMerger.mergeMonoDex(DxDexArchiveMerger.java:177)
    at com.android.builder.dexing.DxDexArchiveMerger.mergeDexArchives(DxDexArchiveMerger.java:118)
    at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:97)
    at com.android.build.gradle.internal.transforms.ExternalLibsMergerTransform.transform(ExternalLibsMergerTransform.kt:121)
    at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:222)
    at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:218)
    at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:102)
    at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:213)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
    at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:46)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:121)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:110)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)
    ... 29 more
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
    at com.android.builder.dexing.DxDexArchiveMerger.lambda$mergeMonoDex$0(DxDexArchiveMerger.java:171)
Caused by: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
Caused by: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
    at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:72)
    at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:36)
Caused by: com.android.dex.DexException: Multiple dex files define Landroid/support/v4/app/TaskStackBuilder$SupportParentable;
    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 6s
51 actionable tasks: 5 executed, 46 up-to-date

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83


告诉我因为 Multiple dex files define Landroid/support/v4/app/TaskStackBuilder$SupportParentable; 有多个 相同的 TaskStackBuilder 类导致编译不了的。

其他的support包都是27.1.1,这个support-v4-23.2.1,从哪里来的?

我们代码的build.gradle文件中,都搜索不到23.2.1 是从哪里来的。我的代码中根本就没有地方引用这个版本的support-v4-23.2.1,那么我们该如何查找这个这个版本的support-v4-23.2.1到底是在哪里引入的呢???

二、如何快速查看Gradle项目包依赖情况?
通过上面的问题,我们想查找这个这个版本的support-v4-23.2.1到底是在哪里引入的,就必须得把项目的gradle依赖树的情况获知,如何获知android项目的gradle依赖呢??可以通过以下几种方式来获知。

2.1 执行命令 ./gradlew 模块名:dependencies
比如我的项目是 demo工程编译失败的,所以我们执行命令

gradlew :demo:dependencies
1


执行命令之后的输出日志如下所示,因为日志太多,截取部分:

+--- com.xtc.data:data-new-sqlcipher:1.0.2
|    +--- net.zetetic:android-database-sqlcipher:3.5.7
|    \--- com.j256.ormlite:ormlite-core:4.48
\--- com.xtc.phone:data-phone:1.1.8
     +--- com.android.support:appcompat-v7:27.1.1 (*)
     +--- com.android.support:support-annotations:27.1.1
     +--- io.reactivex:rxandroid:1.2.1 (*)
     +--- com.facebook.fresco:fresco:0.12.0
     |    +--- com.facebook.fresco:drawee:0.12.0
     |    |    +--- com.android.support:support-v4:23.2.1
     |    |    |    \--- com.android.support:support-annotations:23.2.1 -> 27.1.1
     |    |    \--- com.facebook.fresco:fbcore:0.12.0
     |    +--- com.facebook.fresco:fbcore:0.12.0
     |    \--- com.facebook.fresco:imagepipeline:0.12.0
     |         +--- com.android.support:support-v4:23.2.1 (*)
     |         +--- com.facebook.fresco:imagepipeline-base:0.12.0
     |         |    +--- com.android.support:support-v4:23.2.1 (*)
     |         |    +--- com.parse.bolts:bolts-tasks:1.4.0
     |         |    +--- com.nineoldandroids:library:2.4.0
     |         |    \--- com.facebook.fresco:fbcore:0.12.0
     |         +--- com.parse.bolts:bolts-tasks:1.4.0
     |         +--- com.nineoldandroids:library:2.4.0
     |         \--- com.facebook.fresco:fbcore:0.12.0
     +--- com.facebook.fresco:animated-gif:0.12.0
     |    +--- com.android.support:support-v4:23.2.1 (*)
     |    +--- com.parse.bolts:bolts-tasks:1.4.0
     |    +--- com.facebook.fresco:fbcore:0.12.0
     |    \--- com.facebook.fresco:animated-base:0.12.0
     |         +--- com.android.support:support-v4:23.2.1 (*)
     |         +--- com.facebook.fresco:imagepipeline-base:0.12.0 (*)
     |         +--- com.parse.bolts:bolts-tasks:1.4.0
     |         +--- com.facebook.fresco:fbcore:0.12.0
     |         \--- com.facebook.fresco:imagepipeline:0.12.0 (*)
     \--- com.facebook.fresco:imagepipeline-okhttp3:0.12.0
          +--- com.squareup.okhttp3:okhttp:3.0.1 -> 3.4.1 (*)
          +--- com.facebook.fresco:fbcore:0.12.0
          \--- com.facebook.fresco:imagepipeline:0.12.0 (*)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38


由上面的依赖树我们可以看出来,

com.android.support:support-v4:23.2.1 被 com.facebook.fresco:fresco:0.12.0、com.facebook.fresco:animated-gif:0.12.0引用了。而这两个库又被com.xtc.phone:data-phone:1.1.8引用了。

所以我们在引用com.xtc.phone:data-phone:1.1.8的地方,将com.android.support:support-v4:23.2.1exclude掉即可正常编译。如下所示:

 implementation libraries.data_phone
1
修改为以下即可。

    //implementation libraries.data_phone
    //data_phone里面的fresco包含了com.android.support:support-v4:23.2.1
    implementation (libraries.data_phone){
       exclude group: 'com.android.support', module: 'support-v4'
    }
1
2
3
4
5


如果是直接依赖本地的project的话,也是可以进行exclude,只是需要在project 外面再包一层,写成如下所示:

implementation  project(':phone')
1
改为

 //data_phone里面的fresco包含了com.android.support:support-v4:23.2.1
    implementation  (project(':phone')){
        exclude group: 'com.android.support', module: 'support-v4'
    }
1
2
3
4


重新编译,查看下依赖关系。

\--- com.xtc.phone:data-phone:1.1.8
     +--- com.android.support:appcompat-v7:27.1.1 (*)
     +--- com.android.support:support-annotations:27.1.1
     +--- io.reactivex:rxandroid:1.2.1 (*)
     +--- com.facebook.fresco:fresco:0.12.0
     |    +--- com.facebook.fresco:drawee:0.12.0
     |    |    \--- com.facebook.fresco:fbcore:0.12.0
     |    +--- com.facebook.fresco:fbcore:0.12.0
     |    \--- com.facebook.fresco:imagepipeline:0.12.0
     |         +--- com.facebook.fresco:imagepipeline-base:0.12.0
     |         |    +--- com.parse.bolts:bolts-tasks:1.4.0
     |         |    +--- com.nineoldandroids:library:2.4.0
     |         |    \--- com.facebook.fresco:fbcore:0.12.0
     |         +--- com.parse.bolts:bolts-tasks:1.4.0
     |         +--- com.nineoldandroids:library:2.4.0
     |         \--- com.facebook.fresco:fbcore:0.12.0
     +--- com.facebook.fresco:animated-gif:0.12.0
     |    +--- com.parse.bolts:bolts-tasks:1.4.0
     |    +--- com.facebook.fresco:fbcore:0.12.0
     |    \--- com.facebook.fresco:animated-base:0.12.0
     |         +--- com.facebook.fresco:imagepipeline-base:0.12.0 (*)
     |         +--- com.parse.bolts:bolts-tasks:1.4.0
     |         +--- com.facebook.fresco:fbcore:0.12.0
     |         \--- com.facebook.fresco:imagepipeline:0.12.0 (*)
     \--- com.facebook.fresco:imagepipeline-okhttp3:0.12.0
          +--- com.squareup.okhttp3:okhttp:3.0.1 -> 3.4.1 (*)
          +--- com.facebook.fresco:fbcore:0.12.0
          \--- com.facebook.fresco:imagepipeline:0.12.0 (*)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29


移除了重复类之后,正常编译。

2.2 通过Android Studio右侧的Gradle面板上的dependencies命令
如果上面的命令方式你不习惯,你可以通过Android Studio右侧的Gradle面板上的dependencies命令执行,如下所示

如上图,找到【 demo】工程,展开,然后找到【help】目录,找到【dependencies】命令,点击即可执行命令。如下所示

2.3 执行 gradlew build --scan 命令,生成
如果你嫌在命令行窗口展示观看不友好,这里还有一种体验更好的方式。

输入下面命令行:gradlew build --scan

编译完成的时候,出现输入框,提示我们输入 yes或者no

Do you accept the Gradle Cloud Services license agreement (https://gradle.com/terms-of-service)? [yes, no]
1


我们输入yes,然后继续

Do you accept the Gradle Cloud Services license agreement (https://gradle.com/terms-of-service)? [yes, no]
yes
Gradle Cloud Services license agreement accepted.

Publishing build scan...
https://gradle.com/s/paiitjk7aaiqa

C:\CodeForAndroid\BigData>

1
2
3
4
5
6
7
8
9


我们打开上面生成的链接: https://gradle.com/s/paiitjk7aaiqa,然后登陆网站即可。

登陆网站,然后查看具体的信息,如下所示:

登陆邮箱,查看刚才收到的邮件,如下所示:


2.4 通过 gradle view 插件查看gradle依赖树
如下图所示,在Settings --> Plugins中,搜索 gradle view插件并安装,下图是我已经安装好的截图。


搜索不到的,可以直接去 http://plugins.jetbrains.com 网站搜索,然后下载


http://plugins.jetbrains.com/plugin/7150-gradle-view 下载,如下所示


第一次安装完后,需要重启Android Studio ,重启之后我们来使用gradle view 插件

如上图,找到View–>Tools Windows-Gradle View ,然后点击即可执行。

三 总结
上面我介绍了四种查看Gradle项目依赖树的情况的方法,你们可以自己选择自己喜欢的。个人觉得插件还是最好用的。

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:https://blog.csdn.net/ouyang_peng/article/details/82590820

如果本文对您有所帮助,欢迎您扫码下图所示的支付宝和微信支付二维码对本文进行打赏。


--------------------- 
作者:欧阳鹏 
来源:CSDN 
原文:https://blog.csdn.net/ouyang_peng/article/details/82590820 ,https://blog.csdn.net/u013394527/article/details/76852664
版权声明:本文为博主原创文章,转载请附上博文链接!

转载于:https://my.oschina.net/sfshine/blog/2996463

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值