Andrew
Andrew

Reputation: 3028

Getting Gradle dependencies in IntelliJ IDEA using Gradle build

Grade build, even from inside IntelliJ IDEA does not put the dependencies into the "External Libraries" folder, so these classes don't show up as suggestions in the editor and when I manually add them as an import there is a compile error.

How can I get IntelliJ to automatically incorporate the dependencies in my build.gradle file, for instance:

compile 'com.google.code.gson:gson:1.7.2

Upvotes: 183

Views: 256058

Answers (10)

Sascha Doerdelmann
Sascha Doerdelmann

Reputation: 836

In my case the root project name did not match the root project folder name.

If I check out some git repository, I typically use snake_case. From time to time people set a root project name and use a different spelling e.g. CamelCase.

Example:

  • Folder to check out to: my_project
  • rootProject.name = 'MyProject'

Whenever this happened, IntelliJ was not able to calculate the external libraries.

I deleted the line with the rootProject.name from the gradle.settings file and everything worked fine.

Upvotes: 0

Beezer
Beezer

Reputation: 1108

Andrew's above post is still valid for the latest version of Intellij as of 3rd Quarter of 2017. So use it. 'Cause, build project, and external command line gradle build, does NOT add it to the external dependencies in Intellij...crazy as that sounds it is true. Only difference now is that the UI looks different to the above, but still the same icon for updating is used. I am only putting an answer here, cause I cannot paste a snapshot of the new UI...I dont want any up votes per se. Andrew still gave the correct answer above: enter image description here

Upvotes: 8

Mang Ndie
Mang Ndie

Reputation: 288

in my case, just re-import the jar file into correct directory . File->project Structure->[Project setting -> Modules] enter image description here

Upvotes: 1

akshaynagpal
akshaynagpal

Reputation: 3137

I had this exact same error and nothing else worked. Finally, I did the following:

  1. Close IntelliJ IDEA.
  2. Delete .idea directory from my project folder.
  3. Reopen IntelliJ and import the project again (as Gradle).

After the above, any new gradle dependency I added to build.gradle started appearing in External Dependencies section when I clicked the gradle refresh button.

Upvotes: 6

reinaldoluckman
reinaldoluckman

Reputation: 6348

Tried everything in this thread and nothing worked for me in IntelliJ 2020.2. This answer did the trick, but I had to set the correct path to the JDK and choose it in Gradle settings after that (as showed in figures bellow):

  1. Setting the correct path for the Java SDK (under File->Project Structure):

enter image description here

  1. In Gradle Window, click in "Gradle Settings..."

enter image description here

  1. Select the correct SDK from (1) here:

enter image description here

After that, the option "Reload All Gradle Projects" downloaded all dependencies as expected.

Cheers.

Upvotes: 4

Saurabh
Saurabh

Reputation: 7833

In my case,I was getting error while refreshing gradle ('View'->Tool Windows->Gradle) tab and hit "refresh" and getting this error no such property gradleversion for class jetgradleplugin.

Had to install latest intellij compatible with gradle 5+

Upvotes: 1

Jingxuan Zhou
Jingxuan Zhou

Reputation: 321

For those who are getting the "Unable to resolve dependencies" error:
Toggle "Offline Mode" off
('View'->Tool Windows->Gradle)

gradle window

Upvotes: 22

Andrew
Andrew

Reputation: 37969

After adding dependencies open "Gradle" ('View'->Tool Windows->Gradle) tab and hit "refresh"

example of adding (compile 'io.reactivex:rxjava:1.1.0'):

hit refresh

If Idea still can not resolve dependency, hence it is possibly the dependency is not in mavenCentral() repository and you need add repository where this dependency located into repositories{}

Upvotes: 264

When importing an existing Gradle project (one with a build.gradle) into IntelliJ IDEA, when presented with the following screen, select Import from external model -> Gradle.

Import project from external model

Optionally, select Auto Import on the next screen to automatically import new dependencies.

Upvotes: 23

tddmonkey
tddmonkey

Reputation: 21184

You either need to import the project as a Gradle project from within Idea. When you add a dependency you need to open the Gradle window and perform a refresh.

Alternatively generate the project files from gradle with this:

build.gradle:

apply plugin: 'idea'

And then run:

$ gradle idea

If you modify the dependencies you will need to rerun the above again.

Upvotes: 208

Related Questions