简体   繁体   English

Intellij IDEA 看不到类,但通过 Gradle 构建有效

[英]Intellij IDEA doesn't see classes but building via Gradle works

I use IntelliJ IDEA 2016.2.4 together with Gradle.我将 IntelliJ IDEA 2016.2.4 与 Gradle 一起使用。 When creating an empty project and adding a simple class together with a test class, the project builds fine when running build target through Gradle, but IntelliJ doesn't see eg @Test annotation for JUnit which is included in dependencies by default.当创建一个空项目并将一个简单的类与测试类一起添加时,通过 Gradle 运行build目标时项目构建良好,但 IntelliJ 没有看到例如默认包含在依赖项中的 JUnit 的@Test注释。

When hovering over the "missing" class, IntelliJ offers me to add JUnit4 to classpath.将鼠标悬停在“缺少”的类上时,IntelliJ 允许我将 JUnit4 添加到类路径。 After doing this, nothing changes.这样做之后,什么都没有改变。

I tried invalidating caches and restarting - no success.我尝试使缓存无效并重新启动 - 没有成功。

Under "External Libraries", there is "Gradle: junit:junit:4.11", and the project builds fine, so I presume it's an issue with syntax highlighting and autocompletion.在“外部库”下,有“Gradle:junit:junit:4.11”,并且项目构建良好,所以我认为这是语法突出显示和自动完成的问题。

I'm not sure if it matters, but in build.gradle the line that adds a dependency on JUnit ( testCompile group: 'junit', name: 'junit', version: '4.11' ) is highlighted with a comment:我不确定这是否重要,但在build.gradle中添加对 JUnit 的依赖的行( testCompile group: 'junit', name: 'junit', version: '4.11' )用注释突出显示:

'testCompile' cannot be applied to '(['group':java.lang.String, 'name':java.lang.String,...])' This inspection reports assignments with incompatible types 'testCompile' 不能应用于 '(['group':java.lang.String, 'name':java.lang.String,...])' 此检查报告具有不兼容类型的赋值

It works fine for other dependencies I tried, ie there's no warning like this.它适用于我尝试过的其他依赖项,即没有这样的警告。 But IntelliJ still doesn't see their classes.但是 IntelliJ 仍然没有看到它们的类。

These are my first steps with Gradle, so maybe something is still not configured.这些是我使用 Gradle 的第一步,所以可能还没有配置一些东西。 Does anyone have an idea what might be wrong?有谁知道可能有什么问题?

The cleaner way would be to Invalidate Caches / Restart.更简洁的方法是使缓存无效/重新启动。 You can find this option in File menu.您可以在“文件”菜单中找到此选项。

edit编辑

Intellij Idea uses iml files to resolves class path. Intellij Idea 使用 iml 文件来解析类路径。 There are good chances its not correct.很有可能它不正确。

I had the same problem after update IDEA to version 2020.3.将 IDEA 更新到 2020.3 版后,我遇到了同样的问题。
IDEA saw JDK libraries and didn't see external libraries. IDEA 看到了 JDK 库,而没有看到外部库。
solution:解决方法:

File -> Invalidate Caches / Restart... -> Invalidate and Restart

Your project settings will be saved and IDEA continue see all external libraries.您的项目设置将被保存,IDEA 将继续查看所有外部库。

After trying with Maven and getting the same result, it was clear that it wasn't a problem with Gradle nor JUnit.在尝试使用 Maven 并获得相同的结果后,很明显 Gradle 和 JUnit 都没有问题。

Removing the folder ~/.IdeaIC2016.2 did the job.删除文件夹 ~/.IdeaIC2016.2 完成了这项工作。

In case IDEA not recognizes a single class or a couple of classes, what I do is create new class with a different name, past a content of a missing class there, remove the old class and rename new class to a required one.如果 IDEA 无法识别单个类或几个类,我所做的就是创建具有不同名称的新类,在那里删除缺少类的内容,删除旧类并将新类重命名为所需的类。 This solves the issue.这解决了这个问题。

Example: IDEA "lost" class SomeEntity.java :示例:IDEA“丢失”类SomeEntity.java

  1. Create class MyAwesomeEntity.java创建类MyAwesomeEntity.java
  2. Past the content of SomeEntity.java to MyAwesomeEntity.javaMyAwesomeEntity.java的内容SomeEntity.javaMyAwesomeEntity.java
  3. Remove SomeEntity.java删除SomeEntity.java
  4. Rename MyAwesomeEntity.java to SomeEntity.javaMyAwesomeEntity.java重命名为SomeEntity.java

voila

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

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