[intellij-idea] Kotlin unresolved reference in IntelliJ

I started off with the tutorial for learning Kotlin in IntelliJ.When I tried running the example i.e

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

Execution is suspended with this messageError:(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 seen the other question already.The answer is not valid for my situation.

This question is related to intellij-idea jvm kotlin

The answer is


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

enter image description here


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

In my case, it was a misconfigured IntelliJ instance.

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


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


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


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


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

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


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


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


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.


Another thing I would like to add is that you need to select View -> Tool Windows -> Gradle before you can run the project using the Gradle.

If using the Gradle the project builds and runs normally but using the IntelliJ it doesn't, then this can solve the matter.


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.

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


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!


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


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

apply plugin: 'kotlin-android'


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


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


Simplest Solution:

Tools->Kotlin->Configure Kotin in Project

Upvote if found Usefull.


Examples related to intellij-idea

IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Error: Java: invalid target release: 11 - IntelliJ IDEA IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 ERROR Source option 1.5 is no longer supported. Use 1.6 or later Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 How to configure "Shorten command line" method for whole project in IntelliJ intellij idea - Error: java: invalid source release 1.9 Failed to resolve: com.google.android.gms:play-services in IntelliJ Idea with gradle

Examples related to jvm

Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 How can I get a random number in Kotlin? Kotlin unresolved reference in IntelliJ Is JVM ARGS '-Xms1024m -Xmx2048m' still useful in Java 8? Android Gradle Could not reserve enough space for object heap Android java.exe finished with non-zero exit value 1 Android Studio Gradle project "Unable to start the daemon process /initialization of VM" Android Studio - No JVM Installation found Android Studio error: "Environment variable does not point to a valid JVM installation" Installing Android Studio, does not point to a valid JVM installation error

Examples related to kotlin

No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator How to allow all Network connection types HTTP and HTTPS in Android (9) Pie? Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0 Default interface methods are only supported starting with Android N Error : Program type already present: android.support.design.widget.CoordinatorLayout$Behavior Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 startForeground fail after upgrade to Android 8.1 How to get current local date and time in Kotlin How to add an item to an ArrayList in Kotlin? HTTP Request in Kotlin