172

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

informatik01
  • 16,038
  • 10
  • 74
  • 104
Andrew
  • 2,898
  • 4
  • 25
  • 23

9 Answers9

252

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{}

Community
  • 1
  • 1
Andrew
  • 36,676
  • 11
  • 141
  • 113
  • 17
    But seems it doesn't work. I added a dependency and refreshed, but when I try to import the library either in XML or Java Source Code, IDEA can't resolve it. – AuBee Feb 20 '16 at 10:31
  • 3
    Worked for me. Thankfully, even without using the idea gradle plugin. – gMale Apr 14 '16 at 06:57
  • 2
    @AuBee: I had the same problem. It went away when I built (just once) using Gradle from the command line. – Thomas Jun 27 '16 at 09:33
  • @Thomas Yes. CLI always has more control – Andrew Jun 27 '16 at 17:26
  • @AuBee If Idea can not resolve dependency, hence it is possibly the dependency is not in mavenCentral() repository and you also need add repository into repositories{} – Andrew Jan 08 '17 at 09:32
  • The problem I have with this is that if one my modules has a JAR or directory dependency, then this dependency gets removed from that module as soon as click "Refresh all Gradle projects". I can't find a solution to that... – Georgios Mar 13 '18 at 09:33
  • @Andrey Thanks for the image. IJ Nav sure is mighty cryptic and unintuitive. But what I dont get it is, when Eclipse/Maven does this automatically, why is so retarded in IJ/Gradle ?? – killjoy Jun 30 '18 at 14:22
  • After refreshing the dependencies, I had to do "Invalidate caches / restart" also. Then it worked fine – kavinda Apr 11 '21 at 10:56
206

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.

tddmonkey
  • 20,798
  • 10
  • 58
  • 67
  • `apply plugin: 'idea'` should be added in any case. – Peter Niederwieser Dec 29 '14 at 19:47
  • @PeterNiederwieser - if you're doing an import from Idea, what effect does it have? – tddmonkey Dec 29 '14 at 19:52
  • It allows to configure various aspects of the IntelliJ setup (e.g. which JDK to use), which should be honored by the import. If your build doesn't have any such configuration, it may not be necessary to apply the plugin (I've never tried without). – Peter Niederwieser Dec 29 '14 at 20:04
  • thanks, nice to know, I exclusively use gradle to generate the project files for me so never needed to do this – tddmonkey Dec 29 '14 at 20:51
  • Same holds when generating project files, except that applying the plugin is definitely not optional in that case. – Peter Niederwieser Dec 29 '14 at 22:18
  • The key part of this answer is that once you do that, you need to open the generated .ipr file in IntelliJ, and _not_ re-import the Gradle file again. – James Boutcher Jun 27 '19 at 22:59
23

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.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
  • 3
    Actually, this answer solved my problem and should be the first thing you do prior to the actual correct answer above. – quickinsights Mar 13 '17 at 01:24
  • 3
    This is the only solution that actually works and is correct. Adding something into gradle.properties just for idea is plain wrong. – user219882 May 22 '17 at 14:19
  • I was unable to get syntax highlighting and auto-complete to work at all without performing this step on the original import. You might be able to fix it without re-importing but this is much easier, at least on a small project. – Ed Norris Aug 11 '17 at 16:10
  • @user219882 - why is adding something to your build "plain wrong"? – tddmonkey Oct 23 '18 at 19:42
  • @tddmonkey because the IDE a user chooses to use is irrelevant to describing how a build takes place. – Qix - MONICA WAS MISTREATED Nov 13 '18 at 17:20
  • Do you realise that adding `idea` to the build process doesn't force a choice of IDE? It just provides good support if you do choose that. You can add the eclipse plugin too. Or you want something else just use it. – tddmonkey Nov 14 '18 at 08:16
  • @tddmonkey idea project files do not belong in repositories. They are not required to describe the _source code_ of a project. – Qix - MONICA WAS MISTREATED Nov 20 '18 at 15:50
  • I agree with you. Adding the idea plugin just lets you _generate_ the project files, it doesn't mean you have to check them in. – tddmonkey Nov 20 '18 at 18:11
  • Sorry, I misunderstood. I thought you were talking about adding `.idea/` files into your Git repository. Adding it to gradle.properties is another matter. – Qix - MONICA WAS MISTREATED Dec 14 '18 at 15:28
  • A note on this - Importing gradle project like you would a maven project does not seem to work. You can't even say new project from existing sources. The only way to open a gradle project is to have nothing else open, open intellij, and it gives you option to import project. Then import the whole project (not the gradle file), and mark it as a gradle project as this answer describes. I'm about to abandon using intellij because of my constant frustrations with basic things like this. I hate eclipse, but at least it knows what type of project it's working in. – James O'Brien Mar 18 '19 at 19:46
22

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

gradle window

xskxzr
  • 12,442
  • 12
  • 37
  • 77
Jingxuan Zhou
  • 321
  • 2
  • 4
8

Andrey'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. Andrey still gave the correct answer above: enter image description here

Beezer
  • 1,084
  • 13
  • 18
4

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.

akshaynagpal
  • 2,965
  • 30
  • 32
3

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.

reinaldoluckman
  • 6,317
  • 8
  • 42
  • 50
1

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+

Saurabh
  • 7,525
  • 4
  • 45
  • 46
1

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

Mang Ndie
  • 278
  • 3
  • 6