https://kotlinlang.org logo
#ksp
Title
# ksp
j

jean

12/29/2022, 7:55 AM
When using ksp in a multiplatform project, is it possible to know which target is used when ksp code is ran? I target jvm, ios and js with this configuration:
Copy code
dependencies {
    add("kspCommonMainMetadata", project(":processor"))
    add("kspJvm", project(":processor"))
    add("kspJvmTest", project(":processor"))
    add("kspJs", project(":processor"))
    add("kspJsTest", project(":processor"))
    add("kspIosX64", project(":processor"))
    add("kspIosX64Test", project(":processor"))
}
I end up with a lot of
Redeclaration: MyClass
since all classes under
generated/ksp/js
,
generated/ksp/jvm
and
generated/ksp/ios
have the same package
com.jeantuffier.somePackage
. I’m using this to generate the package line of each file
val packageName = classDeclaration.packageName.asString()
. Is it possible to append it with
jvm
,
ios
or
js
based on the target?
Or am I doing it all wrong and should go for another way of generating the code?
g

glureau

12/29/2022, 8:38 AM
The redeclaration may came from: • source + test source: when you declare Jvm + JvmTest you generate classes for both, so when you run your tests it should conflict • common + platform sources: if the class is generated by kspCommonMain, it should already be available for all platforms, so I expect a conflit with kspJvm since it will generate the same class but in another directory.
I suppose you could limit to just common main and the code should be usable from every platform.
j

jean

12/29/2022, 8:44 AM
I did that first and it did generate code without conflict. But then my test don’t pass because generated classes can’t be resolved even though I added
Copy code
val commonTest by getting {
    dependencies {
        ...
    }
    kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
}
And this is not found
Copy code
add("kspCommonMainMetadataTest", project(":processor"))
And
kspCommonTestMetadata
is not found
g

glureau

12/29/2022, 8:51 AM
You use the generated classes only from tests?
j

jean

12/29/2022, 9:09 AM
I’m testing a class I made that uses a generated class. But when I run the test I get an error saying the generated class can’t be resolved
f

Foso

12/29/2022, 10:51 AM
You can get the current target platform from SymbolProcessorEnvironment
j

jean

12/30/2022, 5:57 AM
I returns me an empty list
That returns all the targets, how can I know which one is the current target?
152 Views