Gradle dependencies

Add or update dependencies in your build.gradle file.

Let's look at Gradle dependencies in a bit more detail. We can add a new dependency upon an external library with ⌘N (macOS) / Alt+Insert (Windows/Linux), select Add Maven artifact dependency. You can now locate your dependency in the tool window.

IntelliJ IDEA will insert the new dependency, usually with the implementation configuration:

dependencies {
    implementation("log4j:log4j:1.2.17")
    testImplementation(platform("org.junit:junit-bom:5.10.3"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

We should change this if we need to, for example if this needs to be testImplementation. IntelliJ IDEA tells us the build file has been changed, and that we need to reload these changes if we want the project to use them.

Build file changed

We can do this by:

  • Selecting one of the suggestions in the message;
  • Pressing on the Refresh Gradle icon in the top right,
  • Using the keyboard shortcut ⌘⇧I (macOS) / Ctrl+Shift+O (Windows/Linux).

Once IntelliJ IDEA has reloaded our updated build file with the new dependency, this new dependency will appear in our external libraries in the project window.

Alternatively, we may want to use IntelliJ IDEA's code completion for adding a new dependency. We get code completion in the build.gradle.kts file the way we would in all supported file types.

So we could add a new testImplementation dependency by starting to type the details. IntelliJ IDEA will even complete the group and artifact name. Try this out, by adding a new dependency on the junit-jupiter-params library. Once again you'll need to load these changes, e.g. with ⌘⇧I (macOS) / Ctrl+Shift+O (Windows/Linux).

Top Tip

If you find you're not getting code completion for your libraries, or the results you expect in your Maven artifact search, there's a trick to help with that. Open the Settings/Preferences dialog, go to Build, Execution, Deployment | Build Tools | Maven | Repositories.

Indexed Maven Repositories

You'll see IntelliJ IDEA has a list of indexed repositories, and the date it was last indexed. Selecting one, or all, of these and pressing "Update" usually helps IntelliJ IDEA to give good suggestions for artifacts.

Next, we'll see how IntelliJ IDEA knows about Gradle source set directories, and how that helps us.