Hi, I'm having trouble to compile kotlin with coro...
# coroutines
m
Hi, I'm having trouble to compile kotlin with coroutines on command line.
1. File.kt
Copy code
import kotlin.concurrent.thread
import kotlinx.coroutines.*

// OutOfMemory Error
//fun main() {
//    repeat(100_000){
//        thread {
//            Thread.sleep(1000L)
//            print(".")
//        }
//    }
//}

fun main(args: Array<String>) {
    runBlocking {
        repeat(100_000) {
            launch {
                delay(1000L)
                print(".")
            }
        }
    }
}
2. In the same folder as file, I have
kotlinx-coroutines-core-1.6.4.jar
3. Compile Command
kotlinc File.kt -cp kotlinx-coroutines-core-1.6.4.jar -include-runtime -d File.jar
4. Error output:
Copy code
File.kt:15:5: error: unresolved reference: runBlocking
    runBlocking {
    ^
File.kt:17:13: error: unresolved reference: launch
            launch {
            ^
File.kt:18:17: error: unresolved reference: delay
                delay(1000L)
                ^
Shouldn't
-cp
expose those class files for the import?
l
I am unsure about kotlinc compiler flags, but are you adding
kotlinx-coroutines-core-1.6.4.jar
on both the compile as well as the runtime classpath? from your third point it seems that it isn’t?!
j
@leandro It's a compile error, so when would you expect the runtime classpath to be involved anyway?
m
@Joffrey Any thoughts on the problem?
j
Sorry I'm not very familiar with the CLI interface of the compiler. A wild guess would be that the
-cp
argument might be required to appear before the
File.kt
argument, but maybe that's not the case.
l
@Joffrey The runtime execution isn’t a problem here, but I wanted to be comprehensive at least in regards to the classpath.
s
You need the jvm version of the coroutines lib on the classpath (
kotlinx-coroutines-core-jvm-1.6.4.jar
)
m
Ah ok! I guess the alternative would be to use
kotlinc-native
s
I guess you would then need the specific lib for the platform you are building for; eg if you are building for a apple silicon mac
kotlinx-coroutines-core-macosarm64
m
@Sidney Beekhoven I think I don't understand what exactly the
kotlinx-coroutines-core
dependency is used for then. Is this library mainly for common code in Multiplatform projects? Only to be used in
commonMain
? And if I had additional android platform coroutine requirements, I would need to include the dependencies like
kotlinx-coroutines-core-jvm
in the
android
layer?
j
If you're using Gradle, you don't have to care about the variants like this, and you can just depend on the base
kotlinx-coroutines-core
dependency. Gradle knows how to read metadata and fetch the relevant artifacts on the relevant platforms. However, if you decide to call the compiler by hand yourself, you'll have to deal with the subtleties like this
And more specifically it is sufficient to depend on
kotlinx-coroutines-core
in
commonMain
in a multiplatform project, you don't have to also add the
-jvm
variant in android/JVM source set dependencies.
123 Views