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

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


当前回答

没有模块:

gradle dependencies

为Android:

 gradle app:dependencies

使用gradle包装:

./gradlew app:dependencies

注意:将app替换为项目模块名。

此外,如果你想检查某个东西是否依赖于compile vs. testCompile vs. androidTestCompile,以及是什么把它拉进来的:

./gradlew :app:dependencyInsight --configuration compile --dependency <name>
./gradlew :app:dependencyInsight --configuration testCompile --dependency <name>
./gradlew :app:dependencyInsight --configuration androidTestCompile --dependency <name>

其他回答

如果您希望在最后的两个步骤内将所有依赖项放在一个文件中。 把这个添加到你的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图。

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

gradle dependencies --configuration compileClasspath

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

gradlew dependencies --configuration compileClasspath

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

如果你想在图形中可视化你的依赖关系,你可以使用gradle-dependency-graph-generator插件。

通常,这个插件的输出可以在build/reports/dependency-graph目录中找到,如果你使用的是0.5.0版本的插件,它包含三个文件(.dot|.png|.svg)。

真实应用(象棋时钟)中的依赖关系图示例:

注意,如果模块在到达build.gradle之前有额外的目录,您可能需要执行类似于./gradlew <module_directory>:<module_name>:dependencies的操作。如果有疑问,执行./gradlew任务——所有这些都是为了检查名称。

对于Android,在终端中输入这个

gradlew app:dependencies

它将列出所有的依赖项和更新版本的依赖项,供您升级

com.android.support:customtabs:26.1.0 -> 27.1.1 (*)