Modern Apps git

← Modern-Apps.git

metadata: setup automatic screenshot generation

Commit
c7fee9cbabe772a8d41d6f3722ad5e284734b9f7
Author
Vayun Mathur <[email protected]>
When
2026-07-06 23:46 UTC (5 months ago)
Parents
fcc05ea

Diff

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.ktsnew file mode 100644index 0000000000..e5f05833b3--- /dev/null+++ b/build-logic/src/main/kotlin/common-conventions-metadata.gradle.kts@@ -0,0 +1,96 @@+import com.android.build.api.dsl.ApplicationExtension+import org.gradle.accessors.dm.LibrariesForLibs++// Wires up on-device app-store screenshot generation.+//+// Apply this ALONGSIDE `common-conventions-app` on any app module that has a+// screenshot generator under `src/androidTest`. It registers a `metadata` task:+//+//     ./gradlew :clock:metadata+//+// which installs the app + its instrumented screenshot generator on a connected+// emulator/device, runs it, and pulls the resulting PNGs into+// `metadata_data/photos/<module-key>/` (where release.sh picks them up).+//+// Modules without a `src/androidTest` screenshot generator simply won't produce+// any images, so applying this plugin is harmless.++val libs = the<LibrariesForLibs>()++configure<ApplicationExtension> {+    defaultConfig {+        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"+    }+}++dependencies {+    add("androidTestImplementation", libs.junit)+    add("androidTestImplementation", libs.androidx.test.runner)+    add("androidTestImplementation", libs.androidx.test.ext.junit)+    add("androidTestImplementation", platform(libs.androidx.compose.bom))+    add("androidTestImplementation", libs.androidx.compose.ui.test.junit4)+    add("debugImplementation", libs.androidx.compose.ui.test.manifest)+}++val moduleKey = path.removePrefix(":").replace(":", "-")+val screenshotsOut = File(rootDir, "metadata_data/photos/$moduleKey")++fun resolveAdb(): String {+    val localProps = File(rootDir, "local.properties")+    val sdkDir = if (localProps.exists()) {+        java.util.Properties().apply { localProps.inputStream().use { load(it) } }.getProperty("sdk.dir")+    } else null+    val sdk = sdkDir+        ?: System.getenv("ANDROID_HOME")+        ?: System.getenv("ANDROID_SDK_ROOT")+        ?: error("Android SDK not found: set sdk.dir in local.properties or ANDROID_HOME")+    return File(sdk, "platform-tools/adb").absolutePath+}++val metadataTask = tasks.register<Exec>("metadata") {+    group = "metadata"+    description = "Generate Play/F-Droid screenshots on a connected emulator and copy them into metadata_data/photos/$moduleKey"+    dependsOn("installDebug", "installDebugAndroidTest")+    // Placeholder; the real command is assembled in afterEvaluate below, once the+    // applicationId is known, so the task only holds plain serializable strings+    // (required for the configuration cache).+    commandLine("true")+}++// Everything is resolved at configuration time into plain Strings so the Exec+// task stays configuration-cache compatible (no project/extension access at+// execution time). To target a specific emulator without touching other+// connected devices (e.g. a physical phone), export ANDROID_SERIAL before+// running -- both AGP's install tasks and adb honour it.+afterEvaluate {+    val appId = the<ApplicationExtension>().defaultConfig.applicationId+        ?: error("applicationId is not set for $path; the metadata task needs it to locate the app on device")+    val adb = resolveAdb()+    val runner = "$appId.test/androidx.test.runner.AndroidJUnitRunner"+    val deviceDir = "/sdcard/Android/data/$appId/files/metadata_screenshots"+    val out = screenshotsOut.absolutePath++    // Pre-grant the permissions/appops that MainActivity would otherwise bounce+    // the user to system settings for on first launch (so those settings screens+    // don't hijack the screenshots), and switch the device to night mode so the+    // app's DynamicTheme renders dark. The device is restored to light after.+    val script = """+        set -e+        "$adb" shell pm clear $appId || true+        "$adb" shell pm grant $appId android.permission.POST_NOTIFICATIONS || true+        "$adb" shell appops set $appId SCHEDULE_EXACT_ALARM allow || true+        "$adb" shell appops set $appId USE_FULL_SCREEN_INTENT allow || true+        "$adb" shell cmd uimode night yes || true+        "$adb" shell rm -rf "$deviceDir" || true+        "$adb" shell am instrument -w "$runner"+        rm -rf "$out"+        mkdir -p "$out"+        "$adb" pull "$deviceDir/." "$out"+        "$adb" shell cmd uimode night no || true+        echo "Metadata screenshots written to $out"+    """.trimIndent()++    metadataTask.configure {+        commandLine("bash", "-c", script)+    }+}
gradle/libs.versions.toml
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.tomlindex fef6233b6c..62f74bcaad 100644--- a/gradle/libs.versions.toml+++ b/gradle/libs.versions.toml@@ -7,6 +7,11 @@ kotlin = "2.4.0" kotlinGradlePlugin = "2.3.20" ksp = "2.3.6" +# Instrumented screenshot tests (metadata generation)+junit = "4.13.2"+androidxTestRunner = "1.7.0"+androidxTestExtJunit = "1.3.0"+ # AndroidX Core & Lifecycle coreKtx = "1.19.0" lifecycle = "2.10.0"@@ -91,6 +96,13 @@ androidx-webkit = { group = "androidx.webkit", name = "webkit", version.ref = "w geckoview = { module = "org.mozilla.geckoview:geckoview-arm64-v8a", version.ref = "geckoview" } jsoup = { module = "org.jsoup:jsoup", version.ref = "jsoup" } +# Instrumented screenshot tests (metadata generation)+junit = { module = "junit:junit", version.ref = "junit" }+androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidxTestRunner" }+androidx-test-ext-junit = { module = "androidx.test.ext:junit", version.ref = "androidxTestExtJunit" }+androidx-compose-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }+androidx-compose-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }+ # Jetpack Compose (BOM Managed) androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" } androidx-compose-ui = { group = "androidx.compose.ui", name = "ui" }
release.sh
diff --git a/release.sh b/release.shindex 5a45c3cf99..15d761fb19 100755--- a/release.sh+++ b/release.sh@@ -52,7 +52,15 @@ for dir in $MODULE_DIRS; do         mkdir -p "$target_path/graphics/icon"         mkdir -p "$target_path/graphics/phone-screenshots"         if [ -f "$dir/src/main/ic_launcher-playstore.png" ]; then cp "$dir/src/main/ic_launcher-playstore.png" "$target_path/graphics/icon/icon.png"; fi-        if [ -f "metadata_data/photos/${module_key}.png" ]; then cp "metadata_data/photos/${module_key}.png" "$target_path/graphics/phone-screenshots/1.png"; fi+        if [ -d "metadata_data/photos/${module_key}" ]; then+            i=1+            for shot in $(ls "metadata_data/photos/${module_key}"/*.png 2>/dev/null | sort -V); do+                cp "$shot" "$target_path/graphics/phone-screenshots/${i}.png"+                i=$((i + 1))+            done+        elif [ -f "metadata_data/photos/${module_key}.png" ]; then+            cp "metadata_data/photos/${module_key}.png" "$target_path/graphics/phone-screenshots/1.png"+        fi         if [ -f "metadata_data/${module_key}.md" ]; then             head -n 1 "metadata_data/${module_key}.md" > "$target_path/short-description.txt"             cp "metadata_data/${module_key}.md" "$target_path/full-description.txt"