Custom data
You can pass custom objects through JNI boundary by marking them with JniSerializable annotation. The KNI processor will generate serializers to convert your data from and to a ByteArray.
import com.dshatz.kni.serialization.JniSerializable
@JniSerializable
data class ColorfulObject(
val color: String,
val weight: Double
)
@JniSerializable
value class Amount(val amountCents: Long) {
val amount: Double get() = amountCents / 100.0
}
You can then use this type in your JNI calls:
// commonMain
@JniCall
expect fun passObject(obj: ColorfulObject): Amount
// nativeMain
@JniCall
actual fun passObject(obj: ColorfulObject): Amount {
return if (obj.color == "orange") Amount(150)
else if (obj.color == "green") Amount(100)
else Amount(80)
}
Supported class types:
data classvalue classsealed class- polymorphic serializer will be generated
Customizing serialization for properties
Apply a @JniSerializable(with = ...) annotation to a property of data class to supply your own implementation of a JniSerializer.
Last modified: 16 July 2026