Getting Gradle dependencies in IntelliJ IDEA using Gradle build

203,150

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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

gradle window

Solution 5

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

Share:
203,150

Related videos on Youtube

Andrew
Author by

Andrew

Updated on November 04, 2021

Comments

  • Andrew
    Andrew over 2 years

    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

    • M. Deinum
      M. Deinum over 9 years
      Import the project as a gradle project, make sure you have gradle support enabled.
  • Peter Niederwieser
    Peter Niederwieser over 9 years
    apply plugin: 'idea' should be added in any case.
  • tddmonkey
    tddmonkey over 9 years
    @PeterNiederwieser - if you're doing an import from Idea, what effect does it have?
  • Peter Niederwieser
    Peter Niederwieser over 9 years
    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).
  • tddmonkey
    tddmonkey over 9 years
    thanks, nice to know, I exclusively use gradle to generate the project files for me so never needed to do this
  • Peter Niederwieser
    Peter Niederwieser over 9 years
    Same holds when generating project files, except that applying the plugin is definitely not optional in that case.
  • AuBee
    AuBee about 8 years
    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.
  • gMale
    gMale about 8 years
    Worked for me. Thankfully, even without using the idea gradle plugin.
  • Keyur Padalia
    Keyur Padalia almost 8 years
    @AuBee: I had the same problem. It went away when I built (just once) using Gradle from the command line.
  • Andrew
    Andrew almost 8 years
    @Thomas Yes. CLI always has more control
  • Andrew
    Andrew over 7 years
    @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{}
  • quickinsights
    quickinsights about 7 years
    Actually, this answer solved my problem and should be the first thing you do prior to the actual correct answer above.
  • user219882
    user219882 almost 7 years
    This is the only solution that actually works and is correct. Adding something into gradle.properties just for idea is plain wrong.
  • Ed Norris
    Ed Norris over 6 years
    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.
  • Georgios
    Georgios about 6 years
    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...
  • killjoy
    killjoy almost 6 years
    @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 ??
  • tddmonkey
    tddmonkey over 5 years
    @user219882 - why is adding something to your build "plain wrong"?
  • Qix - MONICA WAS MISTREATED
    Qix - MONICA WAS MISTREATED over 5 years
    @tddmonkey because the IDE a user chooses to use is irrelevant to describing how a build takes place.
  • tddmonkey
    tddmonkey over 5 years
    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.
  • Qix - MONICA WAS MISTREATED
    Qix - MONICA WAS MISTREATED over 5 years
    @tddmonkey idea project files do not belong in repositories. They are not required to describe the source code of a project.
  • tddmonkey
    tddmonkey over 5 years
    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.
  • Qix - MONICA WAS MISTREATED
    Qix - MONICA WAS MISTREATED over 5 years
    Sorry, I misunderstood. I thought you were talking about adding .idea/ files into your Git repository. Adding it to gradle.properties is another matter.
  • James O'Brien
    James O'Brien about 5 years
    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 Boutcher
    James Boutcher almost 5 years
    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.
  • kavinda
    kavinda about 3 years
    After refreshing the dependencies, I had to do "Invalidate caches / restart" also. Then it worked fine
  • Stefan Collier
    Stefan Collier almost 3 years
    Perfect solution on Intellij 2020.3!