I realised that Kotlin's @Parcelize annotation won't work for me as part of my class that need to be Parcelable stored in non-android modules.
- common (contains classes that needs to be Parcelable)
- desktop-editor
- android-app
I think that I can annotate classes in common module and then serialize them to byte[] in android-app module to store them inside Bundle just like Parcelable do.
So, how can I manage this?
You can use the protobuf format to do this. As an alternative you could use the multiplatform support to handle the differences with a common codebase.
You can also use CBOR. It can be more convenient, since you don't need to mark up your class with @SerialId.
However, it's a good idea for FR to provide @Serializable - based implementation of Parcelable to be able to save on Android serializable classes from common code.
Are you serializing in the desktop module? If not, you should be able to just create a dummy annotation in common and typealias it to Parcelize in the android module.
Parcelize plugin requires making class implements Parcelable by hand, as far as I remember
Sure, but that interface can also be typealiased back to the real thing and made empty in the desktop module.
@JakeWharton on desktop I serialize data as json. Nice Idea btw, but I will try to use CBOR
Wouldn't using CBOR/JSON/protobuf (assuming there is a library involved) format incur more memory/CPU at runtime as well as boilerplate compared to using @Parcelize that generates the code at build time?
Also I'm kind of curious, how would we also annotate the same model with @Entity to ensure we can use it in a Room database? That way we can define a single model that can be used between several platforms and we wont have to include boilerplate to convert between models (pojo to json/protobuf/CBOR/SQL)
@JakeWharton do you have an example of that anywhere?
No, but just do the same thing I outlined above with typealiases
Partial actual-ing of common expect annotations just shipped in 1.2.60-eap-44 (aka RC) meaning you no longer need to actual platform-specific annotations on platforms for which they don't make sense which makes this slightly easier.
So how can i use kotlinx serialized data class in android-app module to store data inside the Bundle?
@dzastinas You would need to write a format that writes the data to a parcel (probably not to a bundle). I'm not aware of one already existing, but it isn't too hard.
@dzastinas I've just published a lib that does just that: https://github.com/AhmedMourad0/bundlizer
@pdvrieze Can you elaborate please, why not a Bundle?
Most helpful comment
Partial actual-ing of common expect annotations just shipped in 1.2.60-eap-44 (aka RC) meaning you no longer need to actual platform-specific annotations on platforms for which they don't make sense which makes this slightly easier.