I have some questions related to the `kotlin-parce...
# android
m
I have some questions related to the
kotlin-parcelize
plugin and how best to use it in a modularized project setup: In an effort to modularize a monolithic Android app module, step 1 (ideally) is to extract common model classes into standalone modules for re-use. Based on the project setup, we want these new modules to be plain JVM modules (no
com.android.library
plugin). This becomes problematic if any of the model classes are
Parcelable
and utilize the parcelize plugin -
kotlin-parcelize
can't run in a module without applying the
com.android.library
plugin. Any recommendations on how to work around this? I can come up with two options myself, but neither one is necessarily ideal; I'm curious if anyone has other ideas or recommendations? 1. Make the common model not Parcelable, create a new Parcelize-specific model class local to the app module, and convert back and forth between the two. 2. Persist the data a different way, or use a different serialization strategy, and remove the
@Parcelize
annotation on those common models.
e
3. Build an Android variant of the common model
Copy code
// commonMain
@OptionalExpectation
annotation class AndroidParcelable

@AndroidParcelable
data class Data(...)

// androidMain
actual typealias AndroidParcelable = Parcelable
I haven't tried this and it might not work, but it may be worth a shot
m
ah! I hadn't even considered kmp, but that actually seems like a great idea
e
if that doesn't work, I've also previously written an adapter from
@Serializable
to `@TypeParceler`/`@WriteWith`, but never ended up using it in the project it was built for, so it's also untested. https://gist.github.com/ephemient/65a3ee700f020661858c89b5648a2772
m
oh wow, yeah that's another great option - super super helpful, thank you!
k
the moko folks also came up w/ a KMP version of Parcelize
r
Can't help but feel that you're overengineering this; if your goal is simply to
modularize a monolithic Android app
, just making a separate Android module using the com.android.library plugin may be the best option
If you know you want to share code with other JVM/ other language projects then definitely KMP is the way to go
But an Android library will be way simpler if you only ever want to run it on Android
☝️ 2