简体   繁体   English

使用 Gradle build 在 IntelliJ IDEA 中获取 Gradle 依赖项

[英]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.等级构建,即使从 IntelliJ IDEA 内部也不会将依赖项放入“外部库”文件夹中,因此这些类不会在编辑器中显示为建议,当我手动将它们添加为导入时,会出现编译错误。

How can I get IntelliJ to automatically incorporate the dependencies in my build.gradle file, for instance:如何让 IntelliJ 自动将依赖项合并到我的build.gradle文件中,例如:

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

After adding dependencies open "Gradle" ('View'->Tool Windows->Gradle) tab and hit "refresh"添加依赖项后,打开“Gradle”(“查看”->“工具窗口”->“Gradle”)选项卡并点击“刷新”

example of adding (compile 'io.reactivex:rxjava:1.1.0'):添加示例(编译'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{}如果 Idea 仍然无法解析依赖项,则可能是依赖项不在 mavenCentral() 存储库中,您需要将此依赖项所在的存储库添加到存储库中{}

You either need to import the project as a Gradle project from within Idea.您要么需要将项目作为 Gradle 项目从 Idea 中导入。 When you add a dependency you need to open the Gradle window and perform a refresh.添加依赖项时,您需要打开 Gradle 窗口并执行刷新。

Alternatively generate the project files from gradle with this:或者,使用以下命令从 gradle 生成项目文件:

build.gradle:构建.gradle:

apply plugin: 'idea'

And then run:然后运行:

$ gradle idea

If you modify the dependencies you will need to rerun the above again.如果您修改了依赖项,您将需要再次重新运行上述内容。

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 .将现有 Gradle 项目(带有build.gradle的项目)导入 IntelliJ IDEA 时,当出现以下屏幕时,选择Import from external model -> Gradle

从外部模型导入项目

Optionally, select Auto Import on the next screen to automatically import new dependencies.或者,在下一个屏幕上选择自动导入以自动导入新的依赖项。

For those who are getting the "Unable to resolve dependencies" error:对于那些收到“无法解决依赖关系”错误的人:
Toggle "Offline Mode" off关闭“离线模式”
('View'->Tool Windows->Gradle) ('查看'->工具窗口->Gradle)

渐变窗口

Andrey's above post is still valid for the latest version of Intellij as of 3rd Quarter of 2017. So use it.截至 2017 年第三季度,Andrey 的上述帖子仍然适用于最新版本的 Intellij。所以请使用它。 '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.因为,构建项目和外部命令行 gradle 构建,不会将其添加到 Intellij 中的外部依赖项中......这听起来很疯狂。 Only difference now is that the UI looks different to the above, but still the same icon for updating is used.现在唯一的区别是 UI 看起来与上面的不同,但仍然使用相同的更新图标。 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.我只是在这里给出一个答案,因为我无法粘贴新 UI 的快照……我本身不想要任何赞成票。 Andrey still gave the correct answer above:安德烈在上面仍然给出了正确答案: 在此处输入图像描述

Tried everything in this thread and nothing worked for me in IntelliJ 2020.2.尝试了该线程中的所有内容,但在 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):这个答案成功了,但我必须设置正确的 JDK 路径,然后在 Gradle 设置中选择它(如下图所示):

  1. Setting the correct path for the Java SDK (under File->Project Structure):为 Java SDK 设置正确的路径(在 File->Project Structure 下):

在此处输入图像描述

  1. In Gradle Window, click in "Gradle Settings..."在 Gradle 窗口中,单击“Gradle 设置...”

在此处输入图像描述

  1. Select the correct SDK from (1) here:从此处的 (1) 中选择正确的 SDK:

在此处输入图像描述

After that, the option "Reload All Gradle Projects" downloaded all dependencies as expected.之后,“重新加载所有 Gradle 项目”选项按预期下载了所有依赖项。

Cheers.干杯。

I had this exact same error and nothing else worked.我有这个完全相同的错误,没有其他工作。 Finally, I did the following:最后,我做了以下事情:

  1. Close IntelliJ IDEA.关闭 IntelliJ IDEA。
  2. Delete .idea directory from my project folder.从我的项目文件夹中删除 .idea 目录。
  3. Reopen IntelliJ and import the project again (as Gradle).重新打开 IntelliJ 并再次导入项目(作为 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.完成上述操作后,当我单击 gradle refresh 按钮时,我添加到 build.gradle 的任何新 gradle 依赖项开始出现在 External Dependencies 部分。

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.在我的情况下,我在刷新 gradle('View'->Tool Windows->Gradle)选项卡并点击“refresh”时遇到错误,并且没有类 jetgradleplugin 的此类属性 gradleversion 出现此错误。

Had to install latest intellij compatible with gradle 5+必须安装与 gradle 5+ 兼容的最新 Intellij

in my case, just re-import the jar file into correct directory .就我而言,只需将 jar 文件重新导入正确的目录即可。 File->project Structure->[Project setting -> Modules]文件->项目结构->[项目设置->模块] 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM