无法在带有 Kotlin 的 Android 中使用 Autodispose

Posted

技术标签:

【中文标题】无法在带有 Kotlin 的 Android 中使用 Autodispose【英文标题】:Unable to use Autodispose in Android with Kotlin 【发布时间】:2019-06-06 22:34:27 【问题描述】:

我试图使用 AutoDispose https://github.com/uber/AutoDispose 在 android 中处理我的 Rx 流。

但是当我将它添加到我的活动/片段时遇到问题。我认为这是一个 kotlin 问题,但我无法弄清楚是什么。

当我添加 AndroidLifecycleScopeProvider.from(lifecycle) 时,Android Studio 中的 from 方法出现错误,并且无法编译。

这是堆栈跟踪

None of the following functions can be called with the arguments supplied: 
public final fun from(p0: (() -> Lifecycle)!): AndroidLifecycleScopeProvider! defined in com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
public open fun from(p0: Lifecycle!): AndroidLifecycleScopeProvider! defined in com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
public open fun from(p0: LifecycleOwner!): AndroidLifecycleScopeProvider! defined in com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider

相同的代码适用于示例应用程序。

【问题讨论】:

@zac-sweers 有时间可以查一下。 你能确认你的lifecycle 变量的类层次结构是什么吗? 你能粘贴完整的可重现样本 sn-p 吗? 这方面有什么更新吗?我也面临同样的问题。 【参考方案1】:

我也遇到过同样的问题,这是由于 Lifecycle 包 Androidx 支持和以前的 v7 支持的兼容性造成的,它们在包工件上有所不同 命名。

AndroidXMirgration eg:工件命名:

com.android.support:appcompat-v7 -> androidx.appcompat:appcompat:1.0.0

AppCompatActivity 实现了 LifecycleOwner,这是 AutoDispose 工作的动力,但在最新版本的 AutoDispose 中,他们使用 AndroidX 代替(请参阅 AndroidLifecycleScopeProvider.from(LifecycleOwner owner) 'LifecycleOwner' ),因此如果您使用,所有者不能被视为正确的类型Non-Androidx with AutoDispose v1.1.0(AFAIK AutoDispose v0.8.0 适用于 com.android.support:appcompat-v7:28.0.0)

因此,您可以使用将您的应用迁移到 AndroidX Mirgration Guide 或使用旧版本的 AutoDispose。见AutoDispose on mvnrepository。

关于变更,请查看变更日志https://github.com/uber/AutoDispose/blob/master/CHANGELOG.md#version-100,v1.0.0 及更高版本依赖于AndroidX,v1.0.0-RC3 应该是我们的选择。

版本 1.0.0 2018-10-10 稳定发布!这在功能上与 1.0.0-RC3 相同,但完全迁移到了新的 AndroidX 工件。从现在开始,对于 Android 项目,您必须使用 AndroidX 工件。如果需要,您可以使用 1.0.0-RC3 来简化迁移。

编辑:如果你想要 RxKotlin 之类的扩展,请复制:https://github.com/uber/AutoDispose/blob/master/sample/src/main/kotlin/com/uber/autodispose/recipes/subscriberproxies.kt

【讨论】:

以上是关于无法在带有 Kotlin 的 Android 中使用 Autodispose的主要内容,如果未能解决你的问题,请参考以下文章

无法在我的 android 应用程序中使 proguard 工作

如何在 kotlin 中使 Material Button ToggleGroup 的所有子项不可点击

InstantiationException:在 Kotlin 中使实体字段不可为空时,没有实体的默认构造函数

如何在Android Studio中使图像中的特定颜色透明?

带有 Java 和 Kotlin 文件、kapt 或 annotationProcessor 的 Android 项目?

Android Kotlin:在 Fragment 下带有 Intent 的 setOnClickListener