Search code examples
androidkoin

Error while resolving instance for class Koin


Help solve the problem!(((I have 3 modules for DI. There is a retrofit object in natworkModule, all viewModels in viewModelModule, and all requests to the server in respositoryModule. I did everything according to the documentation, but I cannot find this error in Google. Thank you in advance!!! Sorry for my english!)

class App : Application(){

    override fun onCreate() {
        super.onCreate()
        startKoin(this, listOf(natworkModule, viewModelModule,repositoryModule))
       }
    }


var natworkModule = module {

  single { createOkHttpClient() }
  single { createApiService<ApiService>(get () ,getProperty(SERVER_URL)) 

  }

}


const val SERVER_URL = "https://api.github.com/"

fun createOkHttpClient() : OkHttpClient{
   val httpLoggingInterceptor = HttpLoggingInterceptor()
   httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC
   return OkHttpClient.Builder()
        .connectTimeout(60L, TimeUnit.SECONDS)
        .readTimeout(60L, TimeUnit.SECONDS)
        .addInterceptor(httpLoggingInterceptor).build()
   }

 inline fun <reified T> createApiService(okHttpClient: OkHttpClient,  url: String): T {
   val retrofit = Retrofit.Builder()
        .baseUrl(url)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(LiveDataCallAdapterFactory()).build()
   return retrofit.create(T::class.java)
 }

var repositoryModule = module {
    factory<TestRepository> {
        TestRepositoryImpl(get())
    }

}

var viewModelModule = module {
    viewModel {
        TestViewModel(get())
    }
}

Solution

  • Problem was in this constant value -> SERVER_URL = "https://api.github.com/" Koin could not find it. Therefore there was an exception. Thanks to all!!!