Getting Gradle dependencies in IntelliJ IDEA using Gradle build
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'):
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.
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)
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:
Related videos on Youtube
Andrew
Updated on November 04, 2021Comments
-
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 over 9 yearsImport the project as a gradle project, make sure you have gradle support enabled.
-
-
Peter Niederwieser over 9 years
apply plugin: 'idea'
should be added in any case. -
tddmonkey over 9 years@PeterNiederwieser - if you're doing an import from Idea, what effect does it have?
-
Peter Niederwieser over 9 yearsIt 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 over 9 yearsthanks, nice to know, I exclusively use gradle to generate the project files for me so never needed to do this
-
Peter Niederwieser over 9 yearsSame holds when generating project files, except that applying the plugin is definitely not optional in that case.
-
AuBee about 8 yearsBut 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 about 8 yearsWorked for me. Thankfully, even without using the idea gradle plugin.
-
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 almost 8 years@Thomas Yes. CLI always has more control
-
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 about 7 yearsActually, this answer solved my problem and should be the first thing you do prior to the actual correct answer above.
-
user219882 almost 7 yearsThis is the only solution that actually works and is correct. Adding something into gradle.properties just for idea is plain wrong.
-
Ed Norris over 6 yearsI 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 about 6 yearsThe 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 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 over 5 years@user219882 - why is adding something to your build "plain wrong"?
-
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 over 5 yearsDo 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 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 over 5 yearsI 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 over 5 yearsSorry, 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 about 5 yearsA 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 almost 5 yearsThe 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 about 3 yearsAfter refreshing the dependencies, I had to do "Invalidate caches / restart" also. Then it worked fine
-
Stefan Collier almost 3 yearsPerfect solution on Intellij 2020.3!