Gradle项目构建成功,但IntelliJ无法解析引用。

huangapple go评论82阅读模式
英文:

Gradle project builds successfully but IntelliJ cannot resolve references

问题

以下是翻译好的部分:

每次使用以下Kotlin库进行编译并且可以使用,但是IntelliJ的自动补全(以及可能其他一些工具)无法解析这些引用。
https://gitlab.com/7Hazard/altv-kotlin

我通过JitPack将其作为依赖项:https://jitpack.io/#com.gitlab.7Hazard/altv-kotlin
JDK 11,Gradle 6.3(使用wrapper)

Gradle项目可以正常构建,但自动补全功能失效。其他依赖项(如kotlinx.coroutines)正常工作。

【图片:IntelliJ的linter错误】1

altv-kotlin是一个fatjar,甚至在jar中包含了源代码:https://gitlab.com/7Hazard/altv-kotlin/-/blob/master/build.gradle#L53
它本身使用了一个Java依赖项,因此altv-kotlin使用了一些Java代码,最终的fatjar包含了Kotlin和Java的源代码,如果这可能会有影响的话。

是否可能是由于某种原因导致源代码没有正确地放入JAR文件中?我甚至无法让IDEA反编译用于自动补全的引用。我强烈感觉这是与altv-kotlin库特定相关的问题,而不是我的任何环境设置的问题。

我尝试过删除.idea文件夹,使缓存失效/重新启动,为IDEA和Gradle项目更新Kotlin插件,甚至将IDEA从2018更新到2020,但都没有解决问题。我也尝试过以下答案,但问题没有解决:
https://stackoverflow.com/a/56619181/8122837
https://stackoverflow.com/a/5905931/8122837
我还注意到有关Kotlin运行时与库捆绑在一起的警告,但即使不捆绑在一起,IDEA也会报错。

英文:

Every usage of the following Kotlin library compiles and can be used, but IntelliJ's autocomplete (and perhaps others) cannot resolve the references.
https://gitlab.com/7Hazard/altv-kotlin

I am using it as a dependency via JitPack: https://jitpack.io/#com.gitlab.7Hazard/altv-kotlin
JDK 11, Gradle 6.3 (wrapper)

The gradle project builds fine but just autocompletion is broken. Other dependencies like kotlinx.coroutines work fine.

Gradle项目构建成功,但IntelliJ无法解析引用。

It altv-kotlin is a fatjar and even includes the sources in the jar: https://gitlab.com/7Hazard/altv-kotlin/-/blob/master/build.gradle#L53
It in turn uses a java dependency, so altv-kotlin uses some Java and the end fatjar includes both Kotlin and Java sources if that makes a difference.

Could it be that the source are not being put in the JAR correctly for some reason? I couldn't even get IDEA to decompile the references for autocompletion. I have a strong feeling this is an issue tied to altv-kotlin library specifically, and not any of my environment setup.

I've tried to delete the .idea folder, Invalidate Caches / Restart, update Kotlin plugin for both IDEA and gradle projects and even updated IDEA from 2018 to 2020 with no avail whatsoever.
I tried these answers as well but did not solve the problem:
https://stackoverflow.com/a/56619181/8122837
https://stackoverflow.com/a/5905931/8122837
I also noticed the warning about Kotlin Runtime being bundled with the library, but even without it being bundled in it IDEA complains.

答案1

得分: 0

所以,就在本地,我似乎通过重新创建项目并将源代码复制到其中解决了这个问题。似乎起作用了,为什么我不知道。
然而,当从JitPack使用远程依赖时,因为我在JAR中包含了所有的类文件,以使其成为一个fatjar。

jar {
    from configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}

自从我从jar任务中移除了那行代码以后,IntelliJ IDEA能够正常解析引用了。

英文:

So, locally I seem to have resolved the issue by recreating the project and copying the sources into there. Seems to have worked, why I don't know.
However, when using the remote dependency from JitPack, because I was including all the classfiles in the JAR to have it be a fatjar.

jar {
    from configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it)
}

Ever since I removed that line from the jar task, IntelliJ IDEA was able to resolve the references normally.

huangapple
  • 本文由 发表于 2020年5月2日 17:10:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61556959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定