Noob question. I'm using Android Studio (android d...
# getting-started
d
Noob question. I'm using Android Studio (android development) I've Included dependencies:
Copy code
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"
But I cannot use
async
and
launch
in a
suspend fun
Copy code
suspend fun foo() {
  async {} // Unresolved reference
  launch {} // Unresolved reference
}
What am I missing here?! EDIT: nevermind, I was remembering it wronly, I need something like
coroutineScope {}
or
withContext(...)
or something like that
Anyone?
m
async and launch are extension functions on CoroutineScope - you cannot call them without it
d
I'm inside a
suspend fun
w
Are you in the same module which that is imported?
d
yes, it's an android app
w
I haven’t use that dependency, we use here:
Copy code
"org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.coroutines}"
d
Copy code
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.2"
Copy code
buildscript {
    ext.kotlin_version = '1.4.21'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.0-alpha03'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 
    }
}
org.jetbrains.kotlinx:kotlinx-coroutines-android
should include
org.jetbrains.kotlinx:kotlinx-coroutines-core
but I manually included core just in case, no difference
m
Why does it matter that you are inside suspend function?
d
cause the scope is that coroutine? it is supposed to create a child coroutine
otherwise structured concurrency goes out of the window
When I used coroutine 1.3.8 I could use
async
and
launch
just fine within a
suspend fun
m
oh is it some android thing then?
d
why would it be? this is Kotlin, as far as I know there's no "android" kotlin, there's just kotlin
m
Can you please point me to some resource showing this? I am a bit lost
d
I've no idea what you are asking about @Milan Hruban
I do not see any change in regard to that here: https://github.com/Kotlin/kotlinx.coroutines/blob/master/CHANGES.md
m
@Daniele Segato some code sample ideally in kotlin docs, that shows usage of
async
or
launch
in suspend function, without coroutine scope
d

https://youtu.be/Mj5P47F6nJg?t=2208

is this good enough?
are you saying I should do
Copy code
coroutineScope {
            launch() {}
            async() {}
        }
?
or
withContext
?
maybe I'm wrong but I remember using it directly inside the
suspend fun
you are saying this was never possible?
v
d
I checked coroutines a while ago, studied it than had to do something else and now i'm getting back to it. I still didn't build hours of work on it
m
well it depends what you want to do - the docs will probably explain it better then me
d
I see, I was probably remembering wrongly... I always used some kind of container for that.... so now I've another question about testing, doing it in another thread.
e
It was possible more that 2 years ago, before 1.0 release -> https://elizarov.medium.com/structured-concurrency-722d765aa952 (it also explains why it is not allowed now)
d
@elizarov ye, sorry, I actually rewatched that part and realized it... My bad, unfurtunately I spent time to study coroutines and try them out a couple of months ago, than I suspended cause I needed SharedFlow and needed to finish a project. Now I'm taking it up again more seriously and I just miss-remembered that. Thanks for your answer!