InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
9.08k stars 718 forks source link

org.koin.core.error.InstanceCreationException: Could not create instance for #574

Closed caiolesk closed 5 years ago

caiolesk commented 5 years ago

I tried to consume my api. Following this steps.. https://medium.com/android-dev-br/modulariza%C3%A7%C3%A3o-android-parte-3-22622ae7d162

val presentationModule = module {

factory { AndroidItemAdapter() }

viewModel { MainViewModel(
        useCase = get(),
        uiScheduler = AndroidSchedulers.mainThread()
) }

}

val dataCacheModule = module { single { ItensDataBase.createDataBase(androidContext()) } factory { ItensCacheSourceImpl(itemDao = get()) } }

val repositoryModule = module { factory { AndroidItensRepositoryImpl( itensCacheDataSource = get(), remoteDataSource = get() ) } }

val dataModules = listOf(remoteDataSourceModule, repositoryModule, dataCacheModule)

val remoteDataSourceModule = module { factory { providesOkHttpClient() } single { createWebService( okHttpClient = get(), url = androidContext().getString(R.string.base_url) ) }

factory<RemoteDataSource> { RemoteDataSourceImpl(itemApi = get()) }

}

fun providesOkHttpClient(): OkHttpClient { return OkHttpClient.Builder() .connectTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS) .build() }

inline fun createWebService(okHttpClient: OkHttpClient , url: String): T { return Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .baseUrl(url) .client(okHttpClient) .build() .create(T::class.java) }

val useCaseModule = module {

factory {
    GetItensUseCases(
            repository = get(),
            scheduler = Schedulers.io()
    )
}

}

val domainModule = listOf(useCaseModule)

override fun onCreate() { super.onCreate()

    startKoin {
        androidContext(this@MyModuleApplication)

        modules(domainModule + dataModules + listOf(presentationModule))
    }
}

ERROR: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.mithz.mithz, PID: 16600 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mithz.mithz/com.mithz.mithz.activity.MainActivity}: org.koin.core.error.InstanceCreationException: Could not create instance for [type:Factory,class:'com.mithz.mithz.feature.main.MainViewModel'] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [type:Factory,class:'com.mithz.mithz.feature.main.MainViewModel'] at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:61) at org.koin.core.instance.FactoryDefinitionInstance.get(FactoryDefinitionInstance.kt:37) at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:75) at org.koin.core.Koin.resolve(Koin.kt:107) at org.koin.core.Koin.get(Koin.kt:96) at org.koin.androidx.viewmodel.ViewModelResolutionKt$createViewModelProvider$1.create(ViewModelResolution.kt:48) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130) at org.koin.androidx.viewmodel.ViewModelResolutionKt.getInstance(ViewModelResolution.kt:27) at org.koin.androidx.viewmodel.ViewModelResolutionKt.getViewModel(ViewModelResolution.kt:18) at com.mithz.mithz.activity.MainActivity$$special$$inlined$viewModel$1.invoke(LifecycleOwnerExt.kt:97) at com.mithz.mithz.activity.MainActivity$$special$$inlined$viewModel$1.invoke(Unknown Source:0) at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) at com.mithz.mithz.activity.MainActivity.getViewModel(Unknown Source:25) at com.mithz.mithz.activity.MainActivity.onCreate(MainActivity.kt:92) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [type:Factory,class:'com.mithz.domain.usecases.GetItensUseCases'] at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:61) at org.koin.core.instance.FactoryDefinitionInstance.get(FactoryDefinitionInstance.kt:37) at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:75) at org.koin.core.Koin.resolve(Koin.kt:107) at org.koin.core.Koin.get(Koin.kt:96) at com.mithz.mithz.di.PresentationModuleKt$presentationModule$1$2.invoke(PresentationModule.kt:28) at com.mithz.mithz.di.PresentationModuleKt$presentationModule$1$2.invoke(Unknown Source:4) at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:54) at org.koin.core.instance.FactoryDefinitionInstance.get(FactoryDefinitionInstance.kt:37)  at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:75)  at org.koin.core.Koin.resolve(Koin.kt:107)  at org.koin.core.Koin.get(Koin.kt:96)  at org.koin.androidx.viewmodel.ViewModelResolutionKt$createViewModelProvider$1.create(ViewModelResolution.kt:48)  at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164)  at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130)  at org.koin.androidx.viewmodel.ViewModelResolutionKt.getInstance(ViewModelResolution.kt:27)  at org.koin.androidx.viewmodel.ViewModelResolutionKt.getViewModel(ViewModelResolution.kt:18)  at com.mithz.mithz.activity.MainActivity$$special$$inlined$viewModel$1.invoke(LifecycleOwnerExt.kt:97)  at com.mithz.mithz.activity.MainActivity$$special$$inlined$viewModel$1.invoke(Unknown Source:0)  at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)  at com.mithz.mithz.activity.MainActivity.getViewModel(Unknown Source:25)  at com.mithz.mithz.activity.MainActivity.onCreate(MainActivity.kt:92)  at android.app.Activity.performCreate(Activity.java:6975)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [type:Factory,class:'com.mithz.domain.repository.AndroidItensRepository']

prudhvir3ddy commented 4 years ago

@caiolesk what's the solution ?

caiolesk commented 4 years ago

@caiolesk what's the solution ?

check if your class Database @Database(version = 1, entities = [AndroidItemCache::class]) abstract class ItensDataBase: RoomDatabase()

pavelsust commented 6 months ago

@caiolesk what's the solution ?

check if your class Database @database(version = 1, entities = [AndroidItemCache::class]) abstract class ItensDataBase: RoomDatabase()

Any solution??

caiolesk commented 6 months ago

@caiolesk what's the solution ?

check if your class Database @database(version = 1, entities = [AndroidItemCache::class]) abstract class ItensDataBase: RoomDatabase()

Any solution??

my solution is annotation on database and injection of this, check your tree of files with dependency injection if you forgot anything