16

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?

Chris Middleton
  • 5,654
  • 5
  • 31
  • 68
  • Please report at https://youtrack.jetbrains.com/issues/IDEA with the sample project to reproduce attached. – CrazyCoder Nov 14 '18 at 20:50
  • @CrazyCoder I will do that if I can get a sample project together, but I was hoping that this may be a common enough problem that someone (such as yourself) might be able to answer here for others to see. I saw that your top answer was about a very similar problem: https://stackoverflow.com/questions/5905896/intellij-inspection-gives-cannot-resolve-symbol-but-still-compiles-code/5905931#5905931 Are there any additional steps you would recommend to rule out this being a bug? – Chris Middleton Nov 14 '18 at 21:14
  • Since you've already tried all the common solutions and they didn't work, your issue is probably different and is not something common that can be answered right away just from the description. – CrazyCoder Nov 14 '18 at 21:16
  • @CrazyCoder Fair enough. Since I am a gradle and IntelliJ newbie, I thought maybe I was just missing something obvious, but even if that's the case, you're right that it may be specific to my situation. I will attempt to report at the site. – Chris Middleton Nov 14 '18 at 21:18
  • @CrazyCoder I came up with a workaround below that I'm honestly surprised works. The details are below. I'm now telling Gradle to consider all the files in the Gradle cache directory as a compile dependency, and it then picks up on the jars that it wouldn't before, even though it's automatically finding other jars in that same Gradle cache. Do you have any thoughts as to why the below workaround would work and what cleaner solution there might be? – Chris Middleton Nov 15 '18 at 17:32
  • @CrazyCoder I just added an update to my post below - in the end, after going through all of that, once I removed all my changes, it continued working, indicating that there might be some kind of corrupted cache going on inside IntelliJ that my workaround fixed. So thankfully, although I don't know why, it now works without any changes from my initial setup. – Chris Middleton Nov 15 '18 at 18:05

8 Answers8

13

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
curd0
  • 2,300
  • 3
  • 27
  • 32
8

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.

Doron Gold
  • 3,664
  • 4
  • 32
  • 44
amay
  • 501
  • 5
  • 7
6

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.

Guillaume Berche
  • 3,049
  • 2
  • 17
  • 18
4

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
mementoGuy
  • 391
  • 2
  • 8
1

In my case,

  1. existing dependency lib deleting
  2. gradle refresh!

ref link : https://youtu.be/0rLZK6hIpm0

DonisYim
  • 36
  • 4
0

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.

Muhammad Tariq
  • 3,318
  • 5
  • 38
  • 42
0

I got the same problem with only one of many others dependencies.

Two steps helped for me:

  1. Fully remove the project.
  2. Clone again it from remote repository.

All other steps were inconclusive.

Evangilit
  • 21
  • 5
0

I just had to run the :clean task

Vinccool96
  • 202
  • 1
  • 5
  • 2
    Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – moken Jul 23 '23 at 10:05