Gradle is a build automation tool that is widely used for building software projects, especially in the Java and Android ecosystems. It provides a flexible and powerful build system that allows developers to automate the build process of their projects, including compiling source code, managing dependencies, running tests, packaging and deploying the software.
In Gradle, you can include dependencies in your project by declaring them in the build.gradle
file using the appropriate configuration. Here are the basic steps to include dependencies in a Gradle project:
- Open the
build.gradle
file for your project. - Find the
dependencies
block in the file. - Declare the dependencies you want to include by specifying the group, artifact, and version information for each dependency. For example:
dependencies {
implementation 'com.example:my-library:1.0.0'
testImplementation 'junit:junit:4.12'
runtimeOnly 'mysql:mysql-connector-java:8.0.23'
}
There are different ways to include dependencies in gradle, each way have their own benefits. You can ensure that only the necessary dependencies are included at each stage of your project’s lifecycle. This can help to reduce the size of your published artifact, improve build times, and ensure that your project’s dependencies are correctly managed.
- implementation
- api
- runtimeOnly
- compileOnly
- kapt
- testImplementation
Implementation
Dependencies declared in the implementation
configuration are hidden from the consumers of your module or library, and are not included in the classpath of the consuming project. This means that any transitive dependencies of the declared dependencies will not be included in the classpath of the consuming project. If you are developing a library, you should use the implementation
configuration for dependencies that you do not want to expose to the consumers of your library.
Example: Consider you are writing a wrapper over google cloud API.
google-myImplementation
and Internally using Google’s API, when a user uses implementation He can’t see the Google’s SDK.
api
Dependencies declared in the api
configuration are exposed to the consumers of your module or library, and are included in the classpath of the consuming project. This means that any transitive dependencies of the declared dependencies will also be included in the classpath of the consuming project. If you are developing a library, you should use the api
configuration for dependencies that you want to expose to the consumers of your library.
Example, consider the above case, if a user uses api to include our library he will have access to the all the dependency that is used inside the library.
runtimeOnly
Dependencies that are required only at runtime are those that are needed when your code is executed, but are not needed during compilation or testing. These dependencies may include database drivers, logging libraries, or other libraries that are needed only when your application is running. Such dependencies are declared in the runtimeOnly
configuration in Gradle
Example
runtimeOnly("org.postgresql:postgresql")
compileOnly
Dependencies that are required only during compilation time are those that are needed to compile your code, but are not required at runtime. These dependencies may include annotation processors, code generators, or other libraries that are needed only to generate code or validate your source code during compilation. Such dependencies are declared in the compileOnly
configuration in Gradle.
Example:
compileOnly("org.graalvm.nativeimage:svm")
This library is only required in build to create native build
kapt
This configuration is used to declare dependencies that are required for Kotlin annotation processing. It is similar to the annotationProcessor
configuration for Java annotation processing. The kapt
configuration is used in combination with the kotlin-kapt
plugin.
By declaring dependencies in the kapt
configuration, you can ensure that the required annotation processing libraries are available during compilation. The kotlin-kapt
plugin will automatically generate code based on the annotations in your Kotlin code and the dependencies declared in the kapt
configuration.
Note that the kapt
configuration should not be used for dependencies that are required at runtime, only for annotation processing during compilation.
testImplementation
This configuration is used to declare dependencies that are only required for testing your project’s code. These dependencies will not be included in the published artifact, and will only be used during testing.
Thank you!!