Hi :wave: I'm an iOS engineer playing around with ...
# koin
a
Hi 👋 I'm an iOS engineer playing around with KMP and particularly the SwiftExport feature. I'm a little bit struggling with my Koin setup, because no matter what I do I keep getting lots of errors like this one below, coming from the code generated by KMP.
Copy code
e: file:///Users/.../repositories/myRepo/sharedKit/build/SwiftExport/iosSimulatorArm64/Debug/files/IoInsertKoinKoinCore/IoInsertKoinKoinCore.kt:1109:76 API marked as @KoinInternalAPI and is intended for internal framework use only. It may change or be removed in future releases without notice. External usage is strongly discouraged.
I tried the DSL and the annotation. I also tried to add the following compiler option
optIn.add("org.koin.core.annotation.KoinInternalApi")
, but I always get the same result and I don't understand what's going on and why. As soon as I make a module's declaration public, I get the error. I'm using Kotlin
2.4.10
and Koin
4.2.2
. FYI, I didn't have this problem with the Obj-C export. I can't provide an example right now, but I'll try to put something together if required. Is there a particular way to setup Koin with SwiftExport? Is there any known limitations or even incompatibilty?
👍 1
👀 1
t
I also gave up on using koin-annotations for classes exposed through Swift Export. I’m using koin-dsl to expose them instead. If you find any better approach or improvements, I’d appreciate it if you could share them with me as well.
a
I ended up keeping everything related to Koin internal to each module, then exposing a public method to call
loadKoinModules
.
Copy code
startKoin {
  AuthenticationDomainModule.load()
  GreetingDomainModule.load()
}

object AuthenticationDomainModule {
    fun load() {
        loadKoinModules(domainModule)
    }
}

internal val domainModule = module {
    includes(dataModule)

    factory { AuthenticationUseCaseImpl(get()) }
}
👍 1
a
what kind of internal API do you expose?
a
Initially I declared a class to expose every public implementation to iOS :
Copy code
class KoinDependencies: KoinComponent {
    val authenticationUseCaseImpl: AuthenticationUseCaseImpl by inject()
    val greetingUseCaseImpl: GreetingUseCaseImpl by inject()
}
Each module is declaring whats available :
Copy code
val greetingDomainModule = module {
    includes(dataModule)

    factory { GreetingUseCaseImpl(get()) }
}
Then I had a free function to init koin:
Copy code
internal val allModules = buildList {
    addAll(greetingDomainModule)
}

fun initKoin() {
    startKoin {
        modules(allModules)
    }
}
But I replaced that with an object
Copy code
// Inside each module
object GreetingDomainModule {
    fun load() {
        loadKoinModules(greetingDomainModule)
    }
}

internal val greetingDomainModule = module {
    includes(dataModule)

    factory { GreetingUseCaseImpl(get()) }
}

// Shared initialisation 
object iOSInjection {
    fun initKoin() {
        startKoin {
            GreetingDomainModule.load()
        }
    }

    fun greetingUseCase(): GreetingUseCaseImpl = getKoin().get()
}
Not sure it answers your question well, but let me know 🙂