788

Is it possible to use Gradle to produce a tree of what depends on what?

I have a project and would like to find out all the dependencies so I may be able to prune it a little with forward declarations etc.

informatik01
  • 16,038
  • 10
  • 74
  • 104
user3286701
  • 7,963
  • 3
  • 12
  • 4
  • 4
    possible duplicate of [what is gradle artifact dependency graph command?](http://stackoverflow.com/questions/12288133/what-is-gradle-artifact-dependency-graph-command) – Oliver Charlesworth Feb 08 '14 at 11:44

16 Answers16

913

Without modules:

gradle dependencies

For Android:

 gradle app:dependencies

Using gradle wrapper:

./gradlew app:dependencies

Note: Replace app with the project module name.

Additionally, if you want to check if something is compile vs. testCompile vs androidTestCompile dependency as well as what is pulling it in:

./gradlew :app:dependencyInsight --configuration compile --dependency <name>
./gradlew :app:dependencyInsight --configuration testCompile --dependency <name>
./gradlew :app:dependencyInsight --configuration androidTestCompile --dependency <name>
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
  • 1
    ah, 'app' is a submodule of your current gradle project I guess. Turns out I didn't read this in your answer: "where app is your project module." Thanks for the reply! – krock Mar 08 '18 at 23:41
  • see also how to achieve that with android studio help https://developer.android.com/studio/build/dependencies.html#view-dependency-tree – sweetrenard Mar 16 '18 at 09:42
  • @ChadBingham : i try your suggestion but it's didn't work :( https://stackoverflow.com/questions/49646103/why-gradlew-appdependencyinsight-failed – zeus Apr 04 '18 at 08:35
  • 3
    I also didn't know the project module (this legacy project has a really weird structure). But this command helped me `./gradlew -q dependencies :dependencies` It looks on all your folders :P – Alberto Maluje Oct 23 '18 at 13:21
  • 12
    In fact `./gradlew :dependencies` will work. It will list the dependencies for all your projects, but it is easy to find the right one. – dirkjot Nov 27 '18 at 21:29
  • @Jenix it depends on the version of your ./gradlew and gradle are running.If ./gradlew has a higher version it can probably have more tasks. – Bachiri Taoufiq Abderrahman Feb 02 '19 at 12:25
  • 2
    On an non-Android project with Gradle wrapper `./gradlew dependencies` works (while `./gradlew app:dependencies` gives me the following failure / exception "Project 'app' not found in root project 'spring-petclinic'." – Marit Sep 07 '22 at 08:13
  • This does not show the dependency tree (transitive dependencies). It just lists out the direct implementations. – Pratzz Sep 14 '22 at 19:57
  • It's not clear how to read this report, what does \--- mean (compared to +---)? – Jesse Barnum Jan 09 '23 at 21:29
  • WRT Windows Android Studio, requires JAVA_HOME env var which can be Android Studio's C:\Program Files\Android\Android Studio\jbr – steve Jun 28 '23 at 14:49
  • @Marit It's because `app` must be replaced with the **your** project _module name_ (unless you indeed have a module named 'app'). – informatik01 Aug 29 '23 at 02:29
281

You can render the dependency tree with the command gradle dependencies. For more information check the section Listing dependencies in a project in the online user guide.

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Benjamin Muschko
  • 32,442
  • 9
  • 61
  • 82
210

If you find it hard to navigate console output of gradle dependencies, you can add the Project reports plugin:

apply plugin: 'project-report'

For a multimodule project (most of projects), you need to configure the report to include the subprojects, too:

htmlDependencyReport {
    projects = project.allprojects
}

And generate a HTML report using:

$ ./gradlew htmlDependencyReport

Report can normally be found in build/reports/project/dependencies/index.html

It looks like this: enter image description here

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
Devstr
  • 4,431
  • 1
  • 18
  • 30
90

In Android Studio (at least since v2.3.3) you can run the command directly from the UI:

Click on the Gradle tab and then double click on :yourmodule -> Tasks -> android -> androidDependencies

The tree will be displayed in the Gradle Console tab

An image is worth a thousand words

58

Often the complete testImplementation, implementation, and androidTestImplementation dependency graph is too much to examine together. If you merely want the implementation dependency graph you can use:

./gradlew app:dependencies --configuration implementation

Source: Listing dependencies in a project

Note: compile has been deprecated in more recent versions of Gradle and in more recent versions you are advised to shift all of your compile dependencies to implementation. Please see this answer here

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
David Rawson
  • 20,912
  • 7
  • 88
  • 124
32

If you want to visualize your dependencies in a graph you can use gradle-dependency-graph-generator plugin.

Generally the output of this plugin can be found in build/reports/dependency-graph directory and it contains three files (.dot|.png|.svg) if you are using the 0.5.0 version of the plugin.

Example of dependences graph in a real app (Chess Clock):

graph

David Miguel
  • 12,154
  • 3
  • 66
  • 68
  • 1
    Hey David, I did as was told in the plugin instructions. I applied plugin in project gradle but I am not seeing any reports folder in build directory. Is there anything we need to do other than build and run the project? – Rushi M Thakker Feb 04 '19 at 11:17
  • This plugin not show dependency version and conflicts. like this for Maven; https://github.com/janssk1/maven-graph-plugin/wiki/Manual This make it useless.. – sytolk Oct 17 '19 at 14:17
  • * What went wrong: Execution failed for task ':generateDependencyGraph'. > Cannot invoke "com.kitfox.svg.SVGDiagram.setIgnoringClipHeuristic(boolean)" because "diagram" is null – Andres Camilo Sierra Hormiga Mar 22 '23 at 19:23
31

For me, it was simply one command

in build.gradle add plugin

apply plugin: 'project-report'

and then go to cmd and run following command

./gradlew htmlDependencyReport

This gives me an HTML report WOW Html report


Or if you want the report in a text file, to make search easy use following command

gradlew dependencyReport

enter image description here

That's all my lord.

Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61
25

Things have moved forward in Gradle so I believe this question merits another answer.
Since Gradle 4.3, "build scans" were introduced. All relevant info is available in the Gradle docs (1, 2). For me, this seems to now be the easiest way to check your dependencies (and generally your build) in a clear, organized way.

They are very easy to create, just execute:

gradle build --scan  

(or ./gradlew build --scan if you use a wrapper)

This produces a randomly generated link where you can see your scan. When opening that link, you enter your email and gain full control of the link: eg. share it or delete it. It has got a lot of info about your build, not just dependencies. You can see your dependencies, their hierarchies, the repository used to obtain them but also a lot of other stuff about your build, namely, its performance (which is of interest in big complex builds), your tests, even your console output and your system configuration, which JDK and JVM was used, max heap size etc.

This is a printscreen from a mock project:

Build scan example

A build scan is a shareable record of a build that provides insights into what happened and why. You can create a build scan at scans.gradle.com for free.

Note however, that info for your build process will be sent to the Gradle servers. You have full control to delete it when you are finished with your inspection.

Finally, you can use build scans with Gradle versions earlier than 4.3 too, you just have to manually add the scans plugin in your buildscript.

Edit:
Incorporating some feedback from the comments some extra notes:
1) It is very difficult to do this by mistake or without understanding that some info for your build will be online (private to you, with the ability to delete it, but still online).

When executing gradle build --scan the following message appears:

Publishing a build scan to scans.gradle.com requires accepting the Gradle
Terms of Service defined at https://gradle.com/terms-of-service. Do you
accept these terms? [yes, no]

You have to explicitly write yes and then the message continues:

Publishing build scan...  
https://gradle.com/s/a12en0dasdu

2) In Gradle Enterprise you can host gradle build scans in your own servers. However I have no experience in this and my proposed approach was about the standard Gradle distribution, using Gradle's servers for your build scans.

3) Gradle itself promotes the build scans as the way to deal with most your build problems.

