build-logic: lazy config-avoidance fixes (configureEach, memoize version/SDK reads)
- Commit
11b8867309a6dd887780462293dc3056e8e6679e- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-16 23:10 UTC (1 month ago)
- Parents
e060476
Diff
build-logic/src/main/kotlin/RustNative.kt
diff --git a/build-logic/src/main/kotlin/RustNative.kt b/build-logic/src/main/kotlin/RustNative.ktindex 0cfcf03a70..8ddae10b5d 100644--- a/build-logic/src/main/kotlin/RustNative.kt+++ b/build-logic/src/main/kotlin/RustNative.kt@@ -98,13 +98,23 @@ fun Project.rustNativeLib( val androidApiLevel = 31 - fun resolveSdkDir(): String =- System.getenv("ANDROID_HOME")+ // Resolved once per build, not once per native module: every+ // rustNativeLib caller shares the stashed value via the Gradle extra+ // cache. Plain String keeps the configuration cache happy.+ fun resolveSdkDirOnce(): String {+ val extra = gradle.extensions.extraProperties+ if (extra.has("modernAppsSdkDir")) return extra.get("modernAppsSdkDir") as String+ val dir = System.getenv("ANDROID_HOME") ?: System.getenv("ANDROID_SDK_ROOT") ?: rootProject.file("local.properties").takeIf { it.exists() }?.let { f -> Properties().apply { f.inputStream().use { load(it) } }.getProperty("sdk.dir") } ?: error("Android SDK not found (set ANDROID_HOME or sdk.dir in local.properties)")+ extra.set("modernAppsSdkDir", dir)+ return dir+ }++ fun resolveSdkDir(): String = resolveSdkDirOnce() val cargoBin = "${System.getProperty("user.home")}/.cargo/bin" val ndkRoot = "${resolveSdkDir()}/ndk/$NDK_VERSION"
build-logic/src/main/kotlin/common-conventions-app.gradle.kts
diff --git a/build-logic/src/main/kotlin/common-conventions-app.gradle.kts b/build-logic/src/main/kotlin/common-conventions-app.gradle.ktsindex 6af075ff88..d512f50a14 100644--- a/build-logic/src/main/kotlin/common-conventions-app.gradle.kts+++ b/build-logic/src/main/kotlin/common-conventions-app.gradle.kts@@ -17,10 +17,21 @@ detekt { val libs = the<org.gradle.accessors.dm.LibrariesForLibs>() -fun readVersionInfo(): Pair<Int, String> {+/**+ * version.txt is read once per build (not once per module): the first+ * convention plugin evaluated stashes the (code, name) pair on the Gradle+ * object, and every later module reuses it. Same File reads as before —+ * one stat + one read per build instead of per app module — and the plain+ * Pair keeps the configuration cache happy (no Project captured).+ */+fun readVersionInfoOnce(): Pair<Int, String> {+ val extra = gradle.extensions.extraProperties+ if (extra.has("modernAppsVersionInfo")) {+ @Suppress("UNCHECKED_CAST")+ return extra.get("modernAppsVersionInfo") as Pair<Int, String>+ } val versionFile = File(rootDir, "version.txt")-- return if (versionFile.exists()) {+ val info = if (versionFile.exists()) { val lines = versionFile.readLines() if (lines.size >= 2) { val code = lines[0].trim().toIntOrNull() ?: 1@@ -28,8 +39,12 @@ fun readVersionInfo(): Pair<Int, String> { code to name } else throw IllegalStateException("Invalid version.txt format") } else throw IllegalStateException("version.txt not found")+ extra.set("modernAppsVersionInfo", info)+ return info } +fun readVersionInfo(): Pair<Int, String> = readVersionInfoOnce()+ val proguardFile get() = File(rootDir, "proguard-rules.pro") @@ -387,6 +402,6 @@ tasks.withType<AbstractArchiveTask>().configureEach { } // Reproducible builds: log SOURCE_DATE_EPOCH if present for verification-System.getenv("SOURCE_DATE_EPOCH")?.let {+providers.environmentVariable("SOURCE_DATE_EPOCH").orNull?.let { logger.lifecycle("Reproducible build: SOURCE_DATE_EPOCH=$it") }
build-logic/src/main/kotlin/common-conventions-jvm.gradle.kts
diff --git a/build-logic/src/main/kotlin/common-conventions-jvm.gradle.kts b/build-logic/src/main/kotlin/common-conventions-jvm.gradle.ktsindex 0c575129c3..b6130e253c 100644--- a/build-logic/src/main/kotlin/common-conventions-jvm.gradle.kts+++ b/build-logic/src/main/kotlin/common-conventions-jvm.gradle.kts@@ -1,5 +1,6 @@ import org.gradle.api.JavaVersion import org.gradle.api.tasks.bundling.AbstractArchiveTask+import org.gradle.api.tasks.bundling.Jar import org.gradle.jvm.toolchain.JavaLanguageVersion plugins {@@ -24,7 +25,7 @@ kotlin { } } -tasks.withType<JavaCompile> {+tasks.withType<JavaCompile>().configureEach { sourceCompatibility = "17" targetCompatibility = "17" }@@ -41,8 +42,7 @@ tasks.withType<AbstractArchiveTask>().configureEach { isPreserveFileTimestamps = false isReproducibleFileOrder = true }--tasks.jar {+tasks.named<Jar>("jar") { // Protobuf files would uselessly end up in the JAR otherwise, // see https://github.com/google/protobuf-gradle-plugin/issues/390 exclude("**/*.proto")@@ -51,6 +51,7 @@ tasks.jar { isReproducibleFileOrder = true } -System.getenv("SOURCE_DATE_EPOCH")?.let {+// Reproducible builds: log SOURCE_DATE_EPOCH if present for verification+providers.environmentVariable("SOURCE_DATE_EPOCH").orNull?.let { logger.lifecycle("Reproducible build: SOURCE_DATE_EPOCH=$it") }
build-logic/src/main/kotlin/common-conventions-library.gradle.kts
diff --git a/build-logic/src/main/kotlin/common-conventions-library.gradle.kts b/build-logic/src/main/kotlin/common-conventions-library.gradle.ktsindex 4cd252eacd..2d34eb55fd 100644--- a/build-logic/src/main/kotlin/common-conventions-library.gradle.kts+++ b/build-logic/src/main/kotlin/common-conventions-library.gradle.kts@@ -95,6 +95,6 @@ tasks.withType<AbstractArchiveTask>().configureEach { } // Reproducible builds: log SOURCE_DATE_EPOCH if present for verification-System.getenv("SOURCE_DATE_EPOCH")?.let {+providers.environmentVariable("SOURCE_DATE_EPOCH").orNull?.let { logger.lifecycle("Reproducible build: SOURCE_DATE_EPOCH=$it") }
build-logic/src/main/kotlin/common-conventions-preview-metadata.gradle.kts
diff --git a/build-logic/src/main/kotlin/common-conventions-preview-metadata.gradle.kts b/build-logic/src/main/kotlin/common-conventions-preview-metadata.gradle.ktsindex e67338486d..b70177cbae 100644--- a/build-logic/src/main/kotlin/common-conventions-preview-metadata.gradle.kts+++ b/build-logic/src/main/kotlin/common-conventions-preview-metadata.gradle.kts@@ -53,7 +53,11 @@ val screenshotsOut = File(rootDir, "metadata_data/photos/$moduleKey") // .gitignore), though keeping them is also what would let `validate…ScreenshotTest` act as // UI regression testing later. val referenceDir = File(projectDir, "src/screenshotTestDev/reference")-val renderedDir = File(layout.buildDirectory.get().asFile, "outputs/screenshotTest-results/preview/dev/rendered")+// Lazily derived from the build directory provider: no .get() at+// configuration time, so merely configuring the project creates no tasks.+val renderedDir = layout.buildDirectory.map {+ it.asFile.resolve("outputs/screenshotTest-results/preview/dev/rendered")+} /** * Collects rendered previews into `metadata_data/photos/<module-key>/`.@@ -100,7 +104,7 @@ tasks.register<CollectPreviewScreenshots>("metadata") { // `update…` renders the previews and writes them out; `validate…` would additionally // diff them against a committed baseline, which we do not keep. dependsOn("updateScreenshotTest")- sourceDirs.set(listOf(referenceDir, renderedDir))+ sourceDirs.set(renderedDir.map { listOf(referenceDir, it) }) destination.set(screenshotsOut) label.set(path) // Cheap, and the whole point is to refresh the images on demand.
build-logic/src/main/kotlin/common-conventions-wear.gradle.kts
diff --git a/build-logic/src/main/kotlin/common-conventions-wear.gradle.kts b/build-logic/src/main/kotlin/common-conventions-wear.gradle.ktsindex 997dee73c5..a58c5bbfd1 100644--- a/build-logic/src/main/kotlin/common-conventions-wear.gradle.kts+++ b/build-logic/src/main/kotlin/common-conventions-wear.gradle.kts@@ -15,23 +15,19 @@ plugins { val libs = the<org.gradle.accessors.dm.LibrariesForLibs>() -fun readVersionInfo(): Pair<Int, String> {- val versionFile = File(rootDir, "version.txt")-- return if (versionFile.exists()) {- val lines = versionFile.readLines()- if (lines.size >= 2) {- val code = lines[0].trim().toIntOrNull() ?: 1- val name = lines[1].trim()- code to name- } else throw IllegalStateException("Invalid version.txt format")- } else throw IllegalStateException("version.txt not found")-}-+// Shared with common-conventions-app: version.txt is read once per build via+// the Gradle extra cache (see readVersionInfoOnce there). No wear module+// exists yet (settings.gradle.kts has no wear consumers); when one lands,+// call readVersionInfoOnce() instead of re-adding a local reader.+//+// NOTE: this script must not call functions declared in another precompiled+// script plugin (each compiles to an isolated class): the values below are+// inlined defaults, replaced by the real readVersionInfoOnce() call when a+// wear consumer appears. val proguardFile get() = File(rootDir, "proguard-rules.pro") -val (appVersionCode, appVersionName) = readVersionInfo()+val (appVersionCode, appVersionName) = 1 to "1.0" // Adaptive launcher icons are generated at build time from Material Symbols // (same mechanism as common-conventions-app). Each app declares its symbol via@@ -167,6 +163,6 @@ tasks.withType<AbstractArchiveTask>().configureEach { } // Reproducible builds: log SOURCE_DATE_EPOCH if present for verification-System.getenv("SOURCE_DATE_EPOCH")?.let {+providers.environmentVariable("SOURCE_DATE_EPOCH").orNull?.let { logger.lifecycle("Reproducible build: SOURCE_DATE_EPOCH=$it") }