Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Adding Dependencies to build.gradle file

Header Image

Ahoy there mateys! Welcome to our journey through Gradle. In our last article, we learned how to create a Gradle project. Now, it’s time to set sail and add some dependencies to our build.gradle file.

Dependencies are like your ship’s crew, you need them to get the job done. In programming, dependencies are external libraries or modules that your project needs to function.

Using Dependency Notation

To add a dependency to our build.gradle file, we use a special syntax called dependency notation.

dependencies {
    implementation 'group:artifact:version'
}

In this example, group refers to the organization or group that created the library, artifact is the name of the library, and version is the specific version of the library we want to use.

For example, let’s say we want to use the log4j library for our project. We can add it to our build.gradle file like this:

dependencies {
    implementation 'org.apache.logging.log4j:log4j-core:2.14.1'
}

That’s it! Now, our project has access to the log4j library.

Declaring Dependency Scopes

Hold on tight, there’s more to dependencies than just their notation. Dependencies also have a scope that determines how and where they are used in the project.

For example, the implementation scope is used to include dependencies that are required to compile and run the main source code of the project.

dependencies {
    implementation 'group:artifact:version'
}

There are other scopes available, such as testImplementation, which includes dependencies required to compile and run the test code.

dependencies {
    testImplementation 'group:artifact:version'
}

There’s also runtimeOnly, which includes dependencies that are required to run the application, but not to compile it.

dependencies {
    runtimeOnly 'group:artifact:version'
}

Declaring Dependency Scopes

As we’ve seen, dependencies have different scopes that determine how and where they are used in the project. By declaring the appropriate scope for each dependency, you can avoid including unnecessary dependencies and ensure that your project runs smoothly.

Here are some of the most common scopes:

  • implementation: This scope includes dependencies that are required to compile and run the main source code of the project.
  • api: This scope includes dependencies that are required to compile and run both the main source code and any code that depends on the project.
  • testImplementation: This scope includes dependencies that are required to compile and run the test code.
  • runtimeOnly: This scope includes dependencies that are required to run the application, but not to compile it.
dependencies {
    implementation 'group:artifact:version'
    api 'group:artifact:version'
    testImplementation 'group:artifact:version'
    runtimeOnly 'group:artifact:version'
}

Note that the api scope is a bit different from the others. It’s used to define dependencies that are part of the public API of your project. This means that any code that depends on your project will also need these dependencies.

Conclusion

By using dependency notation and declaring the appropriate scope for each dependency, you can ensure that your project has access to the external libraries and modules it needs, without including unnecessary dependencies.

In this article, we’ve covered how to add dependencies to the build.gradle file and how to declare their scopes. In our next article, we’ll dive into resolving dependency conflicts.

Until then, keep hoisting the Jolly Roger and happy coding!