https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

andylamax

06/07/2021, 6:09 PM
Hello everyone, I am trying to generate Multiplatform code at build time. can KSP do this? or do I need to dive into Compiler Plugins?
d

Dominaezzz

06/07/2021, 6:09 PM
Depends on what inputs you need for code generation.
a

andylamax

06/07/2021, 6:16 PM
I am trying to generate something like this
Copy code
class GeneratedAction {
  fun execute(param: Param)
}
From a source code like this
Copy code
annotation class Generate

class Param(val id: Int, val name: String)

@Generated
open class GenericAction

class Action(val input: Param) : GenericAction
d

diesieben07

06/07/2021, 6:18 PM
Just generating new code can be done with KSP. It's basically like Java APT, but with a Kotlin-API and supporting kotlin multiplatform (afaik)
But you cannot alter the code (just like with Java APT)
a

andylamax

06/07/2021, 6:20 PM
Music to my ears. Thanks @diesieben07 will look deeper onto KSP then. Coz those Compiler Plugins still scare me
d

diesieben07

06/07/2021, 6:20 PM
well, KSP really is a compiler plugin itself - it just gives you a nice (and stable) API
a

andylamax

06/07/2021, 6:21 PM
Then it is even a better stepping stone
d

diesieben07

06/07/2021, 6:21 PM
You of course don't interact with the Kotlin compiler directly, KSP does that for you. But your code runs as part of the kotlin compiler
b

Big Chungus

06/07/2021, 9:35 PM
Ksp is still jvm-only
Mpp support planned for kotlin 1.5.20
d

diesieben07

06/07/2021, 9:40 PM
Thanks for clarifying, I wasn't sure on that part
Do you have any links on that @Big Chungus? I only found this: https://github.com/google/ksp/issues/60 which is just for making KSP work on the JVM part of a MPP project (not actual "common code").
r

russhwolf

06/07/2021, 10:43 PM
It's come up on Slack a couple times, for example here: https://kotlinlang.slack.com/archives/C013BA8EQSE/p1621877970017600
🙏 1