IntelliJ IDEA 中带有 Gradle 的 Java 项目:无法解析符号“google”,但项目可以编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24600804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:22:57  来源:igfitidea点击:

Java project with Gradle in IntelliJ IDEA: cannot resolve symbol 'google' but project compiles

javaconfigurationimportintellij-ideagradle

提问by Meng Lu

I have a toy Java project set up with Gradle in IntelliJ IDEA 13.1.2. It compiles and runs fine, but the IDE highlights 'google' in

我在 IntelliJ IDEA 13.1.2 中使用 Gradle 设置了一个玩具 Java 项目。它编译并运行良好,但 IDE 在

import com.google.common.base.Strings;

as red and warns "Cannot resolve symbol 'google'". Any idea how to fix it?

为红色并警告“无法解析符号'google'”。知道如何修复它吗?

I have tried 1) deleting .idea/and re-creating the project in IntelliJ IDEA, and 2) re-importing project from the manually created Gradle configuration file build.gradle, but to no avail.

我尝试过 1).idea/在 IntelliJ IDEA 中删除并重新创建项目,以及 2) 从手动创建的 Gradle 配置文件中重新导入项目build.gradle,但无济于事。

采纳答案by Meng Lu

It turns out that the depended packages need to be separately specified in IntelliJ IDEA in addition to build.gradleand explicitly added as a dependency. Namely,

事实证明,依赖包需要在 IntelliJ IDEA 中单独指定,build.gradle并显式添加为依赖项。即,

  1. download the com.google.guavapackage following the instruction in documentation-- I picked com.google.guava:guava-base:r03to download
  2. follow the automatically-prompted window to add it as a project library to the Java project.
  3. Specify that the Java module depends on the com.google.guavapackage in the project settings. See the documentation instruction "Configuring Module Dependencies and Libraries".
  1. 按照文档中的说明下载com.google.guava包——我选择下载com.google.guava:guava-base:r03
  2. 按照自动提示的窗口将其作为项目库添加​​到 Java 项目中。
  3. com.google.guava在项目设置中指定 Java 模块依赖于包。请参阅文档说明“配置模块依赖项和库”

回答by daftuser

I think user Sap is correct, at least in my case. You should not have to manually add the dependency.

我认为用户 Sap 是正确的,至少在我的情况下。您不必手动添加依赖项。

Did you change the dependencies in the gradle file without syncing intellij? Try this button:

您是否在不同步 intellij 的情况下更改了 gradle 文件中的依赖项?试试这个按钮:

screenshot of sync button

同步按钮的屏幕截图

For more information, see: https://www.jetbrains.com/idea/help/synchronizing-changes-in-gradle-project-and-intellij-idea-project.html

有关更多信息,请参阅:https: //www.jetbrains.com/idea/help/synchronizing-changes-in-gradle-project-and-intellij-idea-project.html

回答by GyuHyeon Choi

Check this.

检查这个

You can simply open Gradle tool window at [ View] - [ Tool Windows] - [ Gradle].

您可以简单地在 [ View] - [ Tool Windows] - [ Gradle]处打开 Gradle 工具窗口。

In the window, you can refresh by clicking refresh button.

在窗口中,您可以通过单击刷新按钮进行刷新。

All dependencies manually added directly into build.gradlefile will be resolved.

build.gradle将解决所有直接手动添加到文件中的依赖项。

回答by Namita

One of the solutions that worked for me after trying everything listed on the internet to solve this issue was to install the lombok plugin.

在尝试了互联网上列出的所有内容来解决此问题后,对我有用的解决方案之一是安装 lombok 插件。

Got to File --> Settings --> Plugins and look for Lombok.

转到文件 --> 设置 --> 插件并查找 Lombok。

Make sure the "Enable annotation processing" is ticked

确保勾选“启用注释处理”

回答by Udi Meiri

In my case (Apache Beam sources) a ./gradlew cleanwas needed.

在我的情况下(Apache Beam 来源)./gradlew clean需要一个。

回答by Abderrahim BADIY

In My Case, I've Updated the Gradle version(module: project) from 3.2.2 to 3.5.2, and also there was a problem with the NDK file location it was on the wrong path, I've just switched it to the default NDK path, then invalidate and restart the project.

在我的案例中,我已经将 Gradle 版本(模块:项目)从 3.2.2 更新到 3.5.2,而且 NDK 文件位置存在问题,它位于错误的路径上,我刚刚将其切换为默认 NDK 路径,然后使项目无效并重新启动。

回答by TGU

My Gradle project is using Intellij 2019.2.3, and File->Invalid Cache/Restart...doesn't work, but View->Tool Windows->Gradle->sync(the circular icon)works instantly.

我的 Gradle 项目使用的是 Intellij 2019.2.3,File->Invalid Cache/Restart...但无法运行,但View->Tool Windows->Gradle->sync(the circular icon)可以立即运行。