Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved reference: async in Kotlin in 1.3

I have multi module kotlin gradle project in github here.

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

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
        }
    }

I'm trying to create my first coroutine program from this link.

import kotlinx.coroutines.*
import kotlinx.coroutines.async
import kotlin.system.*
import kotlin.system.measureTimeMillis

suspend  fun computecr(array: IntArray, low: Int, high: Int): Long {
    return if (high - low <= SEQUENTIAL_THRESHOLD) {
        (low until high)
                .map { array[it].toLong() }
                .sum()
    } else {
        val mid = low + (high - low) / 2
        val left = async { computecr(array, low, mid) }
        val right = compute(array, mid, high)
        return left.await() + right
    }
}

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.

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

enter image description here

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

How can I resolve this error?

like image 897
Rajkumar Natarajan Avatar asked Jan 21 '19 03:01

Rajkumar Natarajan


People also ask

How do I fix unresolved reference Kotlin?

If you still see the unresolved reference error after fixing the problem, try to build your Android application with Command + F9 for Mac or Control + F9 for Windows and Linux. The error should disappear after the build is completed.

What is CoroutineScope in Kotlin?

Kotlin coroutines provide an API that enables you to write asynchronous code. With Kotlin coroutines, you can define a CoroutineScope , which helps you to manage when your coroutines should run. Each asynchronous operation runs within a particular scope.

Does Kotlin have async await?

This is somewhat different from languages such as C# that have async and await as part of the syntax. With Kotlin, these are just library functions.


4 Answers

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

kotlin {
    experimental {
        coroutines   = Coroutines.ENABLE
    }
}

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.

like image 174
LiTTle Avatar answered Oct 03 '22 22:10

LiTTle


You can try this:

  CoroutineScope(Dispatchers.Default).async {
        ...
  }
like image 22
Bunthai Deng Avatar answered Oct 03 '22 17:10

Bunthai Deng


The problem is that async (the same as launch) is defined as an extension function on CoroutineScope. In the following example, it is invoked in the receiver CoroutineScope of withContext:

suspend  fun computecr(array: IntArray, low: Int, high: Int): Long {
    return if (high - low <= SEQUENTIAL_THRESHOLD) {
        (low until high)
            .map { array[it].toLong() }
            .sum()
    } else {
        withContext(Default) {
            val mid = low + (high - low) / 2
            val left = async { computecr(array, low, mid) }
            val right = computecr(array, mid, high)
            left.await() + right
        }
    }
}
like image 30
Omar Mainegra Avatar answered Oct 03 '22 22:10

Omar Mainegra


You can try this:

  CoroutineScope(Dispatchers.Default).async {
        ...
  }
like image 34
SpringKhmer Avatar answered Sep 30 '22 22:09

SpringKhmer