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

Rakesh
  • 1,205
  • 1
  • 14
  • 33

5 Answers5

14

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

Amir Hossein
  • 392
  • 3
  • 11
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

K. Donon
  • 254
  • 2
  • 13
  • 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. – lase Feb 28 '22 at 16:45
6

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

4

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

Freddy
  • 764
  • 2
  • 6
  • 21
0

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