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

Android WorkManager ListenableWorker implementation: ListenableFuture interface isn't found

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

app.gradle

...
dependencies {
    ...
    implementation "androidx.work:work-runtime-ktx:2.1.0"
}

MyWorker.kt

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

据我了解,我想要的依赖项是 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,我会得到错误:

Program type already present: com.google.common.util.concurrent.ListenableFuture

android {
    ...
    configurations.all {
        resolutionStrategy {
            force "com.google.guava:listenablefuture:1.0"
        }
    }
}
...

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

build.gradle

dependencies {
   ...
   implementation 'com.google.guava:guava:28.0-android'
}

我还找到了 Threading in ListenableWorker Android documentation section about ListenableWorker implementation, which suggest to use councurrent-futures CallbackToFutureAdapter,如果应用程序不使用 guava。我试过了,但是解决方案没有解决错误,ListenableFuture was not found.