163

I started off with this tutorial for learning Kotlin in IntelliJ IDEA.
When I tried running the following example,

fun main(args: Array<String>) { println("lol") } 

Build fails with the following error:

Error:(5, 5) Kotlin: Unresolved reference: println 

This is the first time I am using IntelliJ. I have never worked on a Java project either. Am I missing something?

I have already seen this other question, though it is not valid for my situation.

6
  • Version Intellij IDEA? What Version of Kotlin plugin is installed? Commented Jul 30, 2015 at 8:57
  • Which option did you select in the Kotlin runtime dialog? Commented Jul 30, 2015 at 9:29
  • I just installed it yesterday.IDEA was 14.1 and latest Kotlin as well Commented Jul 30, 2015 at 9:56
  • Yes I'm also getting this on the "Hello world" example using Gradle and Netbeans. The gradle build works. I looks like a plug in problem with Netbeans too. Alas there are no recent updates for the Netbeans plugin Commented Apr 28, 2017 at 12:23
  • This worked for me for SpringBoot. stackoverflow.com/questions/53775262/… Commented Jan 13, 2021 at 12:49

32 Answers 32

169

Your Intellij IDEA plugin and the Kotlin runtime/compiler that you use in the project need to match. For example if IDE plugin is Beta 1, but your Gradle/Maven project using M12, you'll have issues exactly as you described. So check that everything lines up.

If you still have issues, with some older plugins you need to clear the caches in Intellij IDEA (file menu, Clear Caches and Restart, select option to restart).

Sign up to request clarification or add additional context in comments.

6 Comments

A combination of clearing caches, updating android studio and the kotlin plugin fixed it for me. Thank you
Hi again, with some time to consider stuff and a bit of Kotlin google-ing -- I don't think the cause here is IN the plugin-s per se. println is in the kotlin.io package. the Kotlin-stdlib JAR has "kotlin.io" in there. So no problem on the surface. I explicitly included kotlin.io.* -- Result: Unresolved reference: io. As a JAR archive, the reference resolution ought to be kindergarten stuff. So what's up!??
I'm back ... Netbeans people can also 'fix' this -- Drop the netbeans user cache. You can see the location of the Netbeans cache directory by clicking on the Help / About box. Clean -- Stop Netbeans -- Drop cache -- restart Netbeans -- rebuild! Volia.
If you're seeing this issue in your Gradle build file only but not project source files, this could help: discuss.kotlinlang.org/t/…
what do you mean by plugin versions? android studio has installed 1.8.20 version one. The project also has specifies the same version for kotlin-gradlePlugin. But the error still happens from time to time, especially after using git, switching branches and rebase. Before new Android Studio updates it was working fine
|
50

UPDATE - 01/2022

The jetbrains thread regarding this issue has moved and is nowhere to be found, so the link below is not valid anymore.

The option, per module, Kotlin -> FIX does not exist anymore and has been that way for years now since this was a temporary patch to deal with integration issues during the early previous releases.


If anyone stumbles across this and NEITHER Invalidate Cache or Update Kotlin's Version work:

  1. First, make sure you can build it from outside the IDE. If you're using gradle, for instance:

    gradle clean build

If everything goes well , then your environment is all good to work with Kotlin.

  1. To fix the IDE build, try the following:

Project Structure -> {Select Module} -> Kotlin -> FIX

As suggested by a JetBrain's member here: https://discuss.kotlinlang.org/t/intellij-kotlin-project-screw-up/597

4 Comments

I'm using Android Studio. I don't have a "Kotlin" option in my project structure > modules. Any ideas?
not even seeing this kotlin option after selecting modules ... using enterprise intellij, and kotlin is apparently already configured when i click tools -> kotlin -> configure
This saved me: ./gradlew clean build
What are you talking about? There IS no "FIX" under Kotlin, not for each module and not anywhere else in this useless ide.
40

In my case, it was a misconfigured IntelliJ instance.

Project Structure -> SDKs -> Remove and add the Java SDK again

3 Comments

This was my issue after removing OpenJDK and installing Oracle JDK. Thanks.
This has solved my issue on IntelliJ 2019.3 and OpenJDK 11. I just had to remove and add the SDK again.
IntelliJ Ultimate Edition 2022.2.1 and 2023.1.1, OpenJDK 18 -> issue still happening! and this was the fix. Thank you.
18

Ran into this issue. I had to add the following to my build.grade:

apply plugin: 'kotlin-android'

1 Comment

I was migrating project from java to kotlin and I faced this problem in one of the modules. Finally this solution worked for me. Thank you.
16

Ok, i've been here a few times and not entirely sure how or why this happens but reading suggests its a Kotlin/JVM mismatch. I have tried a number of things mentioned on this page but not without complete success. This was the sort of symptom the IDE was displaying in the message panel after trying to run tests via the IDE. However the code panel has no red lines indicating that modules can't be found or imported or anything untoward.

