Is there anyone using kotlin-inject on a multiplat...
# kotlin-inject
l
Is there anyone using kotlin-inject on a multiplatform project with natural targets (e.g. “iosMain”) and able to generate ksp sources that are available to be consumed?
r
I'm working with multiplatform with ios but barely getting to test it. i'll let you know if i get it to work
l
I think I made it work, but it seems that this issue is making everything so much harder: https://github.com/google/ksp/pull/1021
r
Yeah, I ended up switching to Koin as it seems to be much easier to work with when it comes to multiplatform support
p
Yep we do that. You can just add KSP for all iOS targets. The ide won't understand it but its working fine
l
Sweet, so I think I’m on the right path. Just to confirm, on your shared module, you have something like:
Copy code
kotlin {
  sourceSets {
    iosX64Main {
      dependencies {
        kotlin.srcDir("build/generated/ksp/iosX64/iosX64Main/kotlin")
      }
    }
    iosArm64Main {
      dependencies {
        kotlin.srcDir("build/generated/ksp/iosArm64/iosArm64Main/kotlin")
      }
    }
    iosSimulatorArm64Main {
      dependencies {
        kotlin.srcDir("build/generated/ksp/iosSimulatorArm64/iosSimulatorArm64Main/kotlin")
      }
    }
  }
}

dependencies {
  add("kspIosX64", "me.tatarka.inject:kotlin-inject-compiler-ksp:0.5.1")
  add("kspIosArm64", "me.tatarka.inject:kotlin-inject-compiler-ksp:0.5.1")
  add("kspIosSimulatorArm64", "me.tatarka.inject:kotlin-inject-compiler-ksp:0.5.1")
}
right? Are you using
kspCommonMainMetadata
anywhere? It seems not necessary for this case (of having code outside commonMain).. I haven’t played much with this and I’m wary of iOS archiving/deployments going weird due to (maybe?) some repeated code-gen, once per mArch?
p
Yes it will be repeated. No issues though. We have no commonMainMetaData in place. Once KSP supports this we will switch ofc
l
thank you for confirming 🙏 I found this MR (https://github.com/google/ksp/issues/965) and it seems to be all that we want/need. hopefully it’ll land soon!
d
I’m having the same issue. If I put this in my iosMain
Copy code
@Component
abstract class Foo

fun foo() {
  Foo::class.create()
}
the
create()
function doesn’t exist. Have any of you found some more information?
l
Are you using Kotlin 2.x?
Pre Kotlin 2.0 it was possible to reference the
create()
function from shared source-sets because code from a target source-set was visible to the shared source-set. Starting from Kotlin 2.0 shared source sets can no longer see code from target source-sets, so you need to use
@KmpComponentCreate
d
hi @leandro thanks. Yes I’m using Kotlin 2.0.21 atm. And I later discovered
@KmpComponentCreate
I had missed it. It kinda helps. But I hope that this part of KMP will improve in the future.
226 Views