'Unresolved reference: launch. I can't write example with kotlin coroutines
I am going to learn kotlin coroutines, but i got problem on android studio.
I did need imports of library in gradle, but I got problems with code compiler.
I just can't use launch. Kotlin version is 1.3.50. Error: Unresolved Reference Launch
Import
import kotlinx.coroutines.delay
import kotlinx.coroutines.*
Code which I try to use:
suspend fun firstCoroutine(){
println("Kotlin Start")
launch(CommonPool){
delay(2000)
println("Kotlin Inside")
}
println("Kotlin End")
}
My build.gradle app file
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
//эти файлы используюстя для компиляции, построения и упаковки приложений и библиотек
//настройки для модуля
android {
compileSdkVersion 28
defaultConfig {
applicationId "guard_studio.akira.firstcotlinproject"//идентификатор приложения
minSdkVersion 17//минимальная поддерживаемая версия сдк
targetSdkVersion 28
versionCode 1
versionName "1.0"//версия приложения для гугл плэй
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
//список библиотек подключенных к проекту
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2"
}
build.gradle level project
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "http://dl.bintray.com/kotlin/kotlin-eap"
}
}
}
Solution 1:[1]
It seems that you are following an outdated tutorial. You have to use CoroutineScope if you want to use latest version of the coroutines. Just for start you can use GlobalScope:
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
suspend fun firstCoroutine() {
println("Kotlin Start")
GlobalScope.launch {
delay(2000)
println("Kotlin Inside")
}
println("Kotlin End")
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrei Tanana |
