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.
Parceler, @TypeParceler, @WriteWith)For types that aren't Parcelable, write a Parceler in common code and point Parcelize at it with @TypeParceler (for a whole class or one property) or @WriteWith (on a single type usage):
On Android these map to kotlinx.parcelize.Parceler, kotlinx.parcelize.TypeParceler, kotlinx.parcelize.WriteWith and android.os.Parcel. On other platforms the annotations are no-ops and Parcel can never be instantiated, so a Parceler is never called.
The common exposes a subset of : /, /, /, /, /, /, / and . If you need more of the API, declare the parceler in instead.
Snapshot builds of the next version are published from main to the Maven Central snapshots repository. The current snapshot version is 1.1.1-SNAPSHOT.
Add the snapshots repository in your settings.gradle.kts — under pluginManagement for the Gradle plugin, and under dependencyResolutionManagement for the runtime library:
// settings.gradle.kts
pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
maven("https://central.sonatype.com/repository/maven-snapshots/") {
mavenContent {
snapshotsOnly()
includeGroupByRegex("io\\.github\\.solcott.*")
}
}
}
}
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven("https://central.sonatype.com/repository/maven-snapshots/") {
mavenContent {
snapshotsOnly()
includeGroupByRegex("io\\.github\\.solcott.*")
}
}
}
}
Then use the snapshot version:
// build.gradle.kts
plugins {
id("io.github.solcott.kmp.parcelize") version "1.1.1-SNAPSHOT"
}
[!WARNING] Snapshots are unstable development builds and can change at any time. Gradle caches changing modules for 24 hours by default; run with
--refresh-dependenciesto pick up the latest snapshot immediately.
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.
import io.github.solcott.kmp.parcelize.Parcel
import io.github.solcott.kmp.parcelize.Parcelable
import io.github.solcott.kmp.parcelize.Parceler
import io.github.solcott.kmp.parcelize.Parcelize
import io.github.solcott.kmp.parcelize.TypeParceler
import io.github.solcott.kmp.parcelize.WriteWith
import io.ktor.http.Url
object UrlParceler : Parceler<Url> {
override fun create(parcel: Parcel): Url = Url(parcel.readString().orEmpty())
override fun Url.write(parcel: Parcel, flags: Int) = parcel.writeString(toString())
}
<Url, UrlParceler>
data class Example(
val pdf: Url,
val image: <UrlParceler> Url,
) : Parcelable
Parcelandroid.os.ParcelreadStringwriteStringreadIntwriteIntreadLongwriteLongreadFloatwriteFloatreadDoublewriteDoublereadBytewriteBytecreateByteArraywriteByteArraydataPositionandroidMain| 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, @IgnoreOnParcel, expect class Parcel, expect interface Parceler, @TypeParceler, and @WriteWith.androidMain maps them to the real Android types via actual typealias:
Parcelable → android.os.ParcelableIgnoreOnParcel → kotlinx.parcelize.IgnoredOnParcelParcel → android.os.ParcelParceler → kotlinx.parcelize.ParcelerTypeParceler → kotlinx.parcelize.TypeParcelerWriteWith → kotlinx.parcelize.WriteWithactual 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.