Use @Parcelize and Parcelable in Kotlin Multiplatform common code. On Android they produce real android.os.Parcelable implementations via the official Kotlin Parcelize compiler plugin — on every other platform the annotations and interfaces are harmless no-ops.
- Single annotation in common code — use
@ParcelizeandParcelablefromcommonMainwithout any platform-specific wiring. - Zero boilerplate — the Gradle plugin automatically applies
kotlin-parcelize, adds the runtime dependency, and configures the compiler. - Full target support — Android, JVM, JS, Wasm, iOS, macOS, watchOS, tvOS, Linux, Windows, and Android Native.
In your KMP module's build.gradle.kts:
plugins {
id("org.jetbrains.kotlin.multiplatform") version "<kotlin-version>"
id("io.github.solcott.kmp.parcelize") version "<version>"
}Note
The plugin is published to both the Gradle Plugin Portal and Maven Central. If you use pluginManagement repositories in your settings.gradle.kts, make sure either gradlePluginPortal() or mavenCentral() is included.
That's it — the plugin automatically:
- Applies
org.jetbrains.kotlin.plugin.parcelize - Adds
kmp-parcelize-runtimeas anapidependency tocommonMain - Configures the Parcelize compiler plugin to recognize the common
@Parcelizeannotation on Android targets
import io.github.solcott.kmp.parcelize.Parcelable
import io.github.solcott.kmp.parcelize.Parcelize
@Parcelize
data class UserData(
val name: String,
val age: Int,
) : ParcelableOn Android this generates a full android.os.Parcelable implementation. On all other platforms Parcelable is an empty interface and @Parcelize is a no-op annotation.
Exclude a property from parceling with @IgnoreOnParcel:
import io.github.solcott.kmp.parcelize.IgnoreOnParcel
import io.github.solcott.kmp.parcelize.Parcelable
import io.github.solcott.kmp.parcelize.Parcelize
@Parcelize
data class UserData(
val name: String,
@IgnoreOnParcel val cached: String = "",
) : ParcelableOn Android this maps to kotlinx.parcelize.IgnoredOnParcel. On other platforms it is a no-op.
The project has two artifacts:
| Artifact | Coordinates | Purpose |
|---|---|---|
| Gradle plugin | io.github.solcott.kmp.parcelize |
Wires up the Parcelize compiler plugin and adds the runtime dependency |
| Runtime library | io.github.solcott:kmp-parcelize-runtime |
Provides the expect/actual declarations used in common code |
The runtime uses Kotlin's expect/actual mechanism:
commonMaindeclaresexpect interface Parcelable,@Parcelize, and@IgnoreOnParcel.androidMainmaps them to the real Android types viaactual typealias:Parcelable→android.os.ParcelableIgnoreOnParcel→kotlinx.parcelize.IgnoredOnParcel
- All other platforms provide empty
actualimplementations (no-ops).
The Gradle plugin also passes -P plugin:org.jetbrains.kotlin.parcelize:additionalAnnotation=io.github.solcott.kmp.parcelize.Parcelize to the Kotlin compiler on Android targets, so the Parcelize compiler plugin recognizes the common @Parcelize annotation.
Applying the Gradle plugin is the easiest way to use kmp-parcelize — it handles all configuration for you.
// build.gradle.kts
plugins {
id("io.github.solcott.kmp.parcelize") version "<version>"
}If you prefer to configure the Parcelize compiler plugin yourself, you can depend on the runtime library directly:
// build.gradle.kts
kotlin {
sourceSets {
commonMain.dependencies {
api("io.github.solcott:kmp-parcelize-runtime:<version>")
}
}
}You will also need to apply org.jetbrains.kotlin.plugin.parcelize and configure the additionalAnnotation compiler argument on your Android targets manually.
| Platform | Targets |
|---|---|
| Android | via com.android.kotlin.multiplatform.library or com.android.library |
| JVM | jvm |
| JS | js(IR) — browser, Node.js |
| Wasm | wasmJs — browser, Node.js |
| Apple | iosArm64, iosSimulatorArm64, macosArm64, watchosArm32, watchosArm64, watchosSimulatorArm64, watchosDeviceArm64, tvosArm64, tvosSimulatorArm64 |
| Linux | linuxX64, linuxArm64 |
| Windows | mingwX64 |
| Android Native | androidNativeArm32, androidNativeArm64, androidNativeX64, androidNativeX86 |
A complete working example lives in the sample/ directory, showing a shared KMP module consumed by an Android app.
- Kotlin 2.0+
- Gradle 8.0+
Copyright 2024 Scott Olcott
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.