External class in Kotlin project can't be resolved when using Kotlin, but can be resolved when using Java
Answered
I have a project called "Virtue" that is separate to the project "Base". "Base" is a dependency to "Virtue".
When using Kotlin in Virtue, the project cannot identify the Kotlin singleton "rip.paragon.base.Base" even though it is added as a Maven dependency:
However, when using Java in Virtue, the class is resolved just fine:
Keep in mind Base is coded in Kotlin and contains no Java code whatsoever.
TLDR: Project cannot identify a class in an external library when using Kotlin but it can resolve it when using Java. Not sure how to fix, can't find a solution online. My POM is setup correctly and the external JAR shows up fine under external libraries.
Please sign in to leave a comment.
Something else I should mention, I can use Java external libraries just fine when using Kotlin, but cannot use Kotlin + Kotlin together.
Hello Levi,
Have you tried to run "File | Invalidte Caches"? If it doesn't help please create new issue on YouTrack: https://youtrack.jetbrains.com/issues/KT
Hi, I tried that last night, although it seems after restarting my PC and invalidating caches again it seems to work now. I assume it was some bug related to Maven. I'll post it on YouTrack if it re-occurs. Thanks for the help.
You are welcome!
I'm having the exact same issue again since the original post. I've tried restarting my PC and invalidating caches, deleting the .idea and .iml file, and have tried deleting the LocalHistory and caches folder (there was one other folder too, forgot what it's called) nothing has worked.
Running IntelliJ Ultimate 2019.3 stable build, I have also tried disabling every plugin but Kotlin and .ignore, still not importing correctly.
The project compiles on my PC with Maven fine in the same project directory and it also compiles fine on the CI server I have setup for this project. I have a remote repository defined in the POM which 100% works fine also and I can download artifacts from the server just fine. I'm using Maven 3.6.1. I've also tried nuking the local repository folder (in the .m2 folder) and hasn't worked.
To clarify, the dependency is imported just fine in the POM and on the dependencies list (in the "external libraries" section) there's no red lines or anything. Every other dependency imports fine besides one; this dependency is 99% Kotlin code and everything else is 100% Java, so I'm certain it's something to do with the original issue (not being able to use external Kotlin classes inside Kotlin classes).
Levy,
Please report issue on YouTrack: https://youtrack.jetbrains.com/issues/KT
Hey Levi Taylor,
I think i'm having the same issue here, but its suddenly happening where it was fine before, the only change being I changed the java version from 8 to 11, but trying reverting back now leaves me with the same problem.
I've tried all proposed solutions, did you manage to fix this?
Julioevm, can you please create a YouTrack issue at http://kotl.in/issue and attach your project and IDE logs if possible https://intellij-support.jetbrains.com/hc/en-us/articles/207241085-Locating-IDE-log-files
Julioevm, Did you created the issue as requested? I'm facing the same problem and the use of Java 11 looks like to be the cause.
Hi Lbernardomaia, Yeah I created the issue: https://youtrack.jetbrains.com/issue/KT-38671
Sadly, it was closed since they couldn't reproduce it. The workaround I had to make was to rename the class that was giving me trouble, after changing its name everything worked fine.
Julioevm, thought it was worth mentioning here since I fixed this a long time ago; the issue I had was that I had the Kotlin std-lib compiling with my dependency (in my case, Base.) When I tried to use the dependency in my project (Virtue) it wouldn't detect Kotlin classes. I think this is probably a bug, I don't see why this would be intentional. If you're using Kotlin std-lib, or any other Kotlin dependencies, I suggest making their scope as "provided" so they don't compile with your dependency. Then try use the dependency again.
I suggest invalidating caches and restarting as well.