Home > other >  Unresolved reference for Launch in CoroutineScope(IO).Launch
Unresolved reference for Launch in CoroutineScope(IO).Launch

Time:11-18

I've read the following, it doesn't answer my question.

Unresolved reference: launch

I'm trying to follow the following tutorial https://www.youtube.com/watch?v=z7lfPYLGE7k for creating a simple socket in Kotlin. Everything is trivial up to this point:

I have:

CoroutineScope(IO).Launch {
    client(address, port)
}

However, this gives the error:

Unresolved reference: Launch

It is unclear to me what I should put in my dependencies in build.gradle, I've tried a slew:

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:x.x.x"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:x.x.x"
implementation("androidx.collection:collection-ktx:1.2.0")
implementation("androidx.fragment:fragment-ktx:1.5.4")
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.1"
implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.5.1")
implementation("androidx.navigation:navigation-runtime-ktx:2.5.3")
implementation("androidx.navigation:navigation-fragment-ktx:2.5.3")
implementation("androidx.navigation:navigation-ui-ktx:2.5.3")
implementation("androidx.palette:palette-ktx:1.0.0")
implementation("androidx.lifecycle:lifecycle-reactivestreams-ktx:2.5.1")
implementation("androidx.room:room-ktx:2.4.3")
implementation("androidx.sqlite:sqlite-ktx:2.2.0")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1")
implementation("androidx.work:work-runtime-ktx:2.7.1")
implementation("com.google.android.play:core-ktx:1.8.1")

I also don't understand what I should import, again I've tried probably more than necessary:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.system.Os.socket
import java.net.Socket
import java.util.*
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import androidx.activity.viewModels

Failing this, I've also tried the following to see if I can call the client() function from these:

            viewModelScope.launch {

            }

            viewLifecycleOwner.lifecycleScope.launch {

            }

            lifecycleScope.launch {

            }

However, they have the same problem.

CodePudding user response:

The method you're looking for is:

CoroutineScope(Dispatchers.IO).launch {

   ...
}

Without the capitalization in the method name.

  • Related