-
-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiplatform expect / actual module support #105
Comments
Interesting feedback. Let's see how we can get close to the actual/expect KMP declarations 👍 |
You shouldn't need to provide the context in your module constructor. // commonMain
expect class SettingsPlatformModule
@Module(includes = [SettingsPlatformModule::class])
class SettingsModule
// androidMain
@Module
@ComponentScan("org.example")
actual class SettingsPlatformModule IMHO it would be even better to have @componentscan support across targets. @Module
@ComponentScan("app.settings")
class SettingsModule And in src/androidMain/kotlin, without having a module: @Single
class Settings(val context: Context) {
...
} Currently, i have to declare one module per platform |
In my opinion, it works even better now. If you have a module in common, for example @Single
fun provideAndroidPersistence(context: Context) : Persistence = AndroidPersistence(context) Without creating a separate module for this. Component Scan will put this dependency in the |
@Neyasbit This looks promising. I'm trying to implement this like you described, but does not work for me with latest Koin version. The Component scan does not seem to pick up dependencies from my source sets. Am I missing something? |
@matejsemancik ,
If your module has only platform dependencies, then you should remove generation |
@Neyasbit indeed, looks like we had some ksp misconfiguration in our project that prevented annotations compiler from generating sources outside of commonMain. I managed to solve the problem using suggestions you provided, but there are still some quirks (see mentioned issue 👆) |
The annotated module in commonMain seems to be super interesting approach to specify expect/actual components. Thanks @Neyasbit for pointing it. Example: // in commonMain
@Module
@ComponentScan("com.jetbrains.kmpapp.platform")
class PlatformModule
// package com.jetbrains.kmpapp.platform
expect class PlatformHelper {
fun getName() : String
}
// in androidMain
// package com.jetbrains.kmpapp.platform
@Single
actual class PlatformHelper(
val context: Context
){
actual fun getName(): String = "I'm Android - $context"
}
// in nativeMain
// package com.jetbrains.kmpapp.platform
@Single
actual class PlatformHelper(){
actual fun getName(): String = "I'm Native"
} Good thing it can work on top level function too. Your contract comes from expect class/functions. dependencies {
add("kspCommonMainMetadata", libs.koin.ksp.compiler)
add("kspAndroid", libs.koin.ksp.compiler)
add("kspIosX64", libs.koin.ksp.compiler)
add("kspIosArm64", libs.koin.ksp.compiler)
add("kspIosSimulatorArm64", libs.koin.ksp.compiler)
} |
Added documentation in https://github.com/InsertKoinIO/koin-annotations/pull/139/files |
I tried testing this with |
Yes config check still need work. Looking at it. Closing this one 👍 Follow up on #140 |
Is your feature request related to a problem? Please describe.
I'm going to describe my problem using example.
I have a Multiplatform dependency called
Persistence
which has platform-specific implementations:I would like to provide this
Persistence
dependency in mycommonMain
source sets using annotated Koin module. My approach is to create an expect/actual module class namedPersistencePlatformModule
and annotate it using@Module
annotation, where actual implementation provides platform-specific dependency for creating an instance ofPersistence
(for example Android needs an application Context which is also provided by Koin but only in androidMain source set).Then I include that module into my other module
PersistenceModule
which contains the rest of other dependencies I use in my project:The compilation of above fails with some errors:
Expected class PersistencePlatformModule does not have default constructor
No value passed for parameter 'context'
My next approach was to define a module without expected dependency function:
This will compile without compile-time checks and
PersistenceModule.module
extension is generated. However whenPersistence
is injected at runtime, the application crashes withNoBeanDefFoundException
.This will also not compile with compile-time checks:
Describe the solution you'd like
I would like to be able to specify expect/actual modules using Koin annotations using one of described approaches above ☝️
Describe alternatives you've considered
The alternative is to not use koin-annotations and fall back to typical module-as-function approach:
This approach works, however when combined with other
koin-annotations
modules in your project, you cannot use compile-time checks because they fail to check dependencies provided by this functional-style module.Target Koin project
koin-annotations
The text was updated successfully, but these errors were encountered: