/smuggler

[DEPRECATED] The easiest way to make your data classes Parcelable.

Primary LanguageKotlinMIT LicenseMIT

[DEPRECATED]

Please note the library is deprecated now. Consider using @Parcelize instead which is now an official to generate Parcelable implementation.

Smuggler

Makes your data classes Parcelable. Just add AutoParcelable to the class declaration. That's all. No methods need to be implemented.

data class User(
  val uuid: String,
  val firstName: String,
  val lastName: String
) : AutoParcelable

data class Message(
  val uuid: String,
  val text: String,
  val sender: User,
  val timestamp: Long,
  val seen: Boolean
) : AutoParcelable

data class Chat(
  val title: String,
  val messages: List<Message>,
  val participants: List<User>
) : AutoParcelable

Kotlin objects are supported as well:

interface Command : AutoParcelable

object LoginCommand : Command
object LogoutCommand : Command
object MenuCommand : Command

data class ProductCommand(val id: String) : Command
data class CategoryCommand(val id: String) : Command

Project Setup

buildscript {
  repositories {
    mavenCentral()
  }

  dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
    classpath "com.android.tools.build:gradle:4.2.1"
    classpath "com.joom.smuggler:smuggler-plugin:1.1.8"
  }
}

repositories {
  mavenCentral()
}

apply plugin: "com.android.application"
apply plugin: "com.joom.smuggler"

Some important notes:

  • Smuggler plugin must be applied after android plugin.
  • In case you have a mutli-module project and want to use AutoParceable, you have to add smuggler-plugin to each module.

Supported types

  • Primitive types: boolean, byte, char, double, float, int, long, short
  • Boxed types: Boolean, Byte, Char, Double, Float, Int, Long, Short
  • Other types: String, Bundle, SparseBooleanArray, Date, SparseArray
  • All Parcelable subclasses
  • All Enum subclasses
  • All Serializable subclasses
  • Arrays of any supported type, including primitive arrays and multidimensional arrays like: Array<Array<Array<User>>
  • Sets, Lists and Maps of any supported type, including primitive types and complex types like: Map<List<Message>, List<Array<Set<User>>>

Custom types

  • Serialization and deserialization of custom types are supported via TypeAdapter's:

    object BigIntegerTypeAdapter : TypeAdapter<BigInteger> {
      override fun fromParcel(parcel: Parcel): BigInteger {
        return BigInteger(parcel.createByteArray())
      }
    
      override fun toParcel(value: BigInteger, parcel: Parcel, flags: Int) {
        parcel.writeByteArray(value.toByteArray())
      }
    }
  • Project-level TypeAdapter can be registered using @GlobalAdapter annotation:

    @GlobalAdapter
    object BigIntegerTypeAdapter : TypeAdapter<BigInteger> {
      ...
    }
  • Class-level TypeAdapter can be registered using @LocalAdapter annotation:

    @LocalAdapter(BigIntegerTypeAdapter::class, CalendarTypeAdapter::class)
    data class Local(
      val integer: BigInteger,
      val calendar: Calendar
    ) : AutoParcelable
  • Defining TypeAdapter for a particular type automatically allows to use this type with Lists, Maps, Sets and Arrays:

    data class BigIntegerExample(
      val single: BigInteger,
      val array: Array<BigInteger>,
      val multidimensional: Array<Array<BigInteger>>,
      val list: List<BigInteger>,
      val set: Set<BigInteger>,
      val map: Map<String, BigInteger>,
      val complex: Map<Set<BigInteger>, Array<List<BigInteger>>>
    ) : AutoParcelable
  • Custom TypeAdapter can be defined both as class or object

  • TypeAdapter defined as class must have a public no-args constructor

How does it work?

Smuggler doesn't use reflection so you don't have to worry about its performance. It isn't an annotation proccessor so you don't have to deal with kapt bugs. Instead, Smuggler built on top of transform api and works by proccessing your compiled bytecode and patching classes that implement AutoParcelable interface.

Known limitations

  • Only data classes and kotlin objects are supported
  • Data classes with type parameters aren't supported at the moment
  • Lists, Maps and Arrays with bounded type parameters aren't supported at the moment
  • The library doesn't work nicely with JVM tests at the moment #12

What does "Smuggler" mean?

A smuggler was an individual who dealt with the secret exchanged shipment of goods to block restrictions or tax fees. The items shipped were often considered contraband, and highly illegal. Notable smugglers included Han Solo, Chewbacca, and Lando Calrissian. © http://starwars.wikia.com