简体   繁体   English

IntelliJ 无法识别来自 gradle 项目的某些导入

[英]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.我正在 IntelliJ 中处理 Gradle 项目,但 IntelliJ 无法识别某些包。 When I go to Project Structure -> Modules -> <my module> -> Dependencies, the jar file that contains these packages is there.当我转到 Project Structure -> Modules -> <my module> -> Dependencies 时,包含这些包的 jar 文件就在那里。 (I've examined the jar file with jar tfv <file> to make sure that the classes in question are in the file.) (我已经用jar tfv <file>检查了 jar 文件,以确保有问题的类在文件中。)

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'".有问题的类是红色的,当将鼠标悬停在它们上面时,我收到诸如“无法解析符号‘somepackagename’”或“无法解析符号‘SomeClassName’”之类的错误。

However, the gradle project compiles just fine from the command line.但是,gradle 项目从命令行编译得很好。

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删除 .idea 文件夹并重新导入
  • Reimporting the project from the root build.gradle file从根 build.gradle 文件重新导入项目
  • Clicking on the "Refresh all gradle projects" button单击“刷新所有 gradle 项目”按钮
  • Upgrading IntelliJ升级 IntelliJ
  • Clicking on "Invalidate Caches and Restart"单击“使缓存无效并重新启动”

What can I do to get IntelliJ to recognize the packages in these jar files?我该怎么做才能让 IntelliJ 识别这些 jar 文件中的包?

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 .我也在多项目 Gradle 构建中遇到了这个问题,它仍然可以用最新的(在撰写本文时) 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.看起来像是某种内部缓存问题,因为即使 IDEA 抱怨它无法识别可以成功执行构建的类。

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:最初我通过使缓存无效并重新启动 IDEA 来修复它,正如这里所建议的那样,但后来发现如果我在 Gradle 工具窗口中重新加载项目,它就会消失:

在此处输入图片说明

For me it works every time.对我来说,它每次都有效。

I had the same problem.我有同样的问题。 My IntelliJ didn't recognize some dependencies from the build.gradle.我的 IntelliJ 无法识别 build.gradle 中的某些依赖项。 My guess is that its a cache thing.我的猜测是它是一个缓存的东西。 So I did the following:所以我做了以下事情:

  1. Delete the gradle caches directory.删除 gradle 缓存目录。

     rm -rf ~/.gradle/caches
  2. Restart IntelliJ and refresh gradle dependencies.重新启动 IntelliJ 并刷新 gradle 依赖项。

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.升级后,我刚刚在 Intellij Idea 2019.2.3 上遇到了同样的问题。 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.我之前尝试过的其他操作(重新导入 gradle 项目,删除 .idea/* 和.ipr/ .iml,重新启动 intellij)没有解决问题。

In my case,就我而言,

  1. existing dependency lib deleting现有的依赖库删除
  2. gradle refresh!渐变刷新!

ref link : https://youtu.be/0rLZK6hIpm0参考链接: 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:对于 kotlin 示例,在我的 build.gradle.kts 中,我有多个源集,如下所示:

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. jsMain 将无法访问 junit,而 jvmTest 将无法访问 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.在尝试了有关此问题的所有不同建议后,似乎可行的是将无法识别的 jar 从我的本地 Gradle 缓存复制到我的项目中的文件夹中。 I also had to tell Gradle to look there.我还不得不告诉 Gradle 去那里看看。

In summary, if I change my root project build.gradle like this:总之,如果我像这样更改我的根项目 build.gradle:

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

and the subproject's build.gradle like so:和子项目的 build.gradle 像这样:

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.然后在 IntelliJ 中刷新 Gradle,文件被识别。

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.当然,然后我遇到了一个问题,即我不想在我的项目中提交一个带有 jar 的 lib 文件夹,但是有一些方法可以解决这个问题。 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:我通过创建指向 Gradle 缓存目录的符号链接解决了这个问题:

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 (!).在暂时删除所有变通方法以便我可以评估如何在不提交变通方法(构建脚本更改以及符号链接)的情况下使 IntelliJ 工作后,我发现它仍然有效(!)。 That is, it now works in IntelliJ with no changes (by me) from the state that was not working.也就是说,它现在可以在 IntelliJ 中工作,没有从不工作的状态(由我)改变。

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.我得出的结论是,IntelliJ 的缓存可能已损坏,只有通过符号链接让它识别罐子才能修复。 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.虽然我没有尝试过,但删除 ~/.gradle/caches 和重新加载项目可能会产生相同的效果。

Either way, going through the steps above and then undoing them seems to do the trick.无论哪种方式,执行上述步骤然后撤消它们似乎都可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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