Kotlin unresolved reference in IntelliJ

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?

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


Solution 1:

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).

Solution 2:

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

Solution 3:

In my case, it was a misconfigured IntelliJ instance.

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

Solution 4:

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

apply plugin: 'kotlin-android'

Solution 5:

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