简体   繁体   中英

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

I use IntelliJ IDEA 2016.2.4 together with 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.

When hovering over the "missing" class, IntelliJ offers me to add JUnit4 to classpath. 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.

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:

'testCompile' cannot be applied to '(['group':java.lang.String, 'name':java.lang.String,...])' This inspection reports assignments with incompatible types

It works fine for other dependencies I tried, ie there's no warning like this. But IntelliJ still doesn't see their classes.

These are my first steps with Gradle, so maybe something is still not configured. 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. There are good chances its not correct.

I had the same problem after update IDEA to version 2020.3.
IDEA saw JDK libraries and didn't see external libraries.
solution:

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

Your project settings will be saved and IDEA continue see all external libraries.

After trying with Maven and getting the same result, it was clear that it wasn't a problem with Gradle nor JUnit.

Removing the folder ~/.IdeaIC2016.2 did the job.

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. This solves the issue.

Example: IDEA "lost" class SomeEntity.java :

  1. Create class MyAwesomeEntity.java
  2. Past the content of SomeEntity.java to MyAwesomeEntity.java
  3. Remove SomeEntity.java
  4. Rename MyAwesomeEntity.java to SomeEntity.java

voila

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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