How to find/remove unused dependencies in Gradle

后端 未结 6 1015
花落未央
花落未央 2020-11-30 18:28

I wanted to find unused dependencies in my project. Is there a feature for this in Gradle, like in Maven?

6条回答
  •  失恋的感觉
    2020-11-30 18:29

    I've had a lot of luck using the Gradle Dependency Analysis Plugin. To get started with it, add the following two things to your Gradle build script.

    buildscript {
        repositories {
            maven {
                url "https://plugins.gradle.org/m2/"
            }
        }
        dependencies {
            classpath "com.github.nullstress:DependencyAnalysisPlugin:1.0.3"
        }
    }
    

    and

    apply plugin: "dependencyAnalysis"
    

    Once those are in place, run gradle analyze. If there are unused dependencies, you'll get a build failure that shows output similar to the text below, plus a list of the unused dependencies (both declared and transitive). The build failure is really handy if you want to enforce that there should be no unused dependencies via a CI build.

    :foo:analyze FAILED
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':foo:analyze'.
    > The project has unused declared artifacts
    

提交回复
热议问题