一 作用

因为 Gradle 的依赖发生冲突的时候, 会解决冲突, 默认是使用最高的版本, 但是也会因为 force 字段的关系选择较低的版本, 我们往往需要知道打到APK里面依赖的具体版本.

代码片段

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
project.getGradle().addBuildListener(new BuildListener() {
@Override
void buildStarted(Gradle gradle) {
}
@Override
void settingsEvaluated(Settings settings) {
}
@Override
void projectsLoaded(Gradle gradle) {
}
@Override
void projectsEvaluated(Gradle gradle) {
}
@Override
void buildFinished(BuildResult result) {
project.android.applicationVariants.each { variant ->
if (variant.getAssemble().state.executed) {
if (this.gradle.gradleVersion >= "4.1") {
// android plugin 3.0 以上
def runtime = project.configurations.create("d_analyse" + variant.runtimeConfiguration.name);
runtime.setCanBeConsumed(false);
runtime.extendsFrom(variant.runtimeConfiguration);
runtime.getAttributes().attribute(Attribute.of("artifactType", String.class), "jar")
variant.runtimeConfiguration.getAttributes().keySet().forEach({
runtime.getAttributes().attribute(it, variant.runtimeConfiguration.getAttributes().getAttribute(it));
})
println("${variant.name} analyse")
println("runtime dependency ")
println("------------------")
runtime.resolvedConfiguration.resolvedArtifacts.forEach({
def id = it.moduleVersion.id;
println(id.group + " " + id.name + " " + id.version)
})
println("------------------\n ")
def packageC = project.configurations.create("d_analyse_" + variant.compileConfiguration.name);
packageC.setCanBeConsumed(false);
packageC.extendsFrom(variant.compileConfiguration);
packageC.getAttributes().attribute(Attribute.of("artifactType", String.class), "jar")
variant.compileConfiguration.getAttributes().keySet().forEach({
packageC.getAttributes().attribute(it, variant.compileConfiguration.getAttributes().getAttribute(it));
})
println("package dependency ")
println("------------------")
packageC.resolvedConfiguration.resolvedArtifacts.forEach({
def id = it.moduleVersion.id;
println(id.group + " " + id.name + " " + id.version)
})
println("------------------\n ")
println("\n ")
} else {
// android plugin 3.0 以下
def appPlugin = project.plugins.findPlugin("com.android.application");
appPlugin.variantManager.variantDataList.forEach({
def variantDependencies = it.variantDependency;
if (variant.name == variantDependencies.name) {
println("${variant.name} analyse")
def runtime = project.configurations.create("d_analyse" + variantDependencies.compileConfiguration.name);
runtime.setCanBeConsumed(false)
runtime.extendsFrom(variantDependencies.compileConfiguration);
println("runtime dependency ")
println("------------------")
runtime.resolvedConfiguration.resolvedArtifacts.forEach({
def id = it.moduleVersion.id;
println(id.group + " " + id.name + " " + id.version)
})
println("------------------\n ")
def packageC = project.configurations.create("d_analyse" + variantDependencies.packageConfiguration.name);
packageC.setCanBeConsumed(false)
packageC.extendsFrom(variantDependencies.packageConfiguration);
println("package dependency ")
println("------------------")
packageC.resolvedConfiguration.resolvedArtifacts.forEach({
def id = it.moduleVersion.id;
println(id.group + " " + id.name + " " + id.version)
})
println("------------------\n ")
println("\n ")
}
})
}
}
}
}
});