protobuf-without-codegen-kmp
Installation
SKILL.md
Field numbers on plain data classes, no generated layer
A schema compiler emits code for one platform. In a multiplatform module that is fatal: the
generated types compile on the JVM and strand every other target, so the whole protocol layer ends
up behind expect/actual or duplicated. The alternative is to write the messages by hand and
carry the field numbers as annotations, letting the serialization library produce the same bytes:
// adapted — the message names, field names and numbering are placeholders; the mechanism is real
@Serializable
data class Frame(
@ProtoNumber(1) val kind: String = "",
@ProtoNumber(2) val body: ByteArray = ByteArray(0),
@ProtoNumber(3) val packed: Boolean = false,
)
@OptIn(ExperimentalSerializationApi::class)
private val proto = ProtoBuf { encodeDefaults = false }