Skip to content

Add Kotlin Parcelize snippets - #1049

Open
erikrodriguez-se wants to merge 2 commits into
android:mainfrom
StellarElements:parcelize-snippets
Open

Add Kotlin Parcelize snippets#1049
erikrodriguez-se wants to merge 2 commits into
android:mainfrom
StellarElements:parcelize-snippets

Conversation

@erikrodriguez-se

@erikrodriguez-se erikrodriguez-se commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Snippets migration - Kotlin parcelize

Snippets for the Parcelize guide at developer.android.com/kotlin/parcelize, extracted into kotlin/src/main/kotlin/com/example/android/basics/ (Parcelize.kt and ParcelizeDataClass.kt) with region tags, plus the parcelize plugin in kotlin/build.gradle.kts. The multiplatform snippets are added to the kmp/shared module (package com.example.kmp.snippets).

Fifteen of the page's 24 code blocks are extracted. Most are verbatim; the differences that exist fall into three kinds, described below.

Region tag (android_kotlin_parcelize_*) Page section Verbatim Deviation Rendered / page
_basic intro no import shown as a comment; colon spacing 4 / 4
_companion_parceler intro yes 12 / 12
_external_class_parceler Custom Parcelers yes 9 / 9
_type_parceler_class Custom Parcelers yes 4 / 4
_type_parceler_property Custom Parcelers yes 3 / 3
_type_parceler_type Custom Parcelers yes 3 / 3
_parcelable_creator Create data from Parcel no import shown as a comment 5 / 5
_ignored_on_parcel Skip properties from serialization no colon spacing 10 / 10
_raw_value Parcel.writeValue no colon spacing 2 / 2
_sealed_class Sealed classes and interfaces no colon spacing ×5 8 / 8
_data_class Experimental / Data class serializer no @file:OptIn shown as a comment 6 / 6
_data_class_wrapper Experimental / Data class serializer no colon spacing 8 / 8
_inheritance_base Experimental / Non val or var params no colon spacing ×2 11 / 11
_multiplatform_common Setup Parcelize for Kotlin multiplatform no package shown as a comment; colon spacing 19 / 19
_multiplatform_platform Setup Parcelize for Kotlin multiplatform no package shown as a comment 6 / 6

The three kinds of deviation

Colon spacing. The page writes ): Parcelable; this repo's ktlint config rewrites it to ) : Parcelable. This is unavoidable — spotlessApply makes the change when run — and it accounts for most of the non-verbatim rows above.

File-top declarations shown as a comment. The _basic and _parcelable_creator blocks open with an import, _data_class opens with @file:OptIn(kotlinx.parcelize.Experimental::class), and the two _multiplatform_ blocks open with a package declaration. Kotlin requires those at the top of the file, above the package declaration and outside any snippet region. To keep them visible in the published snippet, the real line lives at the top of the file (uncommented, so it compiles) and a commented copy is placed inside the region. The published snippet therefore shows, for example, // import kotlinx.parcelize.Parcelize where the page shows a live import statement. The page's blocks would be updated to match.

One experimental block omitted. The page's "Non val or var parameters" section has two code blocks: _inheritance_base (the non-experimental approach, extracted) and an experimental variant that requires experimentalCodeGeneration=true. That flag is a parcelize compiler-plugin argument with module-level granularity — it cannot be scoped to a single snippet, and enabling it module-wide would compile the kotlin module's unrelated snippets (coroutines, flow testing) under experimental codegen. The experimental block is therefore not extracted, and the flag is not set. _inheritance_base alone still demonstrates the section's point.

Multiplatform snippets

The Kotlin 2.0+ multiplatform snippets are added to kmp/shared: _multiplatform_common in commonMain and _multiplatform_platform in androidMain. Two notes:

  • iOS actuals. The common code declares expect interface MyParcelable and expect annotation class MyIgnoredOnParcel, so Kotlin requires an actual in every target — including iOS. The page says these "can be empty" but does not show them, so iosMain gets a small file with empty actuals. It is not a page snippet, so it carries no region tags.
  • Gradle config. The additionalAnnotation compiler argument is applied in kmp/shared/build.gradle.kts but left untagged, matching how the page publishes build config (hand-maintained). The page's example package becomes com.example.kmp.snippets, so the additionalAnnotation value points at com.example.kmp.snippets.MyParcelize.

