Решением является приведенный ниже код (каждая часть представляет собой отдельный файл для моего проекта)
Импорт Gradle
// Dagger 2
def dagger_version = '2.27'
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
// Assisted Injection
def dagger_assist_version = '0.5.2'
compileOnly "com.squareup.inject:assisted-inject-annotations-dagger2:$dagger_assist_version"
kapt "com.squareup.inject:assisted-inject-processor-dagger2:$dagger_assist_version"
Класс Worker
class FileSplitterWorker @AssistedInject constructor(
@Assisted private val appContext: Context,
@Assisted private val params: WorkerParameters,
private val messagesRepository: MessagesRepository
) : CoroutineWorker(appContext, params) {
override suspend fun doWork(): Result {
// Do Work with messageRepository
return Result.success()
}
@AssistedInject.Factory
interface Factory : ChildWorkerFactory
}
Создайте этот интерфейс
interface ChildWorkerFactory {
fun create(appContext: Context, params: WorkerParameters): CoroutineWorker
}
И фабричный класс для рабочих
class AppWorkerFactory @Inject constructor(
private val workerFactories: Map<Class<out CoroutineWorker>, @JvmSuppressWildcards Provider<ChildWorkerFactory>>
) : WorkerFactory() {
override fun createWorker(
appContext: Context,
workerClassName: String,
workerParameters: WorkerParameters
): CoroutineWorker? {
val foundEntry =
workerFactories.entries.find { Class.forName(workerClassName).isAssignableFrom(it.key) }
val factoryProvider = foundEntry?.value
?: throw IllegalArgumentException("unknown worker class name: $workerClassName")
return factoryProvider.get().create(appContext, workerParameters)
}
}
Компонент App для Dagger должен быть похож на
@Singleton
@Component(
modules = [
AndroidInjectionModule::class,
AndroidSupportInjectionModule::class,
AppAssistedInjectModule::class,
WorkManagerModule::class,
etc etc...
]
)
interface AppComponent {
fun inject(application: MyApplication)
fun workerFactory(): AppWorkerFactory
@Component.Factory
interface Factory {
fun withContext(@BindsInstance application: MyApplication): AppComponent
}
}
@Module(includes = [AssistedInject_AppAssistedInjectModule::class])
@AssistedModule
interface AppAssistedInjectModule
Модуль WorkerModule должен быть и Интерфейс
@Module(includes = [RepositoryModule::class, etc etc])
interface WorkManagerModule {
@Binds
@IntoMap
@WorkManagerKey(FileSplitterWorker::class)
fun bindFileSplitterWorker(factory: FileSplitterWorker.Factory): ChildWorkerFactory
}
И класс приложения
override fun onCreate() {
super.onCreate()
instance = this
val daggerAppComponent = DaggerAppComponent.factory().withContext(this)
daggerAppComponent.inject(this)
val factory: AppWorkerFactory = daggerAppComponent.workerFactory()
WorkManager.initialize(this, Configuration.Builder().setWorkerFactory(factory).build())
}
companion object {
private lateinit var instance: MyApplication
}
И в конце вы должны объявить workmanager манифестом в теге приложения
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
android:exported="false"
tools:node="remove" />