Hello, I would like to generate source code for di...
# ksp
c
Hello, I would like to generate source code for different targets in a KMP project based on annotations in common. I would like to generate the following: • 1 expect class for common • 2 actual classes for Android and iOS. I am able to generate for one of the targets, depending on which configuration I add the ksp task to. This way I could run the processor per target and let it generate code per target. But it seems more appropriate to generate the code for all targets in one round, during kspMetadata, except that I do not know how to write the source code to other source sets than common from that task. Any suggestions on how to do this?
y
i thought common code never compiled until it is compiled as part of a target? i might be wrong but seems like you just want to operate on the edge nodes (target platforms) and do it for 1 platform
a
That’s an interesting problem… you could have a “common” processor that generates the
expect
in common using
kspMetadata
, and then separate “platform” processor(s) that generate the
actual
for each platform. (maybe the “common” processor generates an “internal” annotation to communicate with the platform processors if needed?) That doesn’t sound very straightforward though
I also think common code won’t get compiled until it’s part of a target, but it does seem reasonable to want to analyze common code (and only common code) to generate common code that other common code can use
c
I was able to generate common code in the KMP lib and use it in the consumer Android & iOS app directly. As long as the code is not marked
internal
is seems part of the public API of the module. So I guess it just get compiled? 🤷‍♂️ @Alex Vanyo that was indeed a solution that came to mind, should work to have processor run separately for each platform and have it generate it's part. But this feels a bit inefficient and illogical since the generated code of all platforms is so closely coupled and it would make more sense to generate it in one go. I might be able to just stick to
kspMetadata
and maybe change the output dirs to the platform source sets, but I should know what those dirs should be then.
a
Just curious, for generating the common code, did you use
kspMetadata
or did you add a
ksp
task for each target?
c
Just
kspMetadata
👍 1
I will for now try out the multi processor approach. Will let you know when I have something working. Thanks for the input!