One of the most highly-anticipated Gradle features has just arrived in Gradle 2.12: support for declaring compile-only dependencies. For Java developers familiar with Maven, compile-only dependencies function similarly to Maven’s
provided
scope, allowing you to declare non-transitive dependencies used only at compilation time. While a similar capability has been available for users of the Gradle War Plugin, compile-only dependencies can now be declared for all Java projects using the Java Plugin.Nov 19, 2017 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 3 specific characteristics. Removes unused dependencies. Unused imports have no performance impact at runtime. Apart from code readability and hence maintainability of code, there may be faster compilation of java code (however, unnoticeable) by tidying up imports, but runtime performance is not impacted, since byte code generated is. Oct 30, 2020 Dependency analysis plugin for gradle. This plugin attempts to replicate the functionality of the maven dependency plugin's analyze goals which fail the build if dependencies are declared but not used or used but not declared. Jul 07, 2020 In this short tutorial, we'll cover how to use the Maven dependency plugin, a plugin that helps us find unused dependencies in our project. Let's begin by adding a couple of dependencies, slf4j-api (the one we will be using) and common-collections (the one we won't use): dependencies commons-collections commons-collections 3.2.2 org.slf4j.
![Remove Remove](/uploads/1/1/8/9/118936172/383325567.png)
Compile-only dependencies address a number of use cases, including:
Gradle See Dependencies
- Dependencies required at compile time but never required at runtime, such as source-only annotations or annotation processors;
- Dependencies required at compile time but required at runtime only when using certain features, a.k.a. optional dependencies;
- Dependencies whose API is required at compile time but whose implementation is to be provided by a consuming library, application or runtime environment.
Compile-only dependencies are distinctly different than regular
compile
dependencies. They are not included on the runtime classpath and they are non-transitive, meaning they are not included in dependent projects. This is true when using Gradle project dependencies and also when publishing to Maven or Ivy repositories. In the latter case, compile-only dependencies are simply omitted from published metadata.As part of our commitment to quality IDE support, compile-only dependencies continue to work with Gradle’s IDEA and Eclipse plugins. When used within IntelliJ IDEA, compile-only dependencies are mapped to IDEA’s own
provided
scope. Within Eclipse, compile-only dependencies are not exported via project dependencies.In the Gradle model we consider tests to be a “consumer” of the production code. With this in mind, compile-only dependencies are not inherited by the test classpath. The intention is that tests, like any other runtime environment, should provide their own implementation, either in the form of mocks or some other dependency.
Declaring compile-only dependencies is simple—just assign dependencies to the new
compileOnly
configuration for the appropriate source set:As a result of the addition of the
compileOnly
configuration, the compile
configuration no longer represents a complete picture of all compile time dependencies. When it’s necessary to reference a compile classpath in build scripts or custom plugins, the appropriate source set’s compileClasspath
property should be used instead.For more information, see the Java Plugin chapter of the Gradle user guide. And as always, we welcome your feedback and questions in the comments below or via the Gradle Forum at discuss.gradle.org.
Related Posts
- ❯ Introducing Java toolchains
- ❯ Introducing Configuration Caching
- ❯ Introducing file system watching
As you might know, by running $ ./gradlew dependencies gradle will print out the list of all libraries you are having at your build.gradle file and and all of their dependencies. You can find dependencies of a library easily if you are working on an small project however is not easy to find if you are working on an enterprise project or your project have different flavors. So the question is how to find (print out) dependencies of a particular dependency?
Assume that we have following libraries in our build.gradle file. Although, it doesn't show any problem and you get it compiled successfully, there is a small problem. Second library, material date picker, has support library as its dependency and it's good practice to exclude it from build process since we already defined it. Also, if your project is very big you will get 65K method limitation during compile and it suggest you to active multidex build if you don't exclude duplicated dependencies. Multidex build activation adds extra complexity into the project and not all versions of Android are compatible with.
Gradle Remove Unused Dependencies
In order to find which dependencies must be excluded we must know a particular library is depends on what libraries. We can do it by adding a configuration to our build.gradle file like following snippet. You can add it at the end of build.gradle file.
You can change the value of custom to whatever library you have in your main dependency and then run following command.
By doing this, gradle will print out dependencies of the library. So, you can remove it if the library has defined earlier for gradle. This is the output for our case:
Gradle Report Unused Dependencies
So you can go back to your main dependency and exclude support library from build process.