Summary: Unable to inject Koin Workmanager I am g...
# koin
m
Summary: Unable to inject Koin Workmanager I am getting
org.koin.core.error.NoBeanDefFoundException: No definition found for type 'androidx.work.WorkManager'. Check your Modules configuration and add missing type and/or qualifier!
When I try to inject WorkManager from Koin. I have in onCreate of Application
Copy code
startKoin {
   androidContext(context)
   workManagerFactory() 
   modules(...
And Android Manifest
Copy code
<provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    android:exported="false"
    tools:node="merge">
    <meta-data
        android:name="androidx.work.WorkManagerInitializer"
        android:value="androidx.startup"
        tools:node="remove" />
</provider>
And then injecting into my classes. Is there anything else I need to do to get WorkManager from Koin besides this?
a
m
yes...
a
can you swap workManagerFactory() and modules() section?
m
it didn't work. I ended up adding this to koin:
Copy code
single { WorkManager.getInstance(get()) }
I thought workManagerFactory() should provide it automatically (like androidContext()) but I guess not
a
yes it should
what version do you use?
m
Copy code
"io.insert-koin:koin-androidx-workmanager:3.5.6"
a
ok 😕
m
yes I'm using the same
workerOf
syntax
a
arf 😕
is you worker not starting at all?
m
It doesn't even get to starting the worker, it fails on injecting the WorkManager into the class that needs to start the worker
Copy code
//koin
viewModelOf(::MyViewModel)
workerOf(::MyWorker)

// myViewModel
class MyViewModel(val workManager: WorkManager) {
  fun doSomething() {
    val work = OneTimeWorkRequestBuilder<MyWorker>().build()
    workManager.enqueue(work)
  }
}
so it fails to create the viewmodel at all, because it can't find the workmanager
Oh actually I see that that sample project also had to retrieve workManager manually. Line 45 in SyncInitializer.kt , so I guess I am on the right track
docs could be a little clearer on that
a
good point 👍
m
I appreciate you looking into it!
207 Views