未解决的参考:1.3中的Kotlin中的异步 [英] Unresolved reference: async in Kotlin in 1.3

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

问题描述

我在github 此处.

我的带有构建文件build.gradle.kts文件的协程子项目是

One of my sub project introducing-coroutines with build file build.gradle.kts file is here

build.gradle.kts的内容是-

The contents of build.gradle.kts is -

    import org.jetbrains.kotlin.gradle.dsl.Coroutines
    import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

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

    group = "chapter2"
    version = "1.0-SNAPSHOT"

    repositories {
        mavenCentral()
    }

    dependencies {
        compile(kotlin("stdlib-jdk8"))
        compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0")
        testCompile("junit", "junit", "4.12")
    }

    configure<JavaPluginConvention> {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    tasks.withType<KotlinCompile> {
        kotlinOptions.jvmTarget = "1.8"
    }

    kotlin {
        experimental {
            coroutines   = Coroutines.ENABLE
        }
    }

我正在尝试从此当我编译程序时,出现以下错误-

When I compile the program I get the below error -

e: /Users/rajkumar.natarajan/Documents/Coding/coroutines-demo/introducing-coroutines/src/main/kotlin/SumUsingCoroutines.kt: (15, 20): Unresolved reference: async
> Task :introducing-coroutines:compileKotlin FAILED

FAILURE: Build failed with an exception.

我可以毫无问题地导入import kotlinx.coroutines.async,但不确定为什么会出现此错误.

I can import import kotlinx.coroutines.async without any issue but not sure why I'm getting this error.

我已经在此处中验证了类似问题,并添加了anko-commons依赖项此处

I have already verified similar issue here and added anko-commons dependency here

如何解决此错误?

推荐答案

首先,您必须从Gradle中删除启用实验协同程序功能的部分.

First of all you have to remove from Gradle the part where you enable the experimental coroutine feature.

kotlin {
    experimental {
        coroutines   = Coroutines.ENABLE
    }
}

您不能再隐式使用async()函数.您必须为全局范围协程GlobalScope.async(){...}显式调用它,也可以使用CoroutineScope(...).async{...}从其他协程范围或从范围函数coroutineScope {...}withContext(...){...}调用它.

You cannot use async() function implicitly anymore. You have to call it explicitly for a global scope coroutine GlobalScope.async(){...} or you can call it from another coroutine scope using CoroutineScope(...).async{...} or from scoping functions coroutineScope {...}, withContext(...){...}.

我写了一个示例供个人使用,以了解自己的协程工作方式.我希望它很好并且有帮助.

I have written an example for personal use to understand myself how coroutines are working. I hope it is good and helpful.

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

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