Cannot resolve symbol repeatOnLifecycle in Android
Asked Answered
M

5

21

I'm following this article to collect flows in UI. But I couldn't resolve repeatOnLifeCycle API in my code. I have added the below dependency, though.

lifecycle:lifecycle-runtime-ktx:2.4.0-alpha03

Please helpenter image description here

Masquer answered 4/8, 2021 at 18:19 Comment(0)
S
15

It's not
lifecycle:lifecycle-runtime-ktx:2.4.0-alpha03
but
androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-alpha03

Shuddering answered 22/12, 2021 at 18:55 Comment(0)
V
10

I hope this gradle config can help you.

 def lifecycle_version = "2.4.0"
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version")
implementation("androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version")
implementation("androidx.lifecycle:lifecycle-common-java8:$lifecycle_version")

With this set, i resolve "repeatOnLifecycle" with this import: import androidx.lifecycle.repeatOnLifecycle

Valiancy answered 7/8, 2021 at 19:44 Comment(1)
This helped me - for whatever reason Android Studio just wouldn't resolve the repeatOnLifecycle extension automatically. After adding it manually myself, it was able to be located and turned out all my imports were fine as they were.Maudmaude
H
7

I also had a similar issue. For me, adding the following in the dependencies section of the build.gradle file (the App Module one and not the Project one) helped to resolve the issue:

implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-alpha03"

I also removed the import androidx.lifecycle.Lifecycle that was already there in the problematic file and got Android Studio to import the right one just in case the old one was wrong.

Found the latest version number to use(i.e. the "2.4.0-alpha03" part) from the following link but I think Android Studio would have later given a hint on the latest version to upgrade to anyway even if I hadn't done this: https://mvnrepository.com/artifact/androidx.lifecycle/lifecycle-runtime-ktx/2.2.0-alpha01

Hemialgia answered 9/8, 2021 at 15:16 Comment(0)
L
4

Note: These APIs are available in the lifecycle:lifecycle-runtime-ktx:2.4.0-alpha01 library or later. lifecycle-runtime-ktx

Lengel answered 10/9, 2021 at 8:50 Comment(0)
F
0

for me changing gradle from offline to online somehow solved the problem :/

Frenulum answered 12/8, 2021 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.