是否有可能使用Gradle生成一个什么依赖什么的树?

我有一个项目,想要找出所有的依赖,所以我可以用前向声明等修剪它一点。


当前回答

在Android Studio中(至少从v2.3.3开始),你可以直接从UI运行命令:

点击Gradle选项卡,然后双击:yourmodule -> Tasks -> android -> androidDependencies

该树将显示在Gradle Console选项卡中

其他回答

试试这两种方法

./gradlew dependencies > ~/dependencies.txt

or

gradle dependencies > ~/dependencies.txt`

这应该写在用户的主目录下的文本文件依赖。

对于最新版本的Gradle(我使用6.4.1版本进行测试):

gradle dependencies --configuration compileClasspath

或者如果你正在使用Gradle Wrapper:

gradlew dependencies --configuration compileClasspath

当使用'debug'和'release'编译配置文件为Android构建时,可以使用debugCompileClasspath和releaseCompileClasspath配置来代替compileClasspath。

如果您希望在最后的两个步骤内将所有依赖项放在一个文件中。 把这个添加到你的build.gradle.kts项目的根目录中:

project.rootProject.allprojects {
    apply(plugin="project-report")

    this.task("allDependencies", DependencyReportTask::class) {
        evaluationDependsOnChildren()
        this.setRenderer(AsciiDependencyReportRenderer())
    }

}

然后应用:

./gradlew allDependencies | grep '\-\-\-' | grep -Po '\w+.*$' | awk -F ' ' '{ print $1 }' | sort | grep -v '\{' | grep -v '\[' | uniq | grep '.\+:.\+:.\+'

这将为您提供项目和子项目中的所有依赖项以及所有第三方依赖项。

如果您希望以编程的方式完成此工作,那么您将需要一个自定义的依赖关系渲染器——您可以从扩展AsciiDependencyReportRenderer开始,它默认情况下打印依赖关系的ascii图。

我还发现运行这个很有用:

./gradlew dI --dependency <your library>

这显示了如何解析依赖关系(dependencyInsight),并帮助你调试到需要在build.gradle中强制或排除库的位置

参见:https://docs.gradle.org/current/userguide/tutorial_gradle_command_line.html

如果你发现很难导航控制台输出的gradle依赖项,你可以添加项目报告插件:

apply plugin: 'project-report'

并生成一个HTML报告使用:

$ ./gradlew htmlDependencyReport

Report通常可以在build/reports/project/dependencies/index.html中找到

它是这样的: