1.在对应模块的gradle文件中增加以下任务
2.执行该任务,点击任务旁边的run或者执行命令 gradlew 模块名:dpsTask
3.到根目录dependencies.csv
task dpsTask {
doLast {
booleanisExecute =false
//仅application生效,lib请使用getLibraryVariants
android.getApplicationVariants().all {
if(isExecute){
return
}
isExecute =true
String name = it.getName();//name为构建变体组合,根据实际情况获取
//debugCompileClasspath
Configuration dependenciesConfiguration = getProject().getConfigurations().getByName(name +"CompileClasspath");
Action attributes =newAction() {
@Override
voidexecute(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(newAction() {
@Override
voidexecute(org.gradle.api.artifacts.ArtifactView.ViewConfiguration viewConfiguration) {
viewConfiguration.lenient(true)
viewConfiguration.attributes(attributes);
}
}).getArtifacts();
Set
resolvedArtifactResults = artifactCollection.getArtifacts(); String path_code = rootProject.rootDir.getAbsolutePath() +"/dependencies.csv";
Writer fileWriter =newFileWriter(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(newConsumer() {
@Override
voidaccept(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()
}
}
}