简体   繁体   English

无法通过 Kotlin 在 Android 中使用 Autodispose

[英]Unable to use Autodispose in Android with Kotlin

I was trying to use AutoDispose https://github.com/uber/AutoDispose to dispose of my Rx streams in android.我试图使用 AutoDispose https://github.com/uber/AutoDispose在 android 中处理我的 Rx 流。

But I am having an issue when I add it to my activity/fragment.但是当我将它添加到我的活动/片段时我遇到了问题。 I think it is a kotlin issue but I am not able to figure out what.我认为这是一个 kotlin 问题,但我无法弄清楚是什么。

when I add AndroidLifecycleScopeProvider.from(lifecycle) I get an error on the from method in Android Studio and it won't compile.当我添加AndroidLifecycleScopeProvider.from(lifecycle) ,Android Studio 中的 from 方法出现错误,并且无法编译。

This is the stack trace这是堆栈跟踪

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

The same code works with there sample app.相同的代码适用于那里的示例应用程序。

I have encounter same issue, it cause by compatibility of Lifecycle package Androidx support and previous v7 support, they are different in package artifact naming.我遇到了同样的问题,这是由 Lifecycle 包 Androidx 支持和以前的 v7 支持的兼容性引起的,它们在包工件命名上不同。

AndroidX安卓X
Mirgration迁徙
eg: artifact naming:例如:工件命名:

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

AppCompatActivity implemented LifecycleOwner that is the power under AutoDispose's work, but in latest version AutoDispose, they use AndroidX instead (see AndroidLifecycleScopeProvider.from(LifecycleOwner owner) 'LifecycleOwner' ), so owner can not be treat as right type if you use Non-Androidx with AutoDispose v1.1.0(AFAIK AutoDispose v0.8.0 works well with com.android.support:appcompat-v7:28.0.0) AppCompatActivity 实现了 LifecycleOwner,这是 AutoDispose 工作的力量,但在最新版本的 AutoDispose 中,他们使用 AndroidX(请参阅 AndroidLifecycleScopeProvider.from(LifecycleOwner owner) 'LifecycleOwner' ),因此如果您使用非 Androidx,则所有者不能被视为正确类型使用 AutoDispose v1.1.0(AFAIK AutoDispose v0.8.0 适用于 com.android.support:appcompat-v7:28.0.0)

So, you can either use Migrate you app to AndroidX Mirgration Guide or use older version of AutoDispose.因此,您可以使用将您的应用迁移到 AndroidX迁移指南或使用旧版本的 AutoDispose。 See AutoDispose on mvnrepository .请参阅mvnrepository 上的 AutoDispose

About the change, see the changelog https://github.com/uber/AutoDispose/blob/master/CHANGELOG.md#version-100 , v1.0.0 and later version depend on AndroidX, v1.0.0-RC3 should be our choice.关于更改,请参阅更改日志https://github.com/uber/AutoDispose/blob/master/CHANGELOG.md#version-100 ,v1.0.0 及更高版本取决于 AndroidX,v1.0.0-RC3 应该是我们的选择.

Version 1.0.0 2018-10-10 Stable release! 1.0.0 版本 2018-10-10 稳定发布! This is identical in functionality to 1.0.0-RC3 but completely migrated to the new AndroidX artifacts.这在功能上与 1.0.0-RC3 相同,但完全迁移到新的 AndroidX 工件。 From this point forward for Android projects, you must be on AndroidX artifacts.从现在开始,对于 Android 项目,您必须使用 AndroidX 工件。 You can use 1.0.0-RC3 to ease migration if need be.如果需要,您可以使用 1.0.0-RC3 来简化迁移。

Edit : If you want RxKotlin like extension, go and copy: https://github.com/uber/AutoDispose/blob/master/sample/src/main/kotlin/com/uber/autodispose/recipes/subscriberproxies.kt编辑:如果你想要像 RxKotlin 这样的扩展,去复制: https : //github.com/uber/AutoDispose/blob/master/sample/src/main/kotlin/com/uber/autodispose/recipes/subscriberproxies.kt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM