Add Kotlin Parcelize snippets - #1049
Conversation
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) |
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.
75d49ee to
3f1f66c
Compare
@francescoo22 I added the two migrate-able snippets into the KMP module as requested and updated the PR description. Thanks! |
|
Thank you, looks good to me! |
kkuan2011
left a comment
There was a problem hiding this comment.
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. |
0d99944 to
3f1f66c
Compare
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.ktandParcelizeDataClass.kt) with region tags, plus the parcelize plugin inkotlin/build.gradle.kts. The multiplatform snippets are added to thekmp/sharedmodule (packagecom.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.
android_kotlin_parcelize_*)_basic_companion_parceler_external_class_parceler_type_parceler_class_type_parceler_property_type_parceler_type_parcelable_creator_ignored_on_parcel_raw_valueParcel.writeValue_sealed_class_data_class@file:OptInshown as a comment_data_class_wrapper_inheritance_basevalorvarparams_multiplatform_common_multiplatform_platformThe three kinds of deviation
Colon spacing. The page writes
): Parcelable; this repo's ktlint config rewrites it to) : Parcelable. This is unavoidable —spotlessApplymakes the change when run — and it accounts for most of the non-verbatim rows above.File-top declarations shown as a comment. The
_basicand_parcelable_creatorblocks open with animport,_data_classopens with@file:OptIn(kotlinx.parcelize.Experimental::class), and the two_multiplatform_blocks open with apackagedeclaration. Kotlin requires those at the top of the file, above thepackagedeclaration 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.Parcelizewhere the page shows a liveimportstatement. The page's blocks would be updated to match.One experimental block omitted. The page's "Non
valorvarparameters" section has two code blocks:_inheritance_base(the non-experimental approach, extracted) and an experimental variant that requiresexperimentalCodeGeneration=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 thekotlinmodule's unrelated snippets (coroutines, flow testing) under experimental codegen. The experimental block is therefore not extracted, and the flag is not set._inheritance_basealone still demonstrates the section's point.Multiplatform snippets
The Kotlin 2.0+ multiplatform snippets are added to
kmp/shared:_multiplatform_commonincommonMainand_multiplatform_platforminandroidMain. Two notes:expect interface MyParcelableandexpect annotation class MyIgnoredOnParcel, so Kotlin requires anactualin every target — including iOS. The page says these "can be empty" but does not show them, soiosMaingets a small file with empty actuals. It is not a page snippet, so it carries no region tags.additionalAnnotationcompiler argument is applied inkmp/shared/build.gradle.ktsbut left untagged, matching how the page publishes build config (hand-maintained). The page'sexamplepackage becomescom.example.kmp.snippets, so theadditionalAnnotationvalue points atcom.example.kmp.snippets.MyParcelize.Code not extracted
pluginsblock (this repo is Kotlin DSL only), the Kotlin DSLpluginsblock, and theexperimentalCodeGenerationblock. Build-config blocks are published in their portableid("…")form on the page rather than this repo's version-catalog form, so they are left hand-maintained. (TheadditionalAnnotationblock is applied but untagged — see "Multiplatform snippets" above.)UserCreatorblock under Create data from Parcel. Kotlin only.expect/actualaliasing 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.// ERROR: not allowedexample, 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.Parcelableand eightkotlinx.parcelizesymbols. A forced full recompile produced zero warnings.