JetBrains/kotlin
Parcelize
Active contributors: Dmitriy Novozhilov, Kirill Rakhman, Rafal Galczak
plugins/parcelize/ is a small but heavily used Android-specific plugin that synthesizes the Parcelable implementation for Kotlin classes annotated with @Parcelize. Without it, Android developers have to hand-write tedious writeToParcel/createFromParcel boilerplate; with it, a single annotation suffices.
Purpose
Generate, at compile time, the methods and fields that Android's Parcelable interface requires (writeToParcel, describeContents, the CREATOR static field) for any class annotated @Parcelize. Make this work over the full Kotlin type system: nullable types, data classes, sealed hierarchies, parametric types, collections, and custom Parceler<T> implementations.
Directory layout
plugins/parcelize/
├── parcelize-compiler/ The compiler plugin
├── parcelize-runtime/ Annotations and runtime helpers (@Parcelize, Parceler)
└── parcelize-tests/ TestsKey abstractions
| Concept | Role |
|---|---|
@Parcelize |
Marks a class for Parcelable code generation. |
Parceler<T> |
A user-supplied custom serializer for a type. |
@WriteWith<P> |
Annotation that instructs the plugin to use a specific Parceler<T> for a property. |
How it works
graph LR
Class["@Parcelize<br/>data class Foo(val x: Int, val y: String?)"] --> FirExt["Parcelize FIR ext"]
FirExt -->|declares synthetic Parcelable members| FirSyn["FIR with declarations"]
Fir2Ir --> IR["IR"]
IR --> IrExt["Parcelize IR generator"]
IrExt -->|generates writeToParcel,<br/>describeContents, CREATOR| IR2["IR"]
IR2 --> JVMBackend["JVM backend"]
JVMBackend --> Cls[".class files"]The plugin runs in both the FIR extension and IR generator phases:
- The FIR extension declares the synthetic
Parcelablemembers so the frontend treats the class as if it implementedParcelablealready. - The IR generator emits the bodies, walking each property and emitting the appropriate
Parcel.write*/Parcel.read*calls based on the property type.
JVM-only
Parcelable is an Android-specific (i.e., JVM-on-Android) interface. The plugin is configured to be a no-op on non-JVM targets, but in practice it's only used in projects that target Android.
KGP integration
libraries/tools/kotlin-gradle-plugin/.../parcelize/ (or id("kotlin-parcelize")) wires the plugin into Gradle:
plugins {
id("kotlin-parcelize")
}Tests
plugins/parcelize/parcelize-tests/ runs golden-file IR tests plus end-to-end tests that exercise the generated Parcelable implementation against the Android framework's expectations.
Integration points
- Consumed by: Android Kotlin projects.
- Co-traveler: the Android Gradle plugin (which detects
kotlin-parcelizeand includes the runtime jar appropriately).
Where to start
For new property-type support (e.g., a new platform type that should be parcelable out of the box): the IR generator in plugins/parcelize/parcelize-compiler/.
For new annotation features (e.g., extending @Parceler): both the FIR extension and the IR generator.
See plugins/index.md for the broader plugin landscape.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.