tryman
  • 3,233
  • 1
  • 12
  • 23
  • 26
    Sorry to downvote the answer, but IMO it's not answering the question and it's not warning clearly enough that by `gradle build --scan` you publish details of your build somewhere on internet - and to delete it you have to associate it with working e-mail address. – Michal Feb 08 '19 at 15:50
  • 3
    Hey Michal, you are welcome to downvote if you feel that way. However I have to ask, did you try it? When executing `gradle build --scan` in cmd, you are prompted: `Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https://gradle.com/terms-of-service. Do you accept these terms? [yes, no]`. You have to manually write `yes`. Just after that, you get the message: `Publishing build scan... https://gradle.com/s/a12en0dasdu` (randomly put link of the same type). So I believe it does warn you appropriately before. (1/2) – tryman Feb 08 '19 at 17:24
  • 3
    You can visit the link and delete the scan immediately, without an email or anything. As an answer, I believe it **does answer the question**. In fact, it shows you extra info about your dependencies (and going the extra mile: extra info for the whole build as well) in comparison to other approaches. If you don't find it to your liking that's totally okay, but it is (to my knowledge) the most comprehensive tool right now for the job, so it's valuable to have it in mind. Finally, it's promoted by Gradle itself, it is literally their [first guide](https://gradle.org/guides/) (2/2) – tryman Feb 08 '19 at 17:36
  • @Michal Sorry, forgot to tag you before so you may have not seen my comments. ( I also edited the post to incorporate the comments with which I answered to you. ) – tryman Feb 08 '19 at 22:13
  • 4
    I haven't tried that - thanks for clarifying about the accepting of terms. I removed my -1. – Michal Feb 09 '19 at 21:19
25

For Android, type this in terminal

gradlew app:dependencies

It will list all the dependencies and the ones with newer versions for you to upgrade like

com.android.support:customtabs:26.1.0 -> 27.1.1 (*)
Remy
  • 788
  • 1
  • 9
  • 14
  • 4
    No , I'm afraid the part about `(*)` is wrong. `(*)` means this dependency declares to use the former(26.1.0) but actually build will use the latter version(27.1.1) [this answer](https://stackoverflow.com/a/34231202/9798666) talked about this. – wkm Sep 06 '20 at 04:04
  • @wkm exactly. The OP is wrong. – Michał Dobi Dobrzański Dec 14 '21 at 08:17
17

For recent versions of Gradle (I tested with the 6.4.1 version):

gradle dependencies --configuration compileClasspath

or if you're using the Gradle Wrapper:

gradlew dependencies --configuration compileClasspath

When building for Android with the 'debug' and 'release' compilation profiles, the debugCompileClasspath and releaseCompileClasspath configurations can be used instead of compileClasspath.

Soufiane Sakhi
  • 809
  • 10
  • 13
13

I also found useful to run this:

./gradlew dI --dependency <your library>

This shows how are being dependencies resolved (dependencyInsight) and help you debugging into where do you need to force or exclude libraries in your build.gradle

See: https://docs.gradle.org/current/userguide/tutorial_gradle_command_line.html

Christian Vielma
  • 15,263
  • 12
  • 53
  • 60
  • i try this like you say but it's didn't work :( https://stackoverflow.com/questions/49646103/why-gradlew-appdependencyinsight-failed – zeus Apr 04 '18 at 08:27
11

In Android Studio

1) Open terminal and ensure you are at project's root folder.

2) Run ./gradlew app:dependencies (if not using gradle wrapper, try gradle app:dependencies)

