首页 > 解决方案 > 无法在 Kotlin 中使用协程(未解决的参考)

问题描述

我创建了 gradle java kotlin 项目。我想使用协程,但我得到了unresolved reference launch错误unresolved reference delay

import java.util.*
import kotlinx.coroutines.*

fun main (args: Array<String>)
{
    launch { // launch coroutine 
        delay(1000L) 
        println("World!")
    }
    println("Hello")
}

在 build.gradle.kts 中,我将这一行包含在依赖项块中:

implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.5.2")

编辑:

我的 build.gradle.kts 文件:

plugins {
    kotlin("jvm") version "1.5.10"
    java
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib"))
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2")
}

tasks.getByName<Test>("test") {
    useJUnitPlatform()
}

EDIT2:清除缓存后一切都修复了。您可以在 File-> Invalidate caches 中执行此操作

标签: javakotlingradlekotlin-coroutinescoroutine

解决方案



推荐阅读