Code not extracted

  • Gradle config — the Groovy plugins block (this repo is Kotlin DSL only), the Kotlin DSL plugins block, and the experimentalCodeGeneration block. Build-config blocks are published in their portable id("…") form on the page rather than this repo's version-catalog form, so they are left hand-maintained. (The additionalAnnotation block is applied but untagged — see "Multiplatform snippets" above.)
  • Java — the UserCreator block under Create data from Parcel. Kotlin only.
  • Multiplatform, pre-Kotlin 2.0 — the two expect/actual aliasing blocks under Setup Parcelize for Kotlin multiplatform. That approach is unsupported in Kotlin 2.0 and higher, which this project uses, so it will not compile. The Kotlin 2.0+ blocks from the same section are extracted — see "Multiplatform snippets" above.
  • The experimental inheritance block and the // ERROR: not allowed example, both under Non val or var parameters in primary constructor. See "One experimental block omitted" above; the ERROR example cannot compile by design.

Deprecations

None. Ten imports, all current — android.os.Parcel, android.os.Parcelable and eight kotlinx.parcelize symbols. A forced full recompile produced zero warnings.

@snippet-bot

snippet-bot Bot commented Aug 25, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 15 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

Comment thread kotlin/build.gradle.kts
@francescoo22

Copy link
Copy Markdown

Multiplatform — all four expect/actual blocks under Setup Parcelize for Kotlin multiplatform. Will not compile in a plain Android module.

Wouldn't it be possible to put Parcelize snippets in a multiplatform module so that we can add them as well? Since we are currently working on improving support for KMP projects using Parcelize, it would be nice to include snippets showing multiplatform usage if that is not too complicated. (I saw that there's already a multiplatform module in the project)

erikrodriguez-se and others added 2 commits September 7, 2026 22:34
Snippets for the Parcelize guide (developer.android.com/kotlin/parcelize),
extracted into kotlin/src/main/kotlin/com/example/android/basics with region
tags, plus the parcelize plugin in kotlin/build.gradle.kts.

One deviation from the page as published: the data class serializer example
uses a declaration-level @OptIn rather than the page's file-level @file:OptIn.
A file annotation must precede the package declaration, so it cannot sit inside
a region tag, and the published snippet would otherwise lose the opt-in
entirely. The page's block needs the matching change.

Kotlin only.

Co-authored-by: Katherine Kuan <843428+kkuan2011@users.noreply.github.com>
Adds the Kotlin 2.0+ multiplatform Parcelize snippets to kmp/shared, per
review feedback on the PR: the common code, the Android platform actuals,
and the additionalAnnotation Gradle config.

iOS actuals are added (empty) because the common expect declarations
require an actual in every target. The Gradle config is left untagged and
hardcoded on the page.
@erikrodriguez-se

Copy link
Copy Markdown
Contributor Author

Multiplatform — all four expect/actual blocks under Setup Parcelize for Kotlin multiplatform. Will not compile in a plain Android module.

Wouldn't it be possible to put Parcelize snippets in a multiplatform module so that we can add them as well? Since we are currently working on improving support for KMP projects using Parcelize, it would be nice to include snippets showing multiplatform usage if that is not too complicated. (I saw that there's already a multiplatform module in the project)

@francescoo22 I added the two migrate-able snippets into the KMP module as requested and updated the PR description. Thanks!

@francescoo22

Copy link
Copy Markdown

Thank you, looks good to me!

@kkuan2011 kkuan2011 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but it looks like the iOS build is failing? Would you know why?

@erikrodriguez-se

Copy link
Copy Markdown
Contributor Author

Looks good, but it looks like the iOS build is failing? Would you know why?

Sorry, this built cleanly locally for me and I didn't see the CI fail. It looks like the build script is asking for an older version of Xcode that the runner doesn't have. I will see if I can update it.

@erikrodriguez-se
erikrodriguez-se force-pushed the parcelize-snippets branch 2 times, most recently from 0d99944 to 3f1f66c Compare September 13, 2026 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants