matsem
11/21/2023, 9:53 AMkoin-annotations
in KMP project, can I include expect/actual class module inside another module? The following setup fails during build with: Expected class PersistencePlatformModule does not have default constructor
and I’m not sure if there’s an issue with my ksp setup, or there’s just no support for these things right now:
// src/commonMain/
@Module([PersistencePlatformModule::class])
class PersistenceModule { ... }
@Module
expect class PersistencePlatformModule {
expect fun platformSpecificDependency() = ...
}
// src/androidMain/
@Module
actual class PersistencePlatformModule(androidContext: Context) { ... }
// src/iosMain/
@Module
actual class PersistencePlatformModule() { ... }
Christopher Mederos
11/21/2023, 10:23 PMfun initKoin(platformModule: Module = module { }, appDeclaration: KoinAppDeclaration = { }) =
startKoin {
appDeclaration()
modules(commonModule(), platformModule)
}
Christopher Mederos
11/21/2023, 10:27 PM// commonMain
expect fun platformModule() : Module
// iosMain
actual fun platformModule() = module {
single<HttpClientEngine> { Darwin.create() }
}
// androidMain
actual fun platformModule() = module {
// android implementation of http client for ktor
single<HttpClientEngine> { Android.create() }
single<HttpClientConfig<AndroidEngineConfig>> { HttpClientConfig() }
}
Christopher Mederos
11/21/2023, 10:30 PMmatsem
11/22/2023, 9:10 AMburnoo
11/25/2023, 5:22 PM// src/commonMain/
@Module([PersistencePlatformModule::class])
class PersistenceModule() { ... }
@Module
expect class PersistencePlatformModule() { }
// src/androidMain/
@Module
actual class PersistencePlatformModule actual constructor() {
fun someDependency(androidContext: Context) : SomeDependency(androidContext)
}
// src/iosMain/
@Module
actual class PersistencePlatformModule actual constructor() { ... }
matsem
11/27/2023, 10:39 AMPersistencePlatformModule
as expected function in order for Koin to pick it up, but this cannot be done when actual implementations have different function signature (androidMain has Context parameter and iosMain has no parameter).
You could omit the expected function from PersistencePlatformModule
and just define dependencies as classic functions in each actual class implementation like in example you provided, but Koin will then crash at runtime with NoBeanDefFoundException
because it apparently does not know about the dependency when it is not commonMain module.arnaud.giuliani
12/05/2023, 10:19 AMarnaud.giuliani
12/05/2023, 10:19 AMarnaud.giuliani
12/05/2023, 10:19 AMmatsem
12/06/2023, 8:10 AMmatsem
12/07/2023, 9:19 AMNacho Ruiz Martin
04/13/2024, 8:14 PMarnaud.giuliani
04/15/2024, 1:47 PMNacho Ruiz Martin
04/17/2024, 8:11 AM