如何在Gradle中查找/删除未使用的依赖项 [英] How to find/remove unused dependencies in Gradle

查看:177
本文介绍了如何在Gradle中查找/删除未使用的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中找到未使用的依赖项.像Maven一样,在Gradle中有此功能吗?

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

推荐答案

更新:28-06-2016:Android对未使用依赖项的支持

2017年6月中,他们发布了4.0.0 version 并将根项目名称"gradle-lint-plugin"重命名为 "nebula-lint-plugin".他们还向 Android支持中添加了 未使用的依赖项.

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.


2016年5月中,Gradle实施了 Gradle lint插件查找和删除不需要的依赖项


In May 2016 Gradle has implemented the Gradle lint plugin for finding and removing unwanted dependency

Gradle Lint插件是一个可插入且可配置的Linter工具,用于 识别并报告滥用或弃用方式 Gradle脚本和相关文件.

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. 删除未使用的依赖项.
  2. 促进代码直接使用的传递依赖项 明确的一阶依赖关系.
  3. 将依赖项重新定位为正确"配置.
  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.

要应用规则,请添加:

gradleLint.rules += 'unused-dependency'

未使用的依赖规则的详细信息为在最后一部分中给出.

Details of Unused Dependency Rule is given in the last part.

要应用Gradle lint插件:

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

或者:

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

apply plugin: 'nebula.lint'

定义您要反对的规则:

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

对于企业构建,建议在init.gradle脚本或通过Gradle apply from机制包含的Gradle脚本中定义皮棉规则.

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.

对于多模块项目,我们建议在allprojects块中应用插件:

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'

该规则检查源于项目的源集外观的已编译二进制文件用于类引用,并将那些引用与您在依赖项中声明的依赖项进行匹配a>阻止.

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.

  • 像com.amazonaws:aws-java-sdk这样的家庭风格罐子被删除了,因为 他们不包含任何代码
  • Family-style jars like com.amazonaws:aws-java-sdk are removed, as they don't contain any code
  • 这具有破坏家庭风格的JAR文件的副作用,例如 com.amazonaws:aws-java-sdk,进入您实际所在的部分 使用,并将其添加为一阶依赖项
  • 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
  • 将Webjar移至运行时配置
  • 不包含META-INF以外的任何内容的
  • JAR文件是 移至运行时
  • "xerces","xercesImpl","xml-apis"应始终在运行时范围内
  • 服务提供者(包含META-INF/服务的JAR文件)例如 如果没有任何可证明的信息,mysql-connector-java将移至运行时 编译时参考
  • 相关性已移至最高源集配置 可能的.例如,除非将"junit"重定位到testCompile,否则 在主要来源集中(稀有)对此有明确的依赖性.
  • 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).

对于您的信息,我想分享一下以前的插件

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

  1. 查找未声明的依赖项(已声明和可传递的)的Gradle插件为 com. github.nullstress.dependency-analysis

但是其最新版本1.0.3创建于2014年12月23日.之后,没有任何更新.

But its latest version 1.0.3 is created 23 December 2014. After that there aren't any updates.

NB:我们的许多工程师对此插件感到困惑 仅更新了版本号,没有其他任何内容.

N.B: Many of our engineers are being confused about this plugin as they updated only the version number, nothing else.

这篇关于如何在Gradle中查找/删除未使用的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