Search code examples
androidkotlinandroidx

androidx.lifecycle.DefaultLifecycleObserver not found


I am having a problem where my build cannot find one of the androidx classes, specifically:

androidx.lifecycle.DefaultLifecycleObserver

Here are my gradle deps:

annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.0.0"
implementation "androidx.lifecycle:lifecycle-runtime:2.0.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"

I don't have any other problems getting androidx classes, just this one.

I have tried to invalidate caches in android studio and restart with no luck. Also a build clean as well as a gradle sync.

I verified here:

https://developer.android.com/jetpack/androidx/migrate

The mapping of that class from the support library to androidx:

android.arch.lifecycle.DefaultLifecycleObserver -> androidx.lifecycle.DefaultLifecycleObserver

Any ideas on why this is happening?


Solution

  • I think that's because you're missing one dependency:

    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
    

    Get the latest version of lifecycle-common-java8 here

    See the documentation:

    If you use Java 8 Language, then observe events with DefaultLifecycleObserver. To include it you should add "androidx.lifecycle:lifecycle-common-java8:" to your build.gradle file.