Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

java - How to find/remove unused dependencies in Gradle

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

  1. Removes unused dependencies.
  2. Promotes transitive dependencies that are used directly by your code to explicit first order dependencies.
  3. 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

  1. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...