未解决的参考:启动 [英] Unresolved reference: launch

查看:107
本文介绍了未解决的参考:启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to run some examples for Kotlin coroutines, but can't build my project. I'm using the latest gradle release - 4.1

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

Any suggestions what to check/fix?

这里是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时,我得到以下信息

when I run gradle compileKotlin I get the following

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`

推荐答案

就像注释中已经回答的一样,kotlinx.coroutines.experimental.*软件包的导入丢失了.您可以在 GitHub (如果您愿意).

Like already answered in the comments, the import is missing for the kotlinx.coroutines.experimental.* package. You can have a look at my examples at GitHub if you like.

import kotlinx.coroutines.experimental.*

fun main(args: Array<String>) {

    launch(CommonPool) {
        delay(1000)
        LOG.debug("Hello from coroutine")
    }

}

这篇关于未解决的参考:启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