Я занимаюсь разработкой новостного приложения и хочу добавить два модуля в класс приложения, но получаю следующее исключение.
java.lang.RuntimeException: Unable to create application yodgorbek.komilov.musobaqayangiliklari.di.application.SportNewsApplication: org.koin.core.error.DefinitionOverrideException: Already existing definition or try to override an existing one: [type:Single,primary_type:'yodgorbek.komilov.musobaqayangiliklari.internet.SportNewsInterface']
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5971)
at android.app.ActivityThread.access$1300(ActivityThread.java:206)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1700)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6820)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)
Caused by: org.koin.core.error.DefinitionOverrideException: Already existing definition or try to override an existing one: [type:Single,primary_type:'yodgorbek.komilov.musobaqayangiliklari.internet.SportNewsInterface']
at org.koin.core.registry.BeanRegistry.addDefinition(BeanRegistry.kt:144)
at org.koin.core.registry.BeanRegistry.saveDefinition(BeanRegistry.kt:101)
at org.koin.core.registry.BeanRegistry.saveDefinitions(BeanRegistry.kt:71)
at org.koin.core.registry.BeanRegistry.loadModules(BeanRegistry.kt:49)
at org.koin.core.KoinApplication.loadModulesAndScopes(KoinApplication.kt:66)
at org.koin.core.KoinApplication.modules(KoinApplication.kt:60)
at yodgorbek.komilov.musobaqayangiliklari.di.application.SportNewsApplication$onCreate$1.invoke(SportNewsApplication.kt:19)
at yodgorbek.komilov.musobaqayangiliklari.di.application.SportNewsApplication$onCreate$1.invoke(SportNewsApplication.kt:11)
at org.koin.core.context.GlobalContextKt.startKoin(GlobalContext.kt:72)
at yodgorbek.komilov.musobaqayangiliklari.di.application.SportNewsApplication.onCreate(SportNewsApplication.kt:16)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1155)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5966)
... 8 more
ниже класса SportNewsApplication.kt
class SportNewsApplication : Application() {
override fun onCreate() {
super.onCreate()
// Adding Koin modules to our application
startKoin {
// androidContext(this@SportNewsApplication)
modules(
listOf(appModules, bbcModules))
}
}
}
ниже appModules.kt
const val BASE_URL = "https://newsapi.org/"
val appModules = module {
// The Retrofit service using our custom HTTP client instance as a singleton
single {
createWebService<SportNewsInterface>(
okHttpClient = createHttpClient(),
factory = RxJava2CallAdapterFactory.create(),
baseUrl = BASE_URL
)
}
// Tells Koin how to create an instance of CatRepository
factory<NewsRepository> { (NewsRepositoryImpl(sportsNewsApi = get())) }
// Specific viewModel pattern to tell Koin how to build MainViewModel
viewModel { MainViewModel(newsRepository = get()) }
}
/* Returns a custom OkHttpClient instance with interceptor. Used for building Retrofit service */
fun createHttpClient(): OkHttpClient {
val client = OkHttpClient.Builder()
client.readTimeout(5 * 60, TimeUnit.SECONDS)
return client.addInterceptor {
val original = it.request()
val requestBuilder = original.newBuilder()
requestBuilder.header("Content-Type", "application/json")
val request = requestBuilder.method(original.method, original.body).build()
return@addInterceptor it.proceed(request)
}.build()
}
/* function to build our Retrofit service */
inline fun <reified T> createWebService(
okHttpClient: OkHttpClient,
factory: CallAdapter.Factory, baseUrl: String
): T {
val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addCallAdapterFactory(factory)
.client(okHttpClient)
.build()
return retrofit.create(T::class.java)
}
ниже bbcModules.kt
const val base_url = "https://newsapi.org/"
val bbcModules = module {
// The Retrofit service using our custom HTTP client instance as a singleton
single {
createBBCWebService<SportNewsInterface>(
okHttpClient = createBBCHttpClient(),
factory = RxJava2CallAdapterFactory.create(),
baseUrl = base_url
)
}
// Tells Koin how to create an instance of CatRepository
factory<BBCRepository> { (BBCRepositoryImpl(bbcsportNewsApi = get())) }
// Specific viewModel pattern to tell Koin how to build MainViewModel
viewModel { BBCSportViewModel(bbcRepository = get()) }
}
/* Returns a custom OkHttpClient instance with interceptor. Used for building Retrofit service */
fun createBBCHttpClient(): OkHttpClient {
val client = OkHttpClient.Builder()
client.readTimeout(5 * 60, TimeUnit.SECONDS)
return client.addInterceptor {
val original = it.request()
val requestBuilder = original.newBuilder()
requestBuilder.header("Content-Type", "application/json")
val request = requestBuilder.method(original.method, original.body).build()
return@addInterceptor it.proceed(request)
}.build()
}
/* function to build our Retrofit service */
inline fun <reified T> createBBCWebService(
okHttpClient: OkHttpClient,
factory: CallAdapter.Factory, baseUrl: String
): T {
val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addCallAdapterFactory(factory)
.client(okHttpClient)
.build()
return retrofit.create(T::class.java)
}
то, что я пробовал
1.clean перестроить и аннулировать перезапуск кэша и другие ответы StackOverflow, которые он сделал не решить мою проблему.
Я также перешел по следующей ссылке
https://github.com/InsertKoinIO/koin/issues/420 Я хочу знать, что мне нужно сделать, чтобы устранить исключение.