metadata: render 512x512 Play Store launcher icon during metadata run
- Commit
777e924687263e651350d950e0ebe50568ce0305- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-07 04:12 UTC (5 months ago)
- Parents
adbbb2e
Diff
build-logic/metadataIconTest/com/vayunmathur/metadata/PlaystoreIconRenderer.kt
diff --git a/build-logic/metadataIconTest/com/vayunmathur/metadata/PlaystoreIconRenderer.kt b/build-logic/metadataIconTest/com/vayunmathur/metadata/PlaystoreIconRenderer.ktnew file mode 100644index 0000000000..29602ea64f--- /dev/null+++ b/build-logic/metadataIconTest/com/vayunmathur/metadata/PlaystoreIconRenderer.kt@@ -0,0 +1,58 @@+package com.vayunmathur.metadata++import android.graphics.Bitmap+import android.graphics.Canvas+import android.graphics.drawable.AdaptiveIconDrawable+import androidx.test.ext.junit.runners.AndroidJUnit4+import androidx.test.platform.app.InstrumentationRegistry+import org.junit.Test+import org.junit.runner.RunWith+import java.io.File++/**+ * Renders the app's own launcher icon (the adaptive `ic_launcher.xml`) to a 512x512+ * Play Store PNG on-device. Adaptive icons reference vectors/colors that can't easily be+ * rasterized on the host, so we composite the background + foreground layers here where+ * the platform can inflate them.+ *+ * This source set is shared into every module's androidTest by the+ * `common-conventions-metadata` plugin, so it runs as part of every `:module:metadata`.+ * The task then pulls the PNG into `<module>/src/main/ic_launcher-playstore.png`.+ */+@RunWith(AndroidJUnit4::class)+class PlaystoreIconRenderer {++ @Test+ fun renderPlaystoreIcon() {+ val ctx = InstrumentationRegistry.getInstrumentation().targetContext+ val size = 512+ val icon = ctx.packageManager.getApplicationIcon(ctx.packageName)++ val bmp = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)+ val canvas = Canvas(bmp)+ if (icon is AdaptiveIconDrawable) {+ // Background fills the whole square.+ icon.background?.apply { setBounds(0, 0, size, size); draw(canvas) }+ // Adaptive-icon foregrounds reserve an ~18dp safe-zone margin on each side+ // of the 108dp layer (real content lives in the center 72dp). Scale the+ // foreground up so that safe zone fills the frame — otherwise the glyph+ // renders small with lots of padding. 108/72 = 1.5x, centered.+ val inset = (size * 0.25f).toInt()+ icon.foreground?.apply {+ setBounds(-inset, -inset, size + inset, size + inset)+ draw(canvas)+ }+ } else {+ icon.setBounds(0, 0, size, size)+ icon.draw(canvas)+ }++ val dir = File(ctx.getExternalFilesDir(null), "metadata_icon").apply {+ deleteRecursively()+ mkdirs()+ }+ File(dir, "ic_launcher-playstore.png").outputStream().use {+ bmp.compress(Bitmap.CompressFormat.PNG, 100, it)+ }+ }+}
build-logic/src/main/kotlin/common-conventions-metadata.gradle.kts
diff --git a/build-logic/src/main/kotlin/common-conventions-metadata.gradle.kts b/build-logic/src/main/kotlin/common-conventions-metadata.gradle.ktsindex dd5001d7fd..375cf3aba0 100644--- a/build-logic/src/main/kotlin/common-conventions-metadata.gradle.kts+++ b/build-logic/src/main/kotlin/common-conventions-metadata.gradle.kts@@ -56,6 +56,9 @@ configure<ApplicationExtension> { defaultConfig { testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" }+ // Shared androidTest source: PlaystoreIconRenderer, which renders each app's+ // adaptive launcher icon to a 512x512 Play Store PNG during the metadata run.+ sourceSets.getByName("androidTest").kotlin.srcDir(File(rootDir, "build-logic/metadataIconTest")) } dependencies {@@ -105,7 +108,9 @@ afterEvaluate { val adb = resolveAdb() val runner = "$appId.test/androidx.test.runner.AndroidJUnitRunner" val deviceDir = "/sdcard/Android/data/$appId/files/metadata_screenshots"+ val iconDeviceFile = "/sdcard/Android/data/$appId/files/metadata_icon/ic_launcher-playstore.png" val out = screenshotsOut.absolutePath+ val srcMainIcon = File(projectDir, "src/main/ic_launcher-playstore.png").absolutePath // Grant the declared permissions/appops before launch (so first-run system // prompts don't hijack the screenshots), switch the device to night mode so@@ -125,6 +130,8 @@ afterEvaluate { lines += """rm -rf "$out"""" lines += """mkdir -p "$out"""" lines += """"$adb" pull "$deviceDir/." "$out""""+ // Also pull the rendered 512x512 Play Store icon into the module's src/main.+ lines += """"$adb" pull "$iconDeviceFile" "$srcMainIcon" || true""" lines += """"$adb" shell cmd uimode night no || true""" lines += """echo "Metadata screenshots written to $out"""" val script = lines.joinToString("\n")
clock/src/main/ic_launcher-playstore.png
diff --git a/clock/src/main/ic_launcher-playstore.png b/clock/src/main/ic_launcher-playstore.pngindex d5b4f337e5..f6f3641d71 100644Binary files a/clock/src/main/ic_launcher-playstore.png and b/clock/src/main/ic_launcher-playstore.png differ