You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin使用协程提示unresolved reference未定义报错如何解决

问题描述

创建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的dependencies块中添加协程核心依赖:

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()
}
解决方案

执行IDE菜单栏操作 File -> Invalidate caches 清除缓存后重启IDE,问题即可修复。

内容的提问来源于stack exchange,提问作者vytaute

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 11:30:03