Kotlin Multiplatform generated code is not aware of target's code

179 Views Asked by At

I've a KMP shared module that imports a KSP library to generate code for iOS targets, and it works as expected:

enter image description here

However, I’m facing a strange behavior; my generated code cannot see/access what’s in iosMain (generated code imports classes from iosMain).

My configuration:

val iosMain by creating {
    dependsOn(commonMain)
    dependencies {
        // ...
    }
}

listOf(iosX64, iosArm64, iosSimulatorArm64).forEach { target ->
    getByName("${target.targetName}Main") {
        dependsOn(iosMain)
    }

    val kspConfigName = "ksp${target.name.replaceFirstChar { it.uppercaseChar() }}"
    dependencies.add(kspConfigName, "my-ksp-lib")
}

What am I doing wrong or missing?

1

There are 1 best solutions below

0
GuilhE On BEST ANSWER

By default, IntelliJ IDEA or other IDEs don't know about the generated code. So it will mark references to generated symbols unresolvable. To make an IDE be able to reason about the generated symbols, mark the following paths as generated source roots:

all {       
    kotlin.srcDir("build/generated/ksp/${target.targetName}/${target.targetName}Main/kotlin")
}

More information about it here in kotlinlang docs