Note that running ./gradle dependencies will only give you dependency tree of project's root folder, so mentioning app in above manner, i.e. ./gradlew app:dependencies is important.

8

Note that you may need to do something like ./gradlew <module_directory>:<module_name>:dependencies if the module has extra directory before reach its build.gradle. When in doubt, do ./gradlew tasks --all to check the name.

林果皞
  • 7,539
  • 3
  • 55
  • 70
4

If you want all the dependencies in a single file at the end within two steps. Add this to your build.gradle.kts in the root of your project:

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

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

}

Then apply:

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

This will give you all the dependencies in your project and sub-projects along with all the 3rd party dependencies.

If you want to get this done in a programmatic way, then you'll need a custom renderer of the dependencies - you can start by extending the AsciiDependencyReportRenderer that prints an ascii graph of the dependencies by default.

codeWhisperer
  • 1,302
  • 1
  • 12
  • 10
  • This is interesting, as the first answer which touches on doing it programatically. I've been struggling trying to programatically walk the tree using project.configurations.qdependency.allDependencies.each{} but this doesn't loop over the 3rd party dependencies. – Calvin Taylor Mar 25 '22 at 13:49
3

double click and run dependency under help in gradle view

enter image description here

upog
  • 4,965
  • 8
  • 42
  • 81
3

Try either this

./gradlew dependencies > ~/dependencies.txt

or

gradle dependencies > ~/dependencies.txt`

which should write the dependencies in text file under user's home directory.

Musa
  • 3,944
  • 4
  • 21
  • 27