enter image description here

My project (at time of writing this) is a gradle 4.10.2 kotlin 1.3 project in IntelliJ (IntelliJ IDEA 2018.2.5 (Ultimate Edition) Build #IU-182.4892.20, built on October 16, 2018)

For starters though, I invalidated caches and restarted. Then ran a gradle clean build. After rebuilding the project still no joy. I went into the project settings and noticed the compiler versions were not set to java 8, I set the Compiler/Kotlin/Target JVM Version and Compiler/Java/Project Byte Code to 1.8/8 (see below), then ran some tests in the IDE and we are back in business! References resolved in my case.

enter image description here

enter image description here

1 Comment

I had a similar issue, but also had to change my modules modules to the correct target JVM version
8

A possible solution for standalone Kotlin projects is to include Kotlin standard libs explicitliy inside the root project.

To do that in IntelliJ IDEA:

  • press Ctrl+Shift+A (Search actions or options)
  • type in "Configure kotlin in project" and let it include standard libs for you

Comments

6

Sometimes this could happen because you are trying to use a project with incompatible versions of Kotlin plugin in Gradle with plugin in your IDE. Check the versions of org.jetbrains.kotlin.jvm and other Kotlin plugins in build.gradle, and version of installed kotlin plugin in your IDE, and try to make it similar:

enter image description here

enter image description here

1 Comment

God bless all of us!
4

For me, it was due to the project missing Gradle Libraries in its project structure.

Just add in build.gradle: apply plugin: 'idea'

And then run: $ gradle idea

After that gradle rebuilds dependencies libraries and the references are recognized!

1 Comment

This one is helped and worked due to missing gradle libraries
4

After trying a few steps, this is what worked for me.

Right-click on project > Open Module Settings > Project Settings > Modules
I removed other configs and kept just one, ie. "firstmicroservice - Kotlin"

From Preferences > Build, Execution, Deployment > Compile > Kotlin Compiler

From Preferences > Build, Execution, Deployment > Compile > Java Compiler

From Preferences > Build, Execution, Deployment > Build Tools > Maven > Runner

Once done RUN:
Maven clean
Maven compile
Maven install

1 Comment

there is no such thing as Kotlin compiler in the settings anymore
4

Invalidate Intellij Idea cashes and restart: File -> Invalidate Cashes... -> Select necessary items and

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
3

My problem was solved by adding kotlin as follow

presentation.gradle (app.gradle)

apply plugin: 'com.android.application' apply plugin: 'kotlin-android' // here apply plugin: 'kotlin-android-extensions' // and here android { ... } dependencies { ... } 

domain.gradle (pure kotlin)

My error was throw here, because Android Studio create my domain module as pure Java Module and applied plugin as java, and I used it in the my presentation module that is a Android/Kotlin Module

The Android Studio finds and import the path of package but the incompatibillity don't allow the build.

Just remove apply plugin: 'java' and swith to kotlin as follow

apply plugin: 'kotlin' // here is dependencies { ... } ... 

data.gradle

apply plugin: 'com.android.library' apply plugin: 'kotlin-android' // here apply plugin: 'kotlin-android-extensions' // and here android { ... } dependencies { .. } 

I think that it will be helpfull

Comments

2

Check and install Android Studio Updates. This fix the problem.

enter image description here

Comments

1

Had the same with IDEA 14.1.5, Kotlin v.1.0.0-beta-1038-IJ141-17.
Kotlin gets its own list entry (like Java) when creating new project, but the only working config was:

New | Project | Java | "Kotlin (Java)" (make sure you have Project SDK configured, too)
use library: Create, "Copy to: lib".

Comments

1

I tried everything: clean build folders, run gradle clean build, execute AS invalidate cache(x2), reinstall Kotlin plugin, restarting pc.

But in the end after doing all this it still didn't work, but doing also removal from project build.gradle of this line: apply plugin: 'kotlin-android-extensions', gradle sync and then re-adding worked

Comments

1

Top Solution not work for me, after some method tries, remove IDEA caches manully works:

caches directory:

~/Library/Caches/JetBrains/<product><version> on macOS ~/.cache/JetBrains/<product><version> on Linux %LOCALAPPDATA%\JetBrains\<product><version> on Windows 

solution source: KTX issues

Comments

1

In my case all settings were fine and I did Invalidate Cache and restart and still I had the problem. I tried below solution and worked for me.

  1. press crtl + shift + alt + s to enter project structure.
  2. In Project Settings select project.
  3. Then in Project SDK click on your java version and select Kotlin instead.
  4. Run the project.
  5. Do all 1 to 4 but again select your jdk.
  6. Re-Run your project.

enter image description here

Comments

1

Had the same with IntelliJ IDEA 2021.3.3.

  1. Check you have lastest kotlin plugin version for your IDE (Preferences -> Languages & Frameworks -> kotlin)

  2. Make sure your kotlin version and jvm target in your build.gradle file matches with your project-> setting -> modules-> Kotlin (for me was JVM 17 and kotlin 1.6)

  3. If still have issues just clean IDE caches:

    • File menu -> invalidate caches...(marked VCS option).

    • If still have issues just manual delete it under ~/Library/Caches/JetBrains/<product><version>

Comments

0

Happened to me today.

My issue was that I had too many tabs open (I didn't know they were open) with source code on them.

If you close all the tabs, maybe you will unconfuse IntelliJ into indexing the dependencies correctly

1 Comment

Actually this make total sense, the reason why you get -ngt score is because you didn't explain it well. If some1 has two project open in IDE let say one with Gradle and one with Maven; running one of the project may lead to unresolved reference.
0

Invalidating caches and updating the Kotlin plugin in Android Studio did the trick for me.

Comments

0

I had this issue because the linter was printing that my object was an instance of Foo whereas the compiler couldn't define its type and consider it was an Any object like (It was more complex in my case, the linter show error in below code but the idea is here) :

 class Foo { val bar: Int = 0 } fun test(): Any { return Foo() } val foo = test() foo.bar // <-- Linter consider that foo is an Instance of Foo but not the compiler because it's an Any object so it show the error at compilation. 

To fix it I had to cast my object :

val foo = test() as Foo 

Comments

0

I was too getting this error, but i have solved this using below concept.

  1. Restart your intellij idea community edition.
  2. After that, go on the right hand corner to set the configuration for java.

enter image description here

  1. choose configuration setting for all module. then re-run the code. It will work.

This error comes because of not configuring java properly.

2 Comments

I am sorry but the 2. is not clear. I couldn't find "the right hand corner"
reinstalling the intelliJ fixed the problem for me
0

In my case, I had tried everything to clear cache, Remove dependencies JARs manually and Reload Maven dependencies.

The problem was solved after I enabled all children and Parent POM's for the project:

  1. Goto

Preferences -> Build,Execution,Deployment -> Build Tool -> Maven -> Ignored Files

  1. Uncheck all ignored files and then reapply
  2. Sync Maven dependencies
  3. Clear Cache and Restart IntelliJ (if you are still seeing this error)

Comments

0

You have a problem with your root build.gradle file

  1. Check versions of org.jetbrains.kotlin:kotlin-gradle-plugin
  2. check versions of com.android.tools.build:gradle

update those version

Comments

0

What worked for me was right clicking the project > Maven > Reload project.

Comments

0

I was stuck on this IDE bug for half a day. It is certainly a problem related to the compatibility of the integrated Kotlin plugin, but if it is not solved with the various actions explained in the previous answers, the only solution that seems to work is to fully UNINSTALL (remove cache and plugins) and REINSTALL IntelliJ. Probably there are some old plugins that lurking somewhere in the IDE

Comments

0

I ran into the same issue, but it was not an intellij issue, rather than an mapping issue with the new Ktor version, so first you may check if you have to migrate the dependencies from 1.6.* to Ktor 2.0.* on this Site:

https://ktor.io/docs/migrating-2.html

Comments

0

If you use @UtilityClass - lombok lib.

Kotlin is not friendly with that.

For example: You have in java class with annotation @UtilityClass

private final String hello = 'Hello' 

You are trying to inject this const to Kotlin class, you will get

'Unresolved reference' 

Solution is : make the field static:

private static final String hello = 'Hello' 

#@UtilityClass #UnresolvedReference #Kotlin #Java

1 Comment

OP is having a very specific project structure problem. Not using a specific lib is not the solution here.
0

In my case I got this issue during maven build process, the reason was in spring-boot-maven-plugin. Initially I had single module maven project with Application class in it. Later I created several modules (core, connectors, api, etc) and copied pom into all of them. After changing project structure only api module had Application.kt class, api module also had other modules as dependencies in pom.xml When I ran mvn clean install spring-boot-maven-plugin was triggered in each module and caused classes inside jar to repackage, I assumed they gets moved between some directories. So other module classes became not visible for 'api' module during package/install The fix was to remove spring-boot-maven-plugin from library like modules and keep it only in the module with Application.kt class

Comments

-1

Sometimes in similar situations (I don't think it is your problem because your case is very simple) it's worth to check kotlin file package.

If you have Kotlin file within the same package and put there some classes and missed the package declaration it looks inside the IntelliJ that you have classes in the same package but without definition of package the IntelliJ shows you:

Error:(5, 5) Kotlin: Unresolved reference: ...

Comments

-1

I had this problem because I was trying to set up a multiplatform library configurations.

I had deleted all the source sets with their dependencies from the "build.gradle" file but kept the "common" source set.

Native was working fine but the JVM was not able to build and showed your error.

In the end, the solution was to not delete the Source set of the JVM neither it's dependencies

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.