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

- 30,738
- 21
- 105
- 131

- 8,474
- 7
- 22
- 24
-
Update 2022/08/17 - Gradle 7.2 - AS Chipmunk: I find this plugin works: https://github.com/autonomousapps/dependency-analysis-android-gradle-plugin – aldok Aug 17 '22 at 07:12
7 Answers
UPDATE for Kotlin Users: 17 December 2021: Detects missing or superfluous build dependencies in Kotlin projects : Version 1.0.9 (latest)
I have added 2 types of configuration for Kotlin users.
- Using the plugins DSL
- Using legacy plugin application
Using the plugins DSL:
plugins {
id("com.faire.gradle.analyze") version "1.0.9"
}
Using legacy plugin application:
buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("com.faire.gradle:gradle-kotlin-buildozer:1.0.9")
}
}
apply(plugin = "com.faire.gradle.analyze")
Resource Link:
- https://plugins.gradle.org/plugin/com.faire.gradle.analyze
- https://github.com/Faire/gradle-kotlin-buildozer
- https://discuss.gradle.org/t/detecting-unused-projects-dependencies/25522
UPDATE: 28-06-2016: Android support to unused-dependency
In June, 2017, they have released the
4.0.0 version
and renamed the root project name"gradle-lint-plugin"
to"nebula-lint-plugin"
. They have also added Android support to unused-dependency.
In May 2016 Gradle has implemented the Gradle lint plugin for finding and removing unwanted dependency
Gradle Lint Plugin: Full Documentation
The Gradle Lint plugin is a pluggable and configurable linter tool for identifying and reporting on patterns of misuse or deprecations in Gradle scripts and related files.
This plugin has various rules. Unused Dependency Rule is one of them. It has three specific characteristics.
- Removes unused dependencies.
- Promotes transitive dependencies that are used directly by your code to explicit first order dependencies.
- Relocates dependencies to the 'correct' configuration.
To apply the rule, add:
gradleLint.rules += 'unused-dependency'
Details of Unused Dependency Rule is given in the last part.
To apply the Gradle lint plugin:
buildscript { repositories { jcenter() } }
plugins {
id 'nebula.lint' version '0.30.2'
}
Alternatively:
buildscript {
repositories { jcenter() }
dependencies {
classpath 'com.netflix.nebula:gradle-lint-plugin:latest.release'
}
}
apply plugin: 'nebula.lint'
Define which rules you would like to lint against:
gradleLint.rules = ['all-dependency'] // Add as many rules here as you'd like
For an enterprise build, we recommend defining the lint rules in a init.gradle script or in a Gradle script that is included via the Gradle apply from mechanism.
For multimodule projects, we recommend applying the plugin in an allprojects
block:
allprojects {
apply plugin: 'nebula.lint'
gradleLint.rules = ['all-dependency'] // Add as many rules here as you'd like
}
Details of Unused Dependency Rule is given in this part
To apply the rule, add:
gradleLint.rules += 'unused-dependency'
The rule inspects compiled binaries emanating from your project's source sets looking for class references and matches those references to the dependencies that you have declared in your dependencies block.
Specifically, the rule makes the following adjustments to dependencies:
1. Removes unused dependencies
- Family-style jars like com.amazonaws:aws-java-sdk are removed, as they don't contain any code
2. Promotes transitive dependencies that are used directly by your code to explicit first order dependencies
- This has the side effect of breaking up family style JAR files, like com.amazonaws:aws-java-sdk, into the parts that you are actually using, and adding those as first order dependencies
3. Relocates dependencies to the 'correct' configuration
- Webjars are moved to the runtime configuration
- JAR files that don't contain any classes and content outside of META-INF are moved to runtime
- 'xerces', 'xercesImpl', 'xml-apis' should always be runtime scoped
- Service providers (JAR files containing META-INF/services) like mysql-connector-java are moved to runtime if there isn't any provable compile-time reference
- Dependencies are moved to the highest source set configuration possible. For example, 'junit' is relocated to testCompile unless there is an explicit dependency on it in the main source set (rare).
UPDATE: Previous plugins
For your kind information, I want to share about previous plugins
- The Gradle plugin that finds unused dependencies, declared and transitive is com.github.nullstress.dependency-analysis
But its latest version 1.0.3 is created 23 December 2014. After that there aren't any updates.
N.B: Many of our engineers are being confused about this plugin as they updated only the version number, nothing else.

