IntelliJ won't recognize some imports from gradle project

Viewed 15612

I am working on a Gradle project in IntelliJ and some packages are not recognized by IntelliJ. When I go to Project Structure -> Modules -> <my module> -> Dependencies, the jar file that contains these packages is there. (I've examined the jar file with jar tfv <file> to make sure that the classes in question are in the file.)

The classes in question are in red and when hovering over them, I get errors like "Cannot resolve symbol 'somepackagename'" or "Cannot resolve symbol 'SomeClassName'".

However, the gradle project compiles just fine from the command line.

I've tried all the existing suggestions I could find, but so far none have helped. Primarily, I've already tried:

  • Deleting the .idea folder and re-importing
  • Reimporting the project from the root build.gradle file
  • Clicking on the "Refresh all gradle projects" button
  • Upgrading IntelliJ
  • Clicking on "Invalidate Caches and Restart"

What can I do to get IntelliJ to recognize the packages in these jar files?

7 Answers

I also experience this problem on multi-project Gradle builds, it's still reproducible with the latest (at the moment of writing) IntellijIDEA 2020.3.2. It looks like some kind of internal caching issue, because even though IDEA complains it can't recognize the classes it can execute the build successfully.

Initially I was fixing it by invalidating caches and restarting IDEA, as had been suggested here, but then discovered that it goes away if I reload the project in Gradle Tool Window:

enter image description here

For me it works every time.

I had the same problem. My IntelliJ didn't recognize some dependencies from the build.gradle. My guess is that its a cache thing. So I did the following:

  1. Delete the gradle caches directory.

    rm -rf ~/.gradle/caches
    
  2. Restart IntelliJ and refresh gradle dependencies.

This worked for me and seems like a simple solution.

I just had the same issue with Intellij Idea 2019.2.3 just after upgrading. Seems the File -> "Invalidate Caches and Restart" action fixed the problem.

Other previous actions I tried (reimport gradle project, remove .idea/* and .ipr/.iml, restart intellij) did not fix the issue.

The issue still occurs on IntelliJ IDEA 2021.3.2 (Community Edition). Invalidate Caches/Restart did not work, this solved it:

  1. Delete the .idea folder
  2. Close and open the project

You might have VPN enabled and this can cause the Gradle to stop importing dependencies.

This happened where my VPN connection was on and I was not able to import the dependencies.

Please check this as well if you are facing issues in addition to the solutions described here.

Also, check the proxy settings. Proxy might cause import problem we well. Try to disable the proxy and check.

Make sure you actually included the dependency in the right spot

for a kotlin example, in my build.gradle.kts I have multiple source sets that look like this:

sourceSets{
val jvmTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
            }
        }
        val jsMain by getting {
            dependencies {
                implementation("khttp:khttp:1.0.0")
            }
        }
}

jsMain won't have access to junit and jvmTest won't have access to khttp.

In short, if you have two source sets who are named very similarly, you are asking for confusion.

Related