Here is my code...
class MainActivity : AppCompatActivity() { val myService: MyService by inject(named("MyService")) val yourService: YourService by inject(named("YourService")) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) myService.myServiceDo() yourService.yourServiceDo() } MyApp :
class MyApp : Application() { override fun onCreate() { super.onCreate() startKoin { androidContext(this@MyApp) module { myModule } } } IService Interface :
interface IService { fun foo()} MyService :
class MyService : IService { override fun foo() { TODO("Not yet implemented") } fun myServiceDo(){ Log.d("MyService", "myServiceDo...") } YourService :
class YourService : IService { override fun foo() { TODO("Not yet implemented") } fun yourServiceDo() { Log.d("YourService", "yourServiceDo...") } myModule :
val myModule = module { single<IService>(named("MyService")) { MyService() } single<IService>(named("YourService")) { YourService() } The error is like org.koin.core.error.NoBeanDefFoundException: No definition found for class:'se.jun.koinex.IService' & qualifier:'MyService'. Check your definitions!
I can't understand why it happens even if I did think I followed Koin Offical Documents.
Could you please help me? thanks.
https://stackoverflow.com/questions/67294061/android-koin-di-cant-get-instance-with-named April 28, 2021 at 01:06PM
没有评论:
发表评论