- 28,384
- 14
- 74
- 132
-
I'm pretty sure this is not compatible with latest changes in Android/gradle. Instead of telling me which dependencies I can dispose of, the plugin tells me to add dependencies and replace other dependencies with the deprecated `compile` instead of `implementation`. – Giszmo Mar 03 '18 at 01:10
-
13This plugin sadly doesn't work with the kotlin dsl. They have no plans to support it. – snowe Feb 01 '19 at 18:09
-
4It also doesn't work with the new gradle configurations (eg: `implementation` and `api`), and even worse, recommends changing from the new ones to the old deprecated ones (eg: `compile`, `testCompile`, etc.). – Laurence Gonsalves Jun 14 '19 at 05:01
-
1@SkyWalker you seem to have not read my comment at all.... >This plugin sadly doesn't work with the kotlin dsl. They have no plans to support it. – snowe Jun 17 '19 at 03:54
-
For 2020: https://www.baeldung.com/gradle-finding-unused-dependencies – Christian Vielma Aug 04 '20 at 15:08
-
1Also, doesn't work for kotlin based gradle files (August 2020) – Christian Vielma Aug 05 '20 at 12:55
-
-
-
-
2`gradle-lint-plugin` or nebula-plugin doesnt work for Android project, (Source)[https://github.com/nebula-plugins/gradle-lint-plugin/issues/36#issuecomment-884790846] – Chetan Gupta Jul 14 '22 at 08:42
I just learned about this one: https://plugins.gradle.org/plugin/com.autonomousapps.dependency-analysis
From the looks it is under active development, but I haven't tested it yet.
Edit: Actually it is pretty awesome, it provides lots of advises (e.g. whether to use api vs implementation)

- 7,014
- 6
- 43
- 62
-
1I would go for this one, I've got so much better results, thanks – Mohammed Fathy Oct 07 '20 at 12:25
-
1Can confirm, this one is excellent, and unlike others also works for kotlin-based gradle files. – sigma1510 Apr 11 '22 at 07:19
The project mentioned in the earlier answers seem to be dead. I use gradle-dependency-analyze. Setup is simple:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'ca.cutterslade.gradle:gradle-dependency-analyze:1.0.3'
}
}
apply plugin: 'ca.cutterslade.analyze'
Then do:
$ gradle analyzeDependencies

- 1,275
- 1
- 13
- 20
-
6I am facing this error: 'Gradle sync failed: Task with name 'classes' not found in project' – Pawan Dec 05 '15 at 21:28
-
I get stackOverflowException doing that. No precise information what is causing this though. I think there might be a circular dependency issue here but it would be a great idea I the tool told me where. – SGal Jun 16 '16 at 12:41
-
2@Pawan this plugin doesn't work with Android projects, and this isn't going to change anytime soon. Proof: https://github.com/wfhartford/gradle-dependency-analyze/issues/18 – diesersamat Jun 19 '17 at 06:59
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

- 2,132
- 1
- 30
- 44
-
4I get "Execution failed for task ':app:analyze'. > Project does not have the java plugin applied." If I add "apply plugin: 'java'" then I'm told its not compatible with the existing android application plugin. any ideas? – waterlooalex Jul 05 '15 at 06:21
-
It sounds like it's something specific with the Android application plugin. I wish I could help, but I have no experience with Android! – jstricker Jul 06 '15 at 13:00
-
The plugin has bugs, like for instance when you have static call to a dependency, not taken into account. – ToYonos Dec 17 '15 at 09:23
-
Editor's Note: This answer is out of date. Please see the top answer.
You can try the com.github.nullstress.dependency-analysis Gradle plugin
Build script snippet for use in all Gradle versions:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.github.nullstress:DependencyAnalysisPlugin:1.0.3"
}
}
apply plugin: "com.github.nullstress.dependency-analysis"
Build script snippet for new, incubating, plugin mechanism introduced in Gradle 2.1:
plugins {
id "com.github.nullstress.dependency-analysis" version "1.0.3"
}
Also, there is a thread (Is there a Gradle equivalent of "mvn dependency:analyze"?) in the Gradle forum about this.

- 30,738
- 21
- 105
- 131

- 1,336
- 12
- 11
-
This plugin seems to be a dead project... at least with the current Gradle version. – cjstehno Sep 23 '15 at 19:07
The projects on most of the historical answers are dead, but gradle-dependency-analyze appears to be alive as of 2016-05-30.

- 30,738
- 21
- 105
- 131

- 1,655
- 11
- 26
It might not be exactly what you are looking for, but at least for me, the most comfortable way of doing this without adding any plugins on top or anything else is to highlight the gradle dependency, do a right-click, go to Git -> Show History For Selection and then go to the GitHub link to see what that dependency was added for, if it doesn't make sense anymore, just remove it.
Of course this still implies that you have to careful about it, but depending on the project it might be the fastest option.

- 3,717
- 3
- 39
- 66