0

After adding the library jetbrains.kotlinx.coroutines.core.jvm, the IDE seems to show coroutine-related classes in the auto-complete (first image below). However, building the source code below

import kotlinx.coroutines.*

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

fails with "Main.kt: (1, 8): Unresolved reference: kotlinx". Why is this so? I added the library in Project Settings -> Modules -> untilted3 (project name) and main (at first I added it to main, then I also added it untiled3) -> Dependencies, as the second image below.

enter image description here

enter image description here

enter image description here

Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135
  • Sounds like you are missing the external coroutines dependency. Have you checked this: https://stackoverflow.com/questions/52522282/using-kotlinx-coroutines-in-intellij-idea-project . And it would be helpful to know if you are using a build tool. – Augusto Jan 01 '22 at 10:49
  • @Augusto As you see above, I had already added the library, but that only made auto-complete work, not compilation. – Damn Vegetables Jan 01 '22 at 10:51
  • Ops, I missed that. Looks like you are using gradle. Did you add the dependency in gradle too? As intellij is running gradle to compile the project rather than the intenal compiler. – Augusto Jan 01 '22 at 10:56
  • @Augusto How do I add the dependency in Gradle? Actually, I had followed the same page that you linked above, and it did not say that I have to add it to Gradle. – Damn Vegetables Jan 01 '22 at 11:01
  • Check this bit of the documentation: https://github.com/Kotlin/kotlinx.coroutines#gradle . You need to check also which if you are using the groovy DSL or Kotlin DSL for gradle. You might already have a dependency block in your gradle file, otherwise add one... And a word of caution: Coroutines look really cool, but they are not that useful in server side development (they are useful for Android apps) – Augusto Jan 01 '22 at 11:04
  • @Augusto I was just trying to test coroutine. Anyway, adding `implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.6.0")` inside of `dependencies{}` in "build.gradle.kts", resolved the reference. I don't know the details about groovy or gradle (the are two different things?); all I know is that IntellJ uses it by default. Do I usually have to add a library in two places like this? It is really inconvenient. – Damn Vegetables Jan 01 '22 at 11:18
  • No, just add it to gradle and then [sync intellij with gradle](https://www.jetbrains.com/idea/guide/tutorials/working-with-gradle/syncing-and-reloading/). About the DSLs, Gradle supports 2 languages for the build files. Groovy is the old one and Kotlin is the new one... I just mentioned this as it's super easy to copy/paste a solution in a build file and wonder for hours why it doesn't work :D – Augusto Jan 01 '22 at 16:51

1 Answers1

0

Generally, as long as I know, this problem is due to a plugin that needs to be added to Gradle Project's file apply plugin: 'kotlin-android-extensions'

Dharman
  • 30,962
  • 25
  • 85
  • 135
Lucas
  • 458
  • 4
  • 6