parcelable

Android Parceling support for the Kotlinx Serialization library.
GitHub tag (latest by date)

Badge

val parcelable = Parcelable.Default
parcelable.encodeToBundle(myModel)
val myModel = parcelable.decodeFromBundle(bundle)

Now all the serialization can be handled from a single library, no need to create both custom Parcelers and Serializers. Have a custom class that requires specific serialization? Use the Kotlinx Serialization library and it will automatically work with Android’s Parcel, Bundle, and Intent.

Building the library

The library is provided through Repsy.io. Checkout the releases page to get the latest version.
GitHub tag (latest by date)

Repository

repositories {
    maven { url = "https://repo.repsy.io/mvn/chrynan/public" }
}

Dependency

implementation "com.chrynan.parcelable:parcelable-android:$VERSION"

Using the library

  • Setup the Parcelable object:

val parcelable = Parcelable {
    serializersModule = mySerializersModule
}

@Serializable
data class MyModel(
    val intField: Int,
    val stringField: String,
    val nullableStringField: String? = null
)
  • Pass your model through Intents and Bundles:

// Put
intent.putExtra(key, myModel, parcelable)
bundle.putParcelable(key, myModel, parcelable)

// Get
val myModel = intent.getParcelableExtra(key, parcelable)
val myModel = bundle.getParcelable(key, parcelable)
  • Or serialize to and from Parcels and Bundles:

// To
parcelable.encodeToParcel(parcel, myModel)
val bundle = parcelable.encodeToBundle(myModel)

// From
val myModel = parcelable.decodeFromParcel(parcel)
val myModel = parcelable.decodeFromBundle(bundle)

Sample

The sample-android module contains an Android App showcasing the use of this library and providing a means to test parceling different models.

Simply update the ExpectedModel.model value to be any Serializable class and run the application to test if the parceling worked.

Sample Screenshots

Main Screen Results Screen

License

Copyright 2021 chRyNaN

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

GitHub

https://github.com/chRyNaN/serialization-parcelable