Reputation: 1552
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 e.g. @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, i.e. 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?
Upvotes: 8
Views: 8843
Reputation: 1388
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
:
MyAwesomeEntity.java
SomeEntity.java
to MyAwesomeEntity.java
SomeEntity.java
MyAwesomeEntity.java
to SomeEntity.java
voila
Upvotes: 0
Reputation: 2864
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.
Upvotes: 11
Reputation: 4637
The cleaner way would be to Invalidate Caches / Restart. You can find this option in File menu.
Intellij Idea uses iml files to resolves class path. There are good chances its not correct.
Upvotes: 5
Reputation: 1552
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.
Upvotes: 2