简体   繁体   English

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

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

I have a toy Java project set up with Gradle in IntelliJ IDEA 13.1.2.我在 IntelliJ IDEA 13.1.2 中使用 Gradle 设置了一个玩具 Java 项目。 It compiles and runs fine, but the IDE highlights 'google' in它编译并运行良好,但 IDE 在

import com.google.common.base.Strings;

as red and warns "Cannot resolve symbol 'google'".为红色并警告“无法解析符号‘谷歌’”。 Any idea how to fix it?知道如何修复它吗?

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重新导入项目,但无济于事。

I think user Sap is correct, at least in my case.我认为用户 Sap 是正确的,至少在我的情况下是这样。 You should not have to manually add the dependency.您不必手动添加依赖项。

Did you change the dependencies in the gradle file without syncing intellij?您是否在不同步 intellij 的情况下更改了 gradle 文件中的依赖项? Try this 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

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.gradle file will be resolved.直接手动添加到build.gradle文件中的所有依赖build.gradle都将得到解决。

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确保勾选“启用注释处理”

我的 Gradle 项目使用的是 Intellij 2019.2.3, File->Invalid Cache/Restart...不起作用,但View->Tool Windows->Gradle->sync(the circular icon)立即起作用。

It turns out that the depended packages need to be separately specified in IntelliJ IDEA in addition to build.gradle and explicitly added as a dependency.原来除了build.gradle ,依赖包还需要在IntelliJ IDEA中单独指定,并显式添加为依赖。 Namely,即,

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

In my case, I have to do the following:就我而言,我必须执行以下操作:

  • close project关闭项目
  • close idea接近的想法
  • remove .idea project directory删除 .idea 项目目录
  • remove (idea.system.path) directory删除(idea.system.path)目录
  • start idea开始想法

You can find your idea.system.path here: https://intellij-support.jetbrains.com/hc/en-us/articles/206544519-Directories-used-by-the-IDE-to-store-settings-caches-plugins-and-logs您可以在此处找到您的idea.system.pathhttps : idea.system.path -插件和日志

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

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

What helped me was checking "dependencies" in "project settings/modules" section.帮助我的是检查“项目设置/模块”部分中的“依赖项”。 Apparently, Idea did not pick up them correctly.显然,Idea 没有正确地捡起它们。

Steps which worked for me:对我有用的步骤:

  1. delete all modules from "project settings/modules"从“项目设置/模块”中删除所有模块
  2. refresh the project from Gradle plugin - that triggers generation of modules从 Gradle 插件刷新项目 - 触发模块的生成

This trick helped me to get modules with correct dependencies generated.这个技巧帮助我获得了生成正确依赖项的模块。 Probably it happened because initially, I imported the project as non-Gradle one.可能是因为最初,我将该项目作为非 Gradle 项目导入。

I was having the wrong import.Check if you have right import.我导入错误。检查您是否正确导入。 In case you have imported using:如果您使用以下方式导入:

import org.junit.Test 

and you have org.junit.jupiter.api.Test in class path, try importing with :并且您在类路径中有org.junit.jupiter.api.Test ,请尝试导入:

import org.junit.jupiter.api.Test;

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

相关问题 在 Gradle 项目 Intellij Idea 中找不到符号方法获取/设置 - cannot find symbol method get/set in Gradle project Intellij Idea Intellij IDEA 2019.3、JDK 11.0.3 中的 Maven java 项目:无法解析符号“java”但仍可编译 - Maven java project in Intellij IDEA 2019.3, JDK 11.0.3: Cannot resolve symbol 'java' but still compile 无法解析 Intellij Idea 中的符号字符串 - Quarkus 项目 - Cannot resolve symbol String in Intellij Idea - Quarkus project IntelliJ IDEA中的Gradle Java调试项目 - Gradle Java debugging project in IntelliJ IDEA Intellij IDEA Java Gradle项目运行 - Intellij IDEA Java Gradle Project Running 错误:找不到符号 - 在 IntelliJ IDEA 的现有 gradle 项目中添加和导入新 class 时 - error: cannot find symbol - when new class is added tand imported in an existing gradle project in IntelliJ IDEA Intellij IDEA 中的“无法解析符号” - "Cannot resolve symbol" in Intellij IDEA Intellij无法在具有多个SourceSet的Gradle Project中解析符号 - Intellij can not resolve Symbol in Gradle Project with multiple SourceSets IntelliJ IDEA:String、System 和其他 Java 类的“无法解析符号” - IntelliJ IDEA: "cannot resolve symbol" for String, System and other Java classes 在 IntelliJ 的 Java 项目中获取“找不到符号” - Getting "cannot find Symbol" in Java project in IntelliJ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM