2

I'm new in Koin and I've stuck with this error for a long time. This error is found when I run the code on emulator. I have no idea how to solve this. So, Do anyone have any suggestion?

E/[Koin]: Instance creation error : could not create instance for [Factory:'com.example.findgithubretrofit2rx.ui.SearchViewModel']: org.koin.core.error.NoBeanDefFoundException: No definition found for class:'com.example.findgithubretrofit2rx.data.UserImpl'. Check your definitions!
        org.koin.core.scope.Scope.throwDefinitionNotFound(Scope.kt:246)
        org.koin.core.scope.Scope.resolveInstance(Scope.kt:216)
        org.koin.core.scope.Scope.get(Scope.kt:181)
        com.example.findgithubretrofit2rx.di.ModulesKt$mainModule$1$1.invoke(Modules.kt:19)
        com.example.findgithubretrofit2rx.di.ModulesKt$mainModule$1$1.invoke(Unknown Source:4)
        org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:50)
        org.koin.core.instance.FactoryInstanceFactory.get(FactoryInstanceFactory.kt:36)
        org.koin.core.registry.InstanceRegistry.resolveInstance$koin_core(InstanceRegistry.kt:87)
        org.koin.core.scope.Scope.resolveInstance(Scope.kt:214)
        org.koin.core.scope.Scope.get(Scope.kt:181)
        org.koin.androidx.viewmodel.ViewModelFactoryKt$defaultViewModelFactory$1.create(ViewModelFactory.kt:50)
        androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
        androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
        org.koin.androidx.viewmodel.ViewModelResolutionKt.get(ViewModelResolution.kt:21)
        org.koin.androidx.viewmodel.ViewModelResolutionKt.resolveInstance(ViewModelResolution.kt:10)
        org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel(ScopeExt.kt:70)
        org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel(ScopeExt.kt:56)
        org.koin.androidx.viewmodel.koin.KoinExtKt.getViewModel(KoinExt.kt:34)
        org.koin.androidx.viewmodel.ext.android.ViewModelStoreOwnerExtKt.getViewModel(ViewModelStoreOwnerExt.kt:60)
        com.example.findgithubretrofit2rx.ui.SearchFragment$$special$$inlined$viewModel$1.invoke(ViewModelStoreOwnerExt.kt:45)
        com.example.findgithubretrofit2rx.ui.SearchFragment$$special$$inlined$viewModel$1.invoke(Unknown Source:0)
        kotlin.UnsafeLazyImpl.getValue(Lazy.kt:81)
        com.example.findgithubretrofit2rx.ui.SearchFragment.getVm(Unknown Source:2)
        com.example.findgithubretrofit2rx.ui.SearchFragment.observeLiveDate(SearchFragment.kt:45)
        com.example.findgithubretrofit2rx.ui.SearchFragment.onViewCreated(SearchFragment.kt:41)
        androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:322)
        androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1185)
        androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1354)
        androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1432)
        androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1495)
        androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2617)
        androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2569)
        androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
        androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
        androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:210)
        android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
        android.app.Activity.performStart(Activity.java:7029)
        android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
        android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        android.app.ActivityThread.-wrap11(Unknown Source:0)
        android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        android.os.Handler.dispatchMessage(Handler.java:106)
        android.os.Looper.loop(Looper.java:164)
        android.app.ActivityThread.main(ActivityThread.java:6494)
        java.lang.reflect.Method.invoke(Native Method)
        com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.findgithubretrofit2rx, PID: 30918
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.findgithubretrofit2rx/com.example.findgithubretrofit2rx.ui.MainActivity}: org.koin.core.error.InstanceCreationException: Could not create instance for [Factory:'com.example.findgithubretrofit2rx.ui.SearchViewModel']
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [Factory:'com.example.findgithubretrofit2rx.ui.SearchViewModel']
        at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:59)
        at org.koin.core.instance.FactoryInstanceFactory.get(FactoryInstanceFactory.kt:36)
        at org.koin.core.registry.InstanceRegistry.resolveInstance$koin_core(InstanceRegistry.kt:87)
        at org.koin.core.scope.Scope.resolveInstance(Scope.kt:214)
        at org.koin.core.scope.Scope.get(Scope.kt:181)
        at org.koin.androidx.viewmodel.ViewModelFactoryKt$defaultViewModelFactory$1.create(ViewModelFactory.kt:50)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
        at org.koin.androidx.viewmodel.ViewModelResolutionKt.get(ViewModelResolution.kt:21)
        at org.koin.androidx.viewmodel.ViewModelResolutionKt.resolveInstance(ViewModelResolution.kt:10)
        at org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel(ScopeExt.kt:70)
        at org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel(ScopeExt.kt:56)
        at org.koin.androidx.viewmodel.koin.KoinExtKt.getViewModel(KoinExt.kt:34)
        at org.koin.androidx.viewmodel.ext.android.ViewModelStoreOwnerExtKt.getViewModel(ViewModelStoreOwnerExt.kt:60)
        at com.example.findgithubretrofit2rx.ui.SearchFragment$$special$$inlined$viewModel$1.invoke(ViewModelStoreOwnerExt.kt:45)
        at com.example.findgithubretrofit2rx.ui.SearchFragment$$special$$inlined$viewModel$1.invoke(Unknown Source:0)
        at kotlin.UnsafeLazyImpl.getValue(Lazy.kt:81)
        at com.example.findgithubretrofit2rx.ui.SearchFragment.getVm(Unknown Source:2)
        at com.example.findgithubretrofit2rx.ui.SearchFragment.observeLiveDate(SearchFragment.kt:45)
        at com.example.findgithubretrofit2rx.ui.SearchFragment.onViewCreated(SearchFragment.kt:41)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:322)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1185)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1354)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1432)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1495)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2617)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2569)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:210)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
        at android.app.Activity.performStart(Activity.java:7029)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
E/AndroidRuntime: Caused by: org.koin.core.error.NoBeanDefFoundException: No definition found for class:'com.example.findgithubretrofit2rx.data.UserImpl'. Check your definitions!
        at org.koin.core.scope.Scope.throwDefinitionNotFound(Scope.kt:246)
        at org.koin.core.scope.Scope.resolveInstance(Scope.kt:216)
        at org.koin.core.scope.Scope.get(Scope.kt:181)
        at com.example.findgithubretrofit2rx.di.ModulesKt$mainModule$1$1.invoke(Modules.kt:19)
        at com.example.findgithubretrofit2rx.di.ModulesKt$mainModule$1$1.invoke(Unknown Source:4)
        at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:50)
            ... 41 more

Here, there're classes that I think its cause the error. SearchFragment.kt , SearchViewModel.kt, UserImpl.kt, RetrofitBuilder.kt

class SearchFragment : Fragment() {

    private val vm: SearchViewModel by viewModel()
    .....
    .......

class SearchViewModel(private val userImpl: UserImpl) : ViewModel() {

    private val disposable = CompositeDisposable()
    ....
    ......

class UserImpl(private val gitHubApi: GitHubApi) : UserRepository {

    override fun getSearchUser(str: String?): Observable<SearchUserResponse> {
        return gitHubApi.searchUsers(str)
    }
}

interface UserRepository {

    fun getSearchUser(str: String?): Observable<SearchUserResponse>
}

class RetrofitBuilder(val okHttpClient: OkHttpClient,
                      val converterFactory: Converter.Factory,
                      val adapterFactory: CallAdapter.Factory) {

    inline fun <reified T> build(baseUrl: String): T {
        return Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl(baseUrl)
            .addConverterFactory(converterFactory)
            .addCallAdapterFactory(adapterFactory)
            .build()
            .create(T::class.java)
    }
}

This is my modules.

val appModule = module {

    factory<UserRepository> { UserImpl(get()) }

    viewModel { SearchViewModel(get())}
}

val networkModule = module {
    single {
        HttpLoggingInterceptor().apply {
            this.level = HttpLoggingInterceptor.Level.BODY
        }
    }

    single {
        object : Interceptor {
            @Throws(IOException::class)
            override fun intercept(chain: Interceptor.Chain): Response {
                val original: Request = chain.request()
                val originalHttpUrl = original.url
                val url = originalHttpUrl.newBuilder()
                    .build()

                // Request customization: add request headers
                val requestBuilder = original.newBuilder().url(url)
                val request = requestBuilder.build()
                val response = chain.proceed(request)
                return response
            }
        }
    }

    single {
        OkHttpClient.Builder()
            .addInterceptor(get<HttpLoggingInterceptor>())
            .addInterceptor(get<Interceptor>())
            .build()
    }

    single<CallAdapter.Factory> { RxJava2CallAdapterFactory.create() }

    single<Converter.Factory> { GsonConverterFactory.create() }

    single { RetrofitBuilder(get(), get(), get()) }

    single<GitHubApi> { get<RetrofitBuilder>().build(GITHUB_BASE_URL) }
}

1 Answer 1

7

I think the problem is the in the names.

in app module you are creating instance for UserRepository

val appModule = module {

    factory<UserRepository> { UserImpl(get()) }

    viewModel { SearchViewModel(get())}
}

And in viewModel you are adding UserImpl

class SearchViewModel(private val userImpl: UserImpl)

Koin needs to know the same parameter that you are adding, even if it is an interface. But as a lot of websites recommends to build your software with abstractions instead of implementations, So with that in mind :). you should UserImpl in viewModel, try to add UserRepository interface.

class SearchViewModel(private val userRepository: UserRepository)
1
  • 1
    Try to clean the project and run again. If the problem persist, you can do this "viewModel { SearchViewModel(get< UserRepository>())}", add UserRepository context. And make sure you are setting the correct interface/class in koin module. Also make sure you are loading 'appModule' and 'networkModule'
    – rguzman
    Commented Jan 6, 2021 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.