gradle 依赖树
Recently, in one of my projects, we were stuck at a very annoying exception IllegalStateException: WorkManager is already initialized . We were pretty confident on we have only initialized WorkManager only one time, then why Android Studio is throwing this exception.
最近,在我的一个项目中,我们陷入了一个非常烦人的异常IllegalStateException: WorkManager is already initialized 。 我们对只初始化WorkManager一次非常有信心,这就是Android Studio为什么抛出此异常的原因。
The initial hunch we had was there’s some other dependency in our project, which is using WorkManager and that’s why this conflict is popped up. We looked at the build.gradle file and realized that we haven’t added any such dependency in our project which might use WorkManager in any way.
我们最初的预感是项目中存在其他依赖项,这是使用WorkManager的原因,这就是为什么会弹出此冲突的原因。 我们查看了build.gradle文件,意识到我们没有在项目中添加任何可能以任何方式使用WorkManager依赖项。
Looking into different solutions like updating / removing dependencies, and plugins, and debugging the app line by line to see where and how it crashes, we simply couldn’t crack the problem. And that’s where it hit me suddenly.
研究不同的解决方案,例如更新/删除依赖关系和插件,以及逐行调试应用程序以查看崩溃的位置和方式,我们根本无法破解问题。 那就是它突然袭击我的地方。
Is it possible to view the whole dependency tree of our project?
是否有可能查看我们项目的整个依赖树?
Answer is yes. It is possible in Android Studio and even terminal too. This article is about different methods to see how you can view the whole dependency tree of your app.
答案是肯定的。 在Android Studio中甚至在终端中也可以。 本文介绍了有关如何查看应用程序的整个依赖关系树的不同方法。
The Gradle Dependency Tree or Graph can tell you the about all the dependencies being used in your app in hierarchical way. It can help you with the version resolutions of same dependencies.
Gradle依赖关系树或图可以告诉您有关在应用程序中以分层方式使用的所有依赖关系的信息。 它可以帮助您使用相同依赖项的版本解析。
For example, your app is using Retrofit version 2.1 but there’s some other dependency in your app which is using Retrofit version 2.2. So, Android Studio will try to upgrade the dependency to the latest version i-e 2.2 for your whole project. Now, if there’s the big difference between old version and new version and require refactoring changes, this can be a problem for your app.
例如,您的应用程序使用的是Retrofit版本2.1,但您的应用程序中使用的是Retrofit版本2.2。 因此,Android Studio将尝试将依赖项升级到整个项目的最新版本,即2.2。 现在,如果旧版本和新版本之间有很大差异,并且需要重构更改,那么这可能是您的应用程序遇到的问题。
Gradle dependency tree can help you detect these kinds of issues. It can also help you in detecting the dependencies which are not being used directly in your app but rather these are being added by nested dependencies.
Gradle依赖关系树可以帮助您检测此类问题。 它还可以帮助您检测未在应用程序中直接使用的依赖项,而是由嵌套的依赖项添加的依赖项。
Now, let’s see different ways to view the Android app’s Gradle dependency tree or graph.
现在,让我们看看查看Android应用程序的Gradle依赖关系树或图形的不同方法。
This is the most easy and accessible method directly through Android Studio. Click on the Gradle tab on the right side and then expand:yourmodule -> Tasks -> android . Finally, double click on androidDependenciesto run it.
直接通过Android Studio,这是最简单易用的方法。 单击在Gradle选项卡上的右侧,然后展开:yourmodule - > Tasks - > android 。 最后,双击androidDependencies来运行它。
The androidDependencies Gradle task in Android Studio Android Studio中的androidDependencies Gradle任务This task will print the whole graph in your Android Studio console like the image below:
此任务将在您的Android Studio控制台中打印整个图形,如下图所示:
The Gradle Dependency Tree printed in Android Studio Terminal 在Android Studio终端中打印的Gradle依赖关系树This is similar to 1st option. Only difference is that you will be doing it manually by running a command in terminal. You can use either Android Studio terminal or your own choice terminal at the root directory of your project. Simply enter this command.
这类似于第一个选项。 唯一的不同是,您将通过在终端中运行命令来手动执行此操作。 您可以在项目的根目录中使用Android Studio终端或自己选择的终端。 只需输入此命令。
./gradlew yourmodule:dependenciesThe yourmodule is the name of the module you want to see dependencies for. For example, for your app, you can use it as app .
yourmodule是要查看其依赖性的模块的名称。 例如,对于您的应用程序,您可以将其用作app 。
Additionally, if you want to check if something is compile, testCompile, androidTestCompile, implementation, testImplementation, or androidTestImplementation dependency as well, you can do so with the configuration parameter like this:
此外,如果您还想检查某些东西是否为compile , testCompile , androidTestCompile , implementation , testImplementation或androidTestImplementation依赖项,则可以使用以下configuration参数来执行此操作:
./gradlew app:dependencies --configuration implementation./gradlew app:dependencies --configuration testImplementation ./gradlew app:dependencies --configuration androidTestImplementationIf you don’t know about Gradle build scans, these are the “X-ray for your Gradle and Maven builds” according to their website. They were introduced in Gradle 4.3 plugin.
如果您不了解Gradle构建扫描,根据他们的网站,这些就是“您的Gradle和Maven构建的X射线”。 它们是在Gradle 4.3插件中引入的 。
Using Gradle Build Scans is probably the most easiest and clean way to analyze not only the dependencies of your project but also other build information.
使用Gradle Build Scans可能是最简单,最干净的方法,不仅可以分析项目的依赖关系,还可以分析其他构建信息。
All you have to do is add --scan at end of any Gradle command in terminal. For example, for viewing the whole dependency tree, you can use this command:
您所要做的就是在终端中任何Gradle命令的末尾添加--scan 。 例如,要查看整个依赖关系树,可以使用以下命令:
gradlew app:dependencies --scanAfter the command ends, you will be asked for a confirmation on whether the build report should be generated or not. Type yes and you will get a link to the report.
命令结束后,将要求您确认是否应生成构建报告。 键入yes ,您将获得该报告的链接。
Confirmation to generate Gradle Build report and the report link after typing “yes” 输入“是”后,确认生成Gradle Build报告和报告链接Open this URL and enter your email. And you will get access to your build in your inbox in a few minutes. Open the build and head for Dependencies tab, and you will see something like this:
打开此URL,然后输入您的电子邮件。 几分钟后,您将可以在收件箱中访问构建。 打开构建并转到“ 依赖项”选项卡,您将看到类似以下内容:
Gradle Dependency Tree in Build Scan Report 构建扫描报告中的Gradle依赖关系树You can see that this has many other options like Performance, Tests, Plugins etc to provide you detailed insights about your build in a very nice formatted HTML page.
您会看到它具有许多其他选项,例如性能,测试,插件等,可以在格式非常漂亮HTML页面中为您提供有关构建的详细见解。
If you don’t want to put email every time you compile your project and see the report in the Android Studio IDE, you can use Project Reports plugin. This requires a little code modification in your app’s build.gradle file. First, put this on top of your app’s build.gradle file:
如果您不想每次编译项目时都发送电子邮件并在Android Studio IDE中查看报告,则可以使用Project Reports插件 。 这需要在您应用的build.gradle文件中进行一些代码修改。 首先,将其放在应用程序的build.gradle文件build.gradle :
apply plugin: 'project-report'And after syncing your project, run this command in Android Studio terminal.
同步项目后,在Android Studio终端中运行此命令。
gradlew htmlDependencyReportThis will give you a local HTML file link in your console. Click on it and you will see a nicely formatted dependencies tree with expand/collapse options.
这将为您提供控制台中的本地HTML文件链接。 单击它,您将看到带有扩展/折叠选项的格式正确的依赖关系树。
Gradle Dependency Tree in HTML Report HTML报表中的Gradle依赖树There’s also this awesome third-party plugin created by Niklas Baudy which shows the visual dependency graph of your Android project like this:
还有一个由Niklas Baudy创建的很棒的第三方插件,它显示了Android项目的可视依赖图,如下所示:
Image Credits: https://github.com/vanniktech/gradle-dependency-graph-generator-plugin 图片来源: https : //github.com/vanniktech/gradle-dependency-graph-generator-pluginYou can learn how to generate this graph by following the instructions available on his Gradle Dependency Graph Generator Plugin library at the following link:
您可以通过以下链接了解其Gradle依赖关系图生成器插件库中可用的说明,以生成该图:
At the end, please Subscribe to my newsletter DroidUp to learn learn about the latest things, tips, and skills in Android development manually handcrafted and curated by Wajahat Karim.
最后,请订阅我的时事通讯DroidUp,以了解有关由Wajahat Karim手工制作和策划的Android开发中的最新知识,技巧和技能的信息。
If you liked this article, you can read my new articles below:
如果您喜欢这篇文章,可以在下面阅读我的新文章:
Originally published at https://wajahatkarim.com on March 9, 2020.
最初于 2020年3月9日 发布在 https://wajahatkarim.com 上。
翻译自: https://android.jlelse.eu/gradle-dependency-tree-819b68898a53
gradle 依赖树