Looking to migrate KMP project (<https://github.co...
# kotlin-inject
j
Looking to migrate KMP project (https://github.com/joreilly/BikeShare) over to using
kotlin-inject-anvil
and just struggling a little with
createComponent
etc plumbing. I have per platform application components and can't do likes of following in those (expect function needs to be in
commonMain
). Are there any examples on how to setup something like this?
Copy code
@MergeComponent.CreateComponent
expect fun KClass<DesktopApplicationComponent>.createComponent(): DesktopApplicationComponent
e
If you're creating the component in the platform application module, then you shouldn't need
CreateComponent
at all, since you can just call
DesktopApplicationComponent::class.create
.
CreateComponent
is only needed if you're creating your component in a common source set, because the common source set can't see the
create
function which is created in the target source set.
j
thanks.....still getting issues but likely something in my setup.....it's complaining about following generated code
Copy code
public fun KClass<DesktopApplicationComponent>.create(): DesktopApplicationComponent =
    KotlinInjectDesktopApplicationComponent::class.create()
Copy code
e: file:///Users/joreilly/dev/github/BikeShare/common/build/generated/ksp/jvm/jvmMain/kotlin/dev/johnoreilly/common/di/KotlinInjectDesktopApplicationComponent.kt:22:52 None of the following candidates is applicable:
fun KotlinInjectDesktopApplicationComponent.Companion.create(): KotlinInjectDesktopApplicationComponent
fun KClass<DesktopApplicationComponent>.create(): DesktopApplicationComponent
e
Do you set
me.tatarka.inject.generateCompanionExtensions=true
?
j
yeah, have following....and that's what allowed existing code to invoke
create
Copy code
ksp {
    arg("me.tatarka.inject.generateCompanionExtensions", "true")
}
so, just to clarify, project working fine with
kotlin-inject
.....just running in to issues migrating to
kotlin-inject-anvil
e
You might have to either remove that arg or do a clean build. I had a similar issue migrating and I just removed that arg.
h
@eygraber how's the experience regarding dependencies provision from the iOS side Let's say i have an interface in commonMain and implementation of Android inside androidMain but the iOS is done in Swift
j
Ah ok, thanks....I'll try that
@eygraber thanks, that fixed that! Did you have to do anything specific for iOS case. I previously had following in swift but that's not working any more
Copy code
let applicationCompoonent = IosApplicationComponent.companion.create()
e
I don't have any experience instantiating the component in Swift (I do everything that I can in Kotlin), but if you removed
me.tatarka.inject.generateCompanionExtensions
then there is no
create
function on the companion anymore. I don't know if the
KClass
is accessible from Swift, but if not you could make a top level function in Kotlin that calls
create
on the
KClass
and call that function from Swift.
j
ok, added following to Kotlin
Copy code
@MergeComponent.CreateComponent
expect fun create(): IosApplicationComponent
and was able to use then in Swift like following
Copy code
let applicationCompoonent = IosApplicationComponentCreateComponentKt.create()
e
Ah, yeah if you use a common ios source set you'd need to do that