Search code examples
androidandroid-architecture-componentsandroid-workmanager

Android WorkManager ListenableWorker implementation: ListenableFuture interface isn't found


I'm trying to implement Android ListenableWorker that startWork() method returns com.google.common.util.concurrent.ListenableFuture interface, but the last one isn't available. Also I need com.google.common.util.concurrent.SettableFuture implementation, which is also isn't available. Which module should I include to make these types available?

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> { ... }
}

As I understand, the dependency I want is com.google.guava:listenablefuture:1.0, but androidx.work:work-runtime-ktx:2.1.0 already depends on it, as well as other libraries that the app uses. The problem is that com.google.guava:listenablefuture:1.0 resolves to com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava, which doesn't contain the required type, it is just a mock dependency.

I tried to add separate com.google.guava:listenablefuture:1.0 dependency, but it also resolves to com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava and nothing changes.

If I force the listenablefuture version to be 1.0, I get the error:

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

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

Solution

  • Finally, the easiest solution I've found is to add the whole guava dependency to the project. I'm not sure it's the right way, because I use only few types from the library. But, it's simple solution, which I'm searching for.

    build.gradle

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

    I've also found Threading in ListenableWorker Android documentation section about ListenableWorker implementation, which suggest to use councurrent-futures CallbackToFutureAdapter, if the app doesn't use guava. I tried it to, but the solution didn't solve the error, ListenableFuture wasn't found.