How to get lifecycle.coroutineScope with new androidx.lifecycle:*:2.2.0-alpha01
Asked Answered
P

4

45

On 7th May, 2019 androidx.lifecycle:*:2.2.0-alpha01 was released announcing:

This release adds new features that adds support for Kotlin coroutines for Lifecycle and LiveData. Detailed documentation on them can be found here.

On documentation it's mentioned that I can get the LifecycleScope:

either via lifecycle.coroutineScope or lifecycleOwner.lifecycleScope properties

But it seems that I'm not able to find none of them. My current dependencises are:

def lifecycle_ver = "2.2.0-alpha01"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_ver"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_ver"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_ver"

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1'

What may be the cause and how do get these apis?

Parmer answered 8/5, 2019 at 7:0 Comment(1)
T
75

I actually spent a couple hours trying to figure this out myself and it turns out it is in a new package that only exists as of the alpha. Add this and you should be good to go.

implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_ver"
Turquoise answered 8/5, 2019 at 7:21 Comment(7)
Are you sure you did that exact line? I pulled up the source for it and have my own code running with it using lifecycleScope. What is it doing when you add it and try to reference lifecycleScope from a lifecycleOwner?Turquoise
Even in an activity you should be able to do lifecycle.lifecycleScope since the activity is a lifecycle owner. What error is it giving you when you add the runtime-ktx dependency?Turquoise
Thanks, works for me. Just use lifecycleScope.launch {...} from acrivityGrandchild
My bad, didn't noticed -ktx at end, you should also edit the answer and mention the same. Wasted a whole day :pParmer
OMG. I'm searching for this for hours! This works. (The jetpack / ktx documentation is absolutely miserable. So many different sites but none shows a clear list of available packages and versions. Shame on you jetpack team!)Dottie
Source: developer.android.com/topic/libraries/architecture/…Yarn
How come I still fail to get it ? I tried it for this : androidexplained.github.io/android/ui/2020/10/21/… . For some reason, no matter what I add to gradle, I can't get lifecycleScope to be used at all...Felishafelita
G
10

The accepted answers is working, but I'm misused for the first time, so I'm trying to make it clear, the current version of lifecycle is "2.1.0" and lifecycleScope, and ViewModelScope is not available in this version, to get them use

For ViewModelScope,

use androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01 or higher.

For LifecycleScope,

use androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha01 or higher.

at this time 2.4.1 is available.

Gid answered 20/11, 2019 at 9:7 Comment(0)
F
1

Try this line of code in Build.Gradle (App Level)

implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.2.0")
Flyn answered 10/9, 2023 at 13:31 Comment(0)
T
0

I've just added this two lines of code to my build.gradle (App) and that worked like a charm. I can import lifecycleScope without any problems.

implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'
Tenorrhaphy answered 19/9, 2022 at 7:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.