gpt4 book ai didi

gradle - Unresolved reference : launch

转载 作者:IT老高 更新时间:2023-10-28 13:32:09 31 4
gpt4 key购买 nike

尝试为 Kotlin 协程运行一些示例,但无法构建我的项目。我正在使用最新的 gradle 版本 - 4.1

有什么建议检查/修复什么?

这里是 build.gradle

buildscript {
ext.kotlin_version = '1.1.4-3'

repositories {
mavenCentral()
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

apply plugin: 'kotlin'
apply plugin: 'application'

kotlin {
repositories {
jcenter()
}

experimental {
coroutines 'enable'
}

dependencies {
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.18"
}
}

main.kt

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

println("Hello, ")
Thread.sleep(2000L)
}

当我运行 gradle compileKotlin 我得到以下内容

e: /Users/philippgrigoryev/projects/kotlin-coroutines/src/main/kotlin/main.kt: (2, 5): Unresolved reference: launch
e: /Users/philippgrigoryev/projects/kotlin-coroutines/src/main/kotlin/main.kt: (2, 13): Unresolved reference: CommonPool
e: /Users/philippgrigoryev/projects/kotlin-coroutines/src/main/kotlin/main.kt: (3, 9): Unresolved reference: delay`

最佳答案

不再直接使用 Launch。 Kotlin documentation建议使用:

fun main() { 
GlobalScope.launch { // launch a new coroutine in background and continue
delay(1000L)
println("World!")
}
println("Hello,") // main thread continues here immediately
runBlocking { // but this expression blocks the main thread
delay(2000L) // ... while we delay for 2 seconds to keep JVM alive
}
}

关于gradle - Unresolved reference : launch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46136136/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com