0%

Gradle获取依赖aar包大小

参考

  • AbstractDependencyReportTask dependencies任务
  • AppClasspathCheckTask checkDebugClassPath任务
  • ApplicationTaskManager
  • VariantScopeImpl computeArtifactCollection 收集Artifact

TASK

  • 复制下面脚本到 miniapp Library项目的build.gradle中
  • 根目录执行
1
2
3

./gradlew :amap_bundle_miniapp:miniapp:dpsTask

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

task dpsTask {
doLast {
boolean isExecute = false
//仅library生效,application请使用getApplicationVariants
android.getLibraryVariants().all {
if(isExecute){
return
}
isExecute = true
String name = it.getName(); //name为构建变体组合,根据实际情况获取
//debugCompileClasspath
Configuration dependenciesConfiguration = getProject().getConfigurations().getByName(name + "CompileClasspath");

Action<AttributeContainer> attributes = new Action<AttributeContainer>() {
@Override
void execute(AttributeContainer container) {
//key : AndroidArtifacts.MODULE_PATH,AndroidArtifacts.ARTIFACT_TYPE
//AndroidArtifacts中type类型 aar,android-classes等,会帮我们过滤
// 配置有疑问 参考 VariantScopeImpl 中
//container.attribute(ARTIFACT_TYPE, "android-classes");
}
};


ArtifactCollection artifactCollection = dependenciesConfiguration.getIncoming()
.artifactView(new Action<org.gradle.api.artifacts.ArtifactView.ViewConfiguration>() {
@Override
void execute(org.gradle.api.artifacts.ArtifactView.ViewConfiguration viewConfiguration) {
viewConfiguration.lenient(true)
viewConfiguration.attributes(attributes);
}
}).getArtifacts();

Set<ResolvedArtifactResult> resolvedArtifactResults = artifactCollection.getArtifacts();

String path_code = rootProject.rootDir.getAbsolutePath() + "/dependencies.csv";
Writer fileWriter = new FileWriter(path_code)

fileWriter.write("dependencies");
fileWriter.write(",");
fileWriter.write("size")
fileWriter.write(",")
fileWriter.write("path")
// 如果觉得依赖项过多,可以过滤
// 获取本build.gradle对应项目的去除重复项的全部依赖
// dependenciesConfiguration.getIncoming().getResolutionResult().getAllComponents()
// 获取本build.gradle对应项目的去除不重复项的全部依赖
// dependenciesConfiguration.getIncoming().getResolutionResult().getAllDependencies()
// 获取本build.gradle中的写入的依赖,依赖的依赖请遍历获取
// dependenciesConfiguration.getIncoming().getResolutionResult().getRoot().getDependencies()

resolvedArtifactResults.forEach(new Consumer<ResolvedArtifactResult>() {
@Override
void accept(ResolvedArtifactResult resolvedArtifactResult) {
fileWriter.write("\n")
fileWriter.write(resolvedArtifactResult.getId().getComponentIdentifier().getDisplayName())
fileWriter.write(",")
fileWriter.write(resolvedArtifactResult.getFile().length()+"")
fileWriter.write(",")
fileWriter.write(resolvedArtifactResult.getFile().getAbsolutePath())
}
})

fileWriter.close()
}
}
}

输出

到根目录dependencies.csv

1
2
3
4
5
6
7
8
9
10
11

dependencies,size,path
com.google.code.gson:gson:2.7,231952,/Users/juneleo/.gradle/caches/modules-2/files-2.1/com.google.code.gson/gson/2.7/751f548c85fa49f330cecbb1875893f971b33c4e/gson-2.7.jar
com.android.support:recyclerview-v7:26.1.0,343863,/Users/juneleo/.gradle/caches/modules-2/files-2.1/com.android.support/recyclerview-v7/26.1.0/d0276ee3e2cc61823610236f7e1f76c700847226/recyclerview-v7-26.1.0.aar
com.android.support:support-core-ui:26.1.0,233096,/Users/juneleo/.gradle/caches/modules-2/files-2.1/com.android.support/support-core-ui/26.1.0/e306308d54052a1ded7bf9c2f5c2fdf5152a1f22/support-core-ui-26.1.0.aar
com.android.support:support-compat:26.1.0,636333,/Users/juneleo/.gradle/caches/modules-2/files-2.1/com.android.support/support-compat/26.1.0/1e1c8ffc01d277d8f01dfd11d5d2ce3a2af4b98c/support-compat-26.1.0.aar
com.android.support:support-annotations:26.1.0,24712,/Users/juneleo/.gradle/caches/modules-2/files-2.1/com.android.support/support-annotations/26.1.0/814258103cf26a15fcc26ecce35f5b7d24b73f8/support-annotations-26.1.0.jar
android.arch.lifecycle:runtime:1.0.0,9603,/Users/juneleo/.gradle/caches/modules-2/files-2.1/android.arch.lifecycle/runtime/1.0.0/30c60a8a357ee1321ffd0c9f08ef54b24045cd10/runtime-1.0.0.aar
android.arch.lifecycle:common:1.0.0,12366,/Users/juneleo/.gradle/caches/modules-2/files-2.1/android.arch.lifecycle/common/1.0.0/e414a4cb28434e25c4f6aa71426eb20cf4874ae9/common-1.0.0.jar
android.arch.core:common:1.0.0,11156,/Users/juneleo/.gradle/caches/modules-2/files-2.1/android.arch.core/common/1.0.0/a2d487452376193fc8c103dd2b9bd5f2b1b44563/common-1.0.0.jar