Hello, Im trying to setup koin with annotations on...
# koin
c
Hello, Im trying to setup koin with annotations on a CMP project where I have to inject classes written in Swift and injecting it into common code with KOIN. Previously this used to work.
Copy code
fun initKoinWithPlatformModules(
    config: KoinAppDeclaration? = null,
    platformModules: List<Module> = emptyList()
) : Koin {
    return startKoin {
        config?.invoke(this)
        modules(
            listOf(
                ComposeAppModule().module,
                CoreDataModule().module,
                CorePresentationModule().module,
                OnboardingPresentationModule().module,
                StorePresentationModule().module
            ) + platformModules
        )
    }.koin
}

fun provideAnalyticsModule(analyticsService: AnalyticsService): Module = module {
    single<AnalyticsService> { analyticsService }
}
Copy code
class DependenciesProviderHelper {

    companion object {
        // Use a nullable with explicit initialization check
        private var _koin: Koin? = null

  val koin: Koin
            get() = _koin ?:  error("Koin not initialized.  Call initKoin() first.")

  fun setKoin(koin: Koin) {
            _koin = koin
        }
    }

 fun initKoin(config: KoinAppDeclaration? = null) : Koin {
        val instance = startKoin<PurrTunesKoinApp> {
            config?.invoke(this)
            analytics()
        }

        setKoin(instance.koin)
        return koin
    }

 fun initKoinWithPlatformModules(
        config: KoinAppDeclaration? = null,
        platformModules: List<Module> = emptyList()
    ) : Koin {
        val instance = startKoin<PurrTunesKoinApp> {
            config?.invoke(this)
            analytics()
            modules(
                platformModules
            )
        }

        setKoin(instance.koin)
        return koin
    }
}
Copy code
public fun getFeatureFlagManagerForIos(): FeatureFlagManager {
    return DependenciesProviderHelper.koin.get<FeatureFlagManager>()
}

public fun getKoinForIos() = DependenciesProviderHelper.koin

@OptIn(BetaInteropApi::class)
fun Koin.getFromKoin(objCClass: ObjCClass): Any {
    val kClazz = getOriginalKotlinClass(objCClass)
    if (kClazz == null) {
        throw IllegalArgumentException(
            "Cannot find Kotlin class for ObjC class: $objCClass.  " +
                    "Make sure the class is exported to iOS."
        )
    }
    return get(kClazz, null, null)
}

@OptIn(BetaInteropApi::class)
fun Koin.getFromKoin(objCClass: ObjCClass, qualifier: Qualifier?, parameter: Any): Any {
    val kClazz = getOriginalKotlinClass(objCClass)
    if (kClazz == null) {
        throw IllegalArgumentException(
            "Cannot find Kotlin class for ObjC class: $objCClass.  " +
                    "Make sure the class is exported to iOS."
        )
    }
    return get(kClazz, qualifier) { parametersOf(parameter) }
}

@OptIn(BetaInteropApi::class)
fun Koin.getProtocolFromKoin(objCProtocol: ObjCProtocol): Any {
    val kClazz = getOriginalKotlinClass(objCProtocol)
    if (kClazz == null) {
        throw IllegalArgumentException(
            "Cannot find Kotlin class for ObjC protocol: $objCProtocol.  " +
                    "Make sure the class is exported to iOS."
        )
    }
    return get(kClazz, null, null)
}

@OptIn(BetaInteropApi::class)
fun Koin.getProtocolFromKoin(objCProtocol: ObjCProtocol, qualifier: Qualifier?, parameter: Any): Any {
    val kClazz = getOriginalKotlinClass(objCProtocol)
    if (kClazz == null) {
        throw IllegalArgumentException(
            "Cannot find Kotlin class for ObjC protocol: $objCProtocol.  " +
                    "Make sure the class is exported to iOS."
        )
    }
    return get(kClazz, qualifier) { parametersOf(parameter) }
}
In IOS app delegate
Copy code
let analyticsModule = InitKoinKt.provideAnalyticsModule(analyticsService: analyticsService)

DependenciesProviderHelper().doInitKoinWithPlatformModules(
   config: nil, platformModules: [analyticsModule]
)

 let featureFlagManager: FeatureFlagManager = KoinHelperKt.getFeatureFlagManagerForIos()
Now when running IOS app I get error error: [Koin] Missing definition: kotlin.Any resolved by: getAny() No matching definition found in any declared module. Check your declaration with Annotation or DSL.
👌 1
I think this might be related to me adding platform module while initialising which over writes other modules the annotation plugin has added. Is there a way to add custom modules at run time?
f
c
Hello, thanks Im following this approach now
a
@François your help for swift could be good to be generalized. Can it be directly exposed from Koin itself?
f
I could try something now that swift-export is available, but no promise 😄
a
it's a recurring request about easy way to export Koin deps to Swift 🙂