0

I am trying to use work manager and use Koin to get some dependencies I have setup. My work manager extends KoinComponent which then allows me to use by inject but every time I try to use a component I am trying to get I get the error

NoBeanDefFoundException: No definition found for class AuthenticationService. Check your definitions!

Bear in mind that I use these dependencies just fine in activities and view models

My work manager

class BackgroundSync(private val context: Context, workerParams: WorkerParameters):CoroutineWorker(context, workerParams),
    KoinComponent{

    override suspend fun doWork(): Result {
        val authService:AuthenticationService by inject()
        val token = authService.getAuthToken() // Error here when trying to use it
    }
}

Then in my Koin module setup I have this

private val myModule = module {
    single<IAuthenticationService> { AuthenticationService() }
}

I used this question as reference but I cant get it to work properly, any idea's in what I am doing wrong?

tyczj
  • 71,600
  • 54
  • 194
  • 296

2 Answers2

0

In Koin you should inject exactly what you provide. In your case, in the koin module, you provide an interface but in BackgroundSync you inject the concrete class.

I believe you need to inject the interface:

class BackgroundSync(private val context: Context, workerParams: WorkerParameters):CoroutineWorker(context, workerParams),
    KoinComponent{

    override suspend fun doWork(): Result {
        val authService:IAuthenticationService by inject()
        val token = authService.getAuthToken() // Error here when trying to use it
    }
}
Feedbacker
  • 890
  • 6
  • 17
0

NoBeanDefFoundException, means without providing the dependency to koin component you are trying to access the object.

Try providing the instance to the koin component like below

private val myModule = module {
    single { AuthenticationService() }
}
Community
  • 1
  • 1
Rahul Singhal
  • 151
  • 1
  • 10