How to find \ remove unused dependencies in gradle

I wanted to find unused dependencies in my project. Is there a function for this in gradle like in maven?

+54
java maven dependency-management gradle
Oct. 15 '13 at 10:57
source share
6 answers

UPDATE: 06/28/2016: Android support for unused dependency

In June 2017, they released 4.0.0 version and renamed the root project name "gradle-lint-plugin" to "nebula-lint-plugin" . They also added Android support for unused-dependency .




In May 2016, Gradle implemented the gradle lint plugin to find and remove unwanted dependencies.

gradle Lint Plugin: full documentation

The Gradle Lint plugin is a plug-in and customizable tool for handling the detection and reporting of abuse patterns or Gradle scripts and related files.

This plugin has different rules. An unused dependency rule is one of them. It has 3 specific characteristics.

  • Removes unused dependencies.
  • Promotes transitive dependencies that are used directly by your code to explicit first-order dependencies.
  • Translates dependencies into the β€œcorrect” configuration.

To apply the rule, add:

 gradleLint.rules += 'unused-dependency' 

Details The unused dependency rule is indicated in the last part.

Application of the Gradle lint plugin:

 buildscript { repositories { jcenter() } } plugins { id 'nebula.lint' version '0.30.2' } 

As an alternative:

 buildscript { repositories { jcenter() } dependencies { classpath 'com.netflix.nebula:gradle-lint-plugin:latest.release' } } apply plugin: 'nebula.lint' 

Determine which rules you want to use:

 gradleLint.rules = ['all-dependency'] // add as many rules here as you'd like 

To build an enterprise, we recommend defining the lint rules in the init.gradle script or in the Gradle script, which is enabled using Gradle using a mechanism.

For projects with several modules, it is recommended to use the plugin in the allprojects block:

 allprojects { apply plugin: 'nebula.lint' gradleLint.rules = ['all-dependency'] // add as many rules here as you'd like } 






Details An unused dependency rule is specified in this section.

To apply the rule, add:

 gradleLint.rules += 'unused-dependency' 

The rule checks the compiled binaries coming from your source sets project, looking for class references and compares these links with the dependencies that you specified in your dependency block.

In particular, the rule performs the following dependency changes:

1) Removes unused dependencies

  • Family-style banks, such as com.amazonaws: aws-java-sdk, are deleted because they do not contain code

2) Promotes transitive dependencies that your code directly uses for explicit first-order dependencies

  • This has the side effect of breaking up family-style bars like com.amazonaws: aws-java-sdk into the parts that you are actually using and adding them as first-order dependencies

3) Moves dependencies to the "correct" configuration

  • Webjars move to runtime configuration
  • Boxes that do not contain classes AND content outside META-INF are moved at run time
  • 'xerces', 'xercesImpl', 'xml-apis' should always be runtime regions
  • Service providers (banks containing META-INF / services), for example mysql-connector-java, move at run time if there is no compile-time provable
  • Dependencies move into the configuration with the highest source code possible. For example, "junit" is ported to testCompile if there is an explicit dependency on it in the main set of sources (rarely).






UPDATE: Previous plugins

For your kind information, I want to share with previous plugins

But the latest version 1.0.3 was created on December 23, 2014 . After this update, no.

NB: Many of our engineers are confused about this plugin because they only updated the version number of nothing.

+29
Jun 02 '16 at 5:16
source share

I was fortunate enough to use 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 they are installed, run gradle analyze . If there are unused dependencies, you will receive a build failure that shows a result similar to the text below, plus a list of unused dependencies (declared and transitive). Build failure is very convenient if you want to ensure that there are no unused dependencies using the 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 
+5
Jun 01 '15 at 19:49
source share

The design mentioned in earlier answers seems to be dead. I am using gradle-dependency-analyze . The 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 
+5
Oct 30 '15 at 13:25
source share

Projects for most of the historical answers are dead, but gradle-dependency-analyze seems alive at the time of this writing (the last fix was two days ago).

+1
Jun 01 '16 at 19:58
source share

This is not a built-in function, and I don't know a third-party plugin (but maybe there is one).

0
Oct. 15 '13 at 16:57
source share

You can try com.github.nullstress.dependency-analysis gradient plugin

Build a script fragment for all versions of Gradle:

 buildscript { repositories { jcenter() } dependencies { classpath "com.github.nullstress:DependencyAnalysisPlugin:1.0.3" } } apply plugin: "com.github.nullstress.dependency-analysis" 

Creating a script fragment for the new, incubation, plug-in 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: analysis"? ) In the Gradle forum about this.

0
Mar 15 '15 at 18:38
source share



All Articles