kmp-parcelize
1.0.4indexedEnable @Parcelize and Parcelable usage in shared common code; build plugin auto-applies parcelize compiler, adds runtime, and generates real Parcelable implementations where supported.
Enable @Parcelize and Parcelable usage in shared common code; build plugin auto-applies parcelize compiler, adds runtime, and generates real Parcelable implementations where supported.
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.
@Parcelize and Parcelable from commonMain without any platform-specific wiring.kotlin-parcelize, adds the runtime dependency, and configures the compiler.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
pluginManagementrepositories in yoursettings.gradle.kts, make sure eithergradlePluginPortal()ormavenCentral()is included.
That's it — the plugin automatically:
org.jetbrains.kotlin.plugin.parcelizekmp-parcelize-runtime as an api dependency to commonMain@Parcelize annotation on Android targetsimport io.github.solcott.kmp.parcelize.Parcelable
import io.github.solcott.kmp.parcelize.Parcelize
@Parcelize
data class UserData(
val name: String,
val age: Int,
) : Parcelable
On 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.
@IgnoreOnParcelExclude 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 = "",
) : Parcelable
On Android this maps to kotlinx.parcelize.IgnoredOnParcel. On other platforms it is a no-op.
The project has two artifacts:
| Artifact |
|---|
The runtime uses Kotlin's expect/actual mechanism:
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.
A complete working example lives in the sample/ directory, showing a shared KMP module consumed by an Android app.
Copyright 2024 Scott Olcott
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance the License.
You may obtain a copy of the License at
http:
Unless applicable law agreed to writing, software
distributed under the License distributed an BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
See the License the specific language governing permissions
limitations under the License.
| 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 |
commonMain declares expect interface Parcelable, @Parcelize, and @IgnoreOnParcel.androidMain maps them to the real Android types via actual typealias:
Parcelable → android.os.ParcelableIgnoreOnParcel → kotlinx.parcelize.IgnoredOnParcelactual implementations (no-ops).| 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 |
Surfaced from shared tags and platforms — no rankings paid for.