Modern Apps git

← Modern-Apps.git

build: deterministic R8 so release APKs reproduce on any machine (#perf)

Commit
29090c0b118dc26c32c1e4203412e0fc3980e572
Author
Vayun Mathur <[email protected]>
When
2026-11-11 06:09 UTC (25 days ago)
Parents
41ded03
F-Droid only accepts a release if its own rebuild matches the published
APK byte for byte (signature aside). Our R8 output was not stable: the
same commit built twice, or on boxes with different CPU counts, gave a
different classes.dex for several apps.

Root cause (R8 9.5.23, bundled with AGP 9.5.0-alpha09): R8 races its
worker threads while strengthening argument types, so one method proto
can come out as `Continuation` or `ContinuationImpl` (a one-byte type
index in classes.dex), and the mapping file differs the same way. The
mapping hash is embedded in the dex as the map id ("r8-map-id-..."
source file and the ~~R8 marker), so even a mapping-only difference
changes the APK. AGP runs every R8 task in-process on one shared
work-stealing pool of min(48, ~cores) threads, so the result depends on
thread timing, the box's CPU count and how many R8 tasks run at once.

Alternatives tried and rejected (9-app subset, 192 vs 8 CPUs + repeat):
- -Dcom.android.tools.r8.deterministicdebugging in the Gradle daemon:
  no effect, AGP hands R8 its own pool so R8's thread count is ignored.
- -renamesourcefileattribute SourceFile: removes one copy of the map
  id, but the ~~R8 marker keeps it and the proto race remains.
- R8 in its own process with a 1-thread pool only: 8/9 apps stable;
  a single-thread work-stealing pool does not fix the task order.

Change:
- settings.gradle.kts applies the com.android.settings plugin with a
  default execution profile that runs R8 in a separate worker process
  whose JVM sees one CPU (-XX:ActiveProcessorCount=1, common-pool
  parallelism 1, deterministicdebugging, -Xmx3g, ParallelGC).
- gradle.properties sets android.r8.threadPoolSize=1, the per-process
  R8 pool in isolation mode.
- The settings plugin version must equal `agp` in libs.versions.toml
  (settings plugins cannot read the version catalog).

Result: all 9 subset APKs (calendar, files, calculator, youpipe,
arrows, backup, camera, cast, setupwizard) are byte-identical, ignoring
signatures, between a 192-CPU build with 69 concurrent R8 tasks, an
8-CPU build with 4 Gradle workers, and a repeat of the first.

Cost: each R8 task is single-threaded, so release builds should raise
android.r8.maxWorkers to run many apps at once; the longest R8 task
(youpipe) goes from ~45 s to ~145 s on the Genoa box. Dev and debug
builds do not run R8 and are unaffected.

Diff

gradle.properties
diff --git a/gradle.properties b/gradle.propertiesindex 1743ff8589..4093a1b295 100644--- a/gradle.properties+++ b/gradle.properties@@ -27,6 +27,11 @@ kotlin.code.style=official # Performance & Optimization org.gradle.configuration-cache=true android.enableR8.fullMode=true+# One thread per R8 task (each runs in its own process, see settings.gradle.kts):+# multi-threaded R8 output depends on thread timing, which broke F-Droid's+# byte-for-byte reproducibility. Release builds parallelize across apps instead+# (android.r8.maxWorkers).+android.r8.threadPoolSize=1  # Preview Support (Remove once SDK 37 is stable) android.suppressUnsupportedCompileSdk=37.0
settings.gradle.kts
diff --git a/settings.gradle.kts b/settings.gradle.ktsindex 78a005f250..c9af58ef79 100644--- a/settings.gradle.kts+++ b/settings.gradle.kts@@ -13,6 +13,39 @@ pluginManagement {     } } +// Deterministic R8 (reproducible release APKs, see gradle.properties+// android.r8.threadPoolSize): R8 9.5 races its worker threads while recording+// argument-type-strengthened signatures, so its output (method protos in+// classes.dex and the mapping hash embedded as the map id) depends on thread+// timing, CPU count and how many R8 tasks share AGP's in-process thread pool.+// Running each R8 task in its own worker process gives it its own pool, which+// gradle.properties sizes to one thread: every APK is then byte-identical on+// any machine (F-Droid's builder included) while tasks still run in parallel+// across apps (android.r8.maxWorkers). The version must equal `agp` in+// gradle/libs.versions.toml (settings plugins cannot read the catalog).+plugins {+    id("com.android.settings") version "9.5.0-alpha09"+}++android {+    execution {+        profiles {+            create("deterministicR8") {+                r8 {+                    runInSeparateProcess = true+                    jvmOptions += listOf(+                        "-Xmx3g", "-XX:+UseParallelGC", "-Dfile.encoding=UTF-8",+                        "-XX:ActiveProcessorCount=1",+                        "-Djava.util.concurrent.ForkJoinPool.common.parallelism=1",+                        "-Dcom.android.tools.r8.deterministicdebugging=true",+                    )+                }+            }+        }+        defaultProfile = "deterministicR8"+    }+}+ @Suppress("UnstableApiUsage") dependencyResolutionManagement {     repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)