Koin dependency Injection Isssue - Android

211 Views Asked by At

In my android application am using MVVM architecture and using koin library for DI. Below is my Repository class:

class JaiminRepository constructor(
    private var remoteDataSource : RemoteDataSource,
    private var enrollApiInterface : EnrollApiInterface
) {
   ...
  }

Created module for is as below:

val jaiminRepositoryModule = module {

    single {
        JaiminRepository(get(),get())
    }

}

For this I am getting error as :

Instance creation error : could not create instance for [Singleton:'com.jaimin.sdk.repository.JaiminRepository']: org.koin.core.error.NoBeanDefFoundException: |- No definition found for class:'com.jaimin.api.RemoteDataSource'. Check your definitions! org.koin.core.scope.Scope.throwDefinitionNotFound(Scope.kt:287) org.koin.core.scope.Scope.resolveValue(Scope.kt:257)

So I have added factory for RemoteDataSource.

factory {
    RemoteDataSource()
}

and finally it look like as below:

val jaiminRepositoryModule = module {

        factory {
            RemoteDataSource()
        }

        single {
            JaiminRepository(get(),get())
        }

    }

But still am getting error. What might be the issue? Do I need to do something with EnrollApiInterface also? Please guide. Thanks in Advance.

1

There are 1 best solutions below

2
Stephan On

You have to define all dependencies in the module(or another module), otherwise your repository can't be created. Make sure you also provide the EnrollApiInterface:

val jaiminRepositoryModule = module {

    factory<EnrollApiInterface> {
        EnrollApiInterfaceImpl()
    }

    factory {
        RemoteDataSource()
    }

    single {
        JaiminRepository(get(),get())
    }

}