Repository navigation
Migrate App resources snippets - #1157
djubinville wants to merge 8 commits into
Conversation
e32a62b to
7c0ac0b
Compare
7c0ac0b to
f0efb63
Compare
|
Addressed review feedback across commit
|
9a5bd1f to
e9c4e74
Compare
ede85ff to
6d1061a
Compare
|
Looks like there's a build error, can you resolve before we send for Compose review? |
|
Fixed in |
e12b7f6 to
8125b09
Compare
jakeroseman
left a comment
There was a problem hiding this comment.
Approving. I re-checked every earlier review comment on the PR and the companion DAC CL against the code at 8125b09 and built :compose:snippets (spotlessCheck and compileDebugKotlin pass, with no deprecation warnings in the files this PR touches). The only code change since my last review is the TypedArray .use { } change in MoreResources.kt that dturner asked for; it is in place for both arrays and is listed in the PR description together with the Drawable? change. All 14 macros on the five pages resolve to exactly one START/END pair at this head, and the branch merges cleanly with main. Nothing blocking is left; two optional notes are below.
Non-blocking: dturner's change request from 2026-10-30 is still the PR's review decision. Both of those comments are addressed in MoreResources.kt lines 55-61, so the PR needs a re-review from dturner before it can merge.
Non-blocking, for you and dturner to decide: in MoreResources.kt there is no import for androidx.core.content.res.use, so .use { } on lines 55 and 59 resolves to the Kotlin standard library AutoCloseable.use. TypedArray implements AutoCloseable only from API level 31 (close() is "Added in API level 31"). It compiles here because the module's minSdk is 36, but a reader with a lower minSdk who copies the snippet needs the core-ktx use extension, and imports are not shown on the page. One option is to add import androidx.core.content.res.use to the file so the snippet is built against the extension that works on all API levels; the rendered code would not change.
Optional description fix: the More-resources entry could also list the spacing change colors.getColor(0,0) -> colors.getColor(0, 0) in android_resources_more_typed_array, since it is a visible difference from the page.
Use the core-ktx TypedArray.use extension so the snippet compiles against recycle() across all API levels rather than relying on AutoCloseable.use (API 31+).
|
Thanks for the review and notes, @jakeroseman! Addressed both optional follow-ups in
Verified locally on top of
@dturner — Both |
Code snippets are for:
compose/snippets/src/main/res/values/string_aliases.xml(android_resources_providing_string_alias) is a migrated XML snippet.compose/snippets/src/main/res/drawable/my_icon.xml(copied from the Material vector drawablefavorite.xmlsopainterResource(id = R.drawable.my_icon)inProvidingResources.ktcompiles verbatim),compose/snippets/src/main/res/values/strings.xml(R.bool.screen_small,R.integer.max_speed,R.array.bits,R.array.icons, andR.array.colors),compose/snippets/src/main/res/values-es/strings.xml(helloandhiSpanish<string>translations required for Android LintMissingTranslationto pass because:compose:snippetsincludes avalues-es/locale directory),compose/snippets/src/main/res/values/colors.xml(R.color.opaque_red), andcompose/snippets/src/main/res/values/dimens.xml(R.dimen.font_size) are supporting resource declarations because the migrated snippets inProvidingResources.kt,MoreResources.kt, andstring_aliases.xmlreference those resource IDs and will not pass compilation and lint without them. Existing repository resourcesR.string.hello_world(inProvidingResources.kt) andR.drawable.cupcake(reused in place ofR.drawable.myimageinDrawableResources.ktand in<array name="icons">instrings.xmlin place ofhome/settings/logout) are reused to avoid adding duplicate assets or colliding string keys.List of modifications:
ProvidingResources.kt(stringResource(id = R.string.hello),painterResource(id = R.drawable.my_icon), andcontext.getString(R.string.hello_world)with a terminal period added to the inline comment), plus 1 XML string-alias snippet intores/values/string_aliases.xml(android_resources_providing_string_alias, omitting the top-level<?xml version="1.0" encoding="utf-8"?>processing instruction from inside the region tag soaapt2compiles the XML file, with companionhelloandhiSpanish entries inres/values-es/strings.xmlto satisfy Android LintMissingTranslation).DateText) intoResourcesSnippets.kt(android_resources_runtime_changes_date_text) so all snippets on the page reside inResourcesSnippets.kt.Internationalization.ktand formatted.format(Date())on a single line.ResourcesCompat.getDrawablesnippet intoDrawableResources.kt, reusing existing drawable assetR.drawable.cupcakein place ofR.drawable.myimage.MoreResources.kt, updated the deprecated one-argumentresources.getColor(R.color.opaque_red)overload toresources.getColor(R.color.opaque_red, null)(replacing the deprecated single-argument overload; the guide prose does not specify the overload), wrappedresources.obtainTypedArray(R.array.icons)andresources.obtainTypedArray(R.array.colors)in.use { ... }blocks (importingandroidx.core.content.res.usefor compatibility across all API levels) so eachTypedArrayis recycled after use, madeval drawable: Drawable? = ...nullable to match the SDK return type, and added spacing after the comma incolors.getColor(0,0)->colors.getColor(0, 0)inandroid_resources_more_typed_array.Snippets not migrated:
<bitmap>and<color>XML alias example blocks left hardcoded on the guide page.AndroidManifest.xml<activity>configuration blocks.findViewByIdView-based snippets (ImageButton,ImageView,TextView), and XML drawable syntax/example blocks on a reference-doc-like page left hardcoded on the guide page.Activity.showDialog/onCreateDialogsnippets, and XML resource syntax/example blocks on a reference-doc-like page left hardcoded on the guide page.