Using gradle to find dependency tree

后端 未结 14 1712
-上瘾入骨i
-上瘾入骨i 2020-11-22 03:20

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

相关标签:
14条回答
  • 2020-11-22 03:30

    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:

    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.

    0 讨论(0)
  • 2020-11-22 03:32

    For Android, use this line

     gradle app:dependencies
    

    or if you have a gradle wrapper:

    ./gradlew app:dependencies
    

    where app is your project module.

    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>
    
    0 讨论(0)
  • 2020-11-22 03:41

    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.

    0 讨论(0)
  • 2020-11-22 03:42

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

    apply plugin: 'project-report'
    

    And generate a HTML report using:

    $ ./gradlew htmlDependencyReport
    

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

    It looks like this:

    0 讨论(0)
  • 2020-11-22 03:43

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

    0 讨论(0)
  • 2020-11-22 03:44

    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

    0 讨论(0)
提交回复
热议问题