未找到 androidx.lifecycle.DefaultLifecycleObserver

标签 android kotlin androidx

我遇到一个问题,我的构建无法找到其中一个 androidx 类,特别是:

androidx.lifecycle.DefaultLifecycleObserver

这是我的 gradle 部门:

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

我在获取 androidx 类时没有任何其他问题,只有这个。

我曾尝试使 android studio 中的缓存无效并重新启动,但没有成功。还有一个 build clean 和 gradle sync。

我在这里验证:

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

该类从支持库到 androidx 的映射:

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

关于为什么会发生这种情况有什么想法吗?

最佳答案

我认为那是因为您缺少一个依赖项:

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

获取最新版本的lifecycle-common-java8 here

参见 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.

关于未找到 androidx.lifecycle.DefaultLifecycleObserver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55819499/

相关文章:

Android Retrofit 在获取 jsonobject 时总是返回 boolean 类型的 false 值

android - 如何使用android-ndk-r10d在windows上编译ffmpeg-2.5.3

android - Koin: NoBeanDefFoundException, 检查你的模块定义

android - 如何将 ViewBinding 与 RecyclerView Adapter 与 Kotlin 中已有的内部类一起使用?

android - play-services-ads :18. 0.0 和 appcompat-v7 :28. 0.0 - 合并失败,并且无法组合使用 groupid com.android.support 和 androidx.* 的依赖项

java - 迁移到 AndroidX 后,我收到此错误 inflateing class <unknown>

java - 使用 robotium 在 ListView 中单击图像时可扩展 ListView 滚动

android - 在 Android 中读取大型列表以获取 AutoCompleteTextView

android - Jetpack Compose 无法编译应用程序,找不到 kotlin-compiler

android - 在 android 的 build.gradle 中添加位置服务时出现问题