Android WorkManager ListenableWorker 实现:未找到 ListenableFuture 接口

Val*_*kov 6 android android-architecture-components android-workmanager

我正在尝试实现 Android ListenableWorkerstartWork() 方法返回com.google.common.util.concurrent.ListenableFuture接口,但最后一个不可用。我还需要com.google.common.util.concurrent.SettableFuture实施,但这也是不可用的。我应该包含哪个模块才能使这些类型可用?

应用程序.gradle

...
dependencies {
    ...
    implementation "androidx.work:work-runtime-ktx:2.1.0"
}
Run Code Online (Sandbox Code Playgroud)

我的Worker.kt

class SendRegistrationTokenWorker(
    context: Context,
    params: WorkerParameters,
) : ListenableWorker(context, params) {
   override fun startWork(): ListenableFuture<Result> { ... }
}
Run Code Online (Sandbox Code Playgroud)

据我了解,我想要的依赖项是com.google.guava:listenablefuture:1.0,但androidx.work:work-runtime-ktx:2.1.0已经依赖于它,以及应用程序使用的其他库。问题是com.google.guava:listenablefuture:1.0解析为com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava,它不包含所需的类型,它只是一个模拟依赖项。

我尝试添加单独的com.google.guava:listenablefuture:1.0依赖项,但它也解析为com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava并且没有任何变化。

如果我强制listenablefuture版本为1.0,我会收到错误:

程序类型已存在:com.google.common.util.concurrent.ListenableFuture

android {
    ...
    configurations.all {
        resolutionStrategy {
            force "com.google.guava:listenablefuture:1.0"
        }
    }
}
...
Run Code Online (Sandbox Code Playgroud)

Val*_*kov 3

最后,我发现的最简单的解决方案是将整个番石榴依赖项添加到项目中。我不确定这是正确的方法,因为我只使用库中的几种类型。但是,这是我正在寻找的简单解决方案。

构建.gradle

dependencies {
   ...
   implementation 'com.google.guava:guava:28.0-android'
}
Run Code Online (Sandbox Code Playgroud)

我还在ListenableWorker Android 文档部分中找到了有关 ListenableWorker 实现的线程,建议使用councurrent-futures CallbackToFutureAdapter(如果应用程序不使用 guava)。我尝试了一下,但解决方案没有解决错误,ListenableFuture没有找到。