Skip to content

Add Kotlin Parcelize snippets - #1049

Open
erikrodriguez-se wants to merge 1 commit into
android:mainfrom
StellarElements:parcelize-snippets
Open

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

Conversation

@erikrodriguez-se

Copy link
Copy Markdown

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.

Thirteen 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

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, and _data_class opens with @file:OptIn(kotlinx.parcelize.Experimental::class). 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 currently 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.

Code not extracted

  • Gradle config — the Groovy plugins block (this repo is Kotlin DSL only), the Kotlin DSL plugins block, the additionalAnnotation 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.
  • Java — the UserCreator block under Create data from Parcel. Kotlin only.
  • Multiplatform — all four expect/actual blocks under Setup Parcelize for Kotlin multiplatform. Will not compile in a plain Android module.
  • 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.

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>
@snippet-bot

snippet-bot Bot commented Aug 25, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 13 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

getByName("release") {
isMinifyEnabled = true
isShrinkResources = true

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not in the original fork PR but this is a linter nit worth addressing since we're touching the file

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.

1 participant