简体   繁体   中英

IntelliJ won't recognize some imports from gradle project

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?

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:

在此处输入图片说明

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.

In my case,

  1. existing dependency lib deleting
  2. gradle refresh!

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

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.

After trying all the different suggestions about this issue, the one that seemed to work was to copy the unrecognized jars from my local Gradle cache into a folder within my project. I also had to tell Gradle to look there.

In summary, if I change my root project build.gradle like this:

allprojects {
    repositories {
        flatDir {
            dirs 'lib'
        }
    }
    ... existing repos ...
}

and the subproject's build.gradle like so:

dependencies {
    ... existing dependencies ...
    compile fileTree(dir: 'lib', include: '*.jar')
}

and then run the following:

jars=($(find /Users/me/.gradle/caches/modules-2/files-2.1/unrecognizedorg/ -name '*.jar'))
for jar in "${jars[@]}" ; do
    cp "$jar" ~/myproject/lib/
done

and then refresh Gradle within IntelliJ, the files are recognized.

Of course, then I have a problem of having a lib folder with jars that I do not want committed in my project, but there are ways around that. There's also the problem of keeping these folders up-to-date if any dependencies change.

I solved this by making a symbolic link to the Gradle cache directory:

ln -s /Users/me/.gradle/caches/modules-2/files-2.1/ lib

So that I don't have to copy the files, and any changes are picked up automatically.

Update

After temporarily removing all my workarounds so that I could assess how to make IntelliJ work without committing the workarounds (the build script changes as well as the symbolic link), I discovered that it still worked (!). That is, it now works in IntelliJ with no changes (by me) from the state that was not working.

The conclusion I draw is that perhaps IntelliJ had a corrupted cache, which was only fixed by getting it to recognize the jars via the symbolic link. Although I haven't tried it, it's possible that both deleting the ~/.gradle/caches and reloading the project would have had the same effect.

Either way, going through the steps above and then undoing them seems to do the trick.

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