Skip to content
TypeScript

perf(mobile): clean Android builds keep their Gradle and native caches (#20069)

Julius Marminge6a9f58830913
diff --git a/.gitignore b/.gitignore
index 38bac103f1f..26b8965ecab 100644
--- a/.gitignore
+++ b/.gitignore
@@ -33,6 +33,7 @@ dist-electron/
.showcase/
apps/mobile/.showcase/
apps/mobile/.generated/
+apps/mobile/.android-cache/
/.generated/
artifacts/app-store/screenshots/
.github/pr-assets/
diff --git a/scripts/mobile-native-client.test.ts b/scripts/mobile-native-client.test.ts
new file mode 100644
index 00000000000..0b9734a182f
--- /dev/null
+++ b/scripts/mobile-native-client.test.ts
@@ -0,0 +1,65 @@
+import * as NodeServices from "@effect/platform-node/NodeServices";
+import { assert, it } from "@effect/vitest";
+import * as Effect from "effect/Effect";
+import * as FileSystem from "effect/FileSystem";
+import * as Path from "effect/Path";
+
+import { NativeClientError, prebuildAndroid } from "./mobile-native-client.ts";
+
+const caches = [".gradle/history", "app/.cxx/object"];
+
+// A generated Android project with Gradle and native outputs, and a stand-in for
+// `expo prebuild --clean`, which deletes the directory and writes fresh sources.
+const fixture = Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const mobile = yield* fs.makeTempDirectoryScoped({ prefix: "mobile-native-client-" });
+ const android = path.join(mobile, "android");
+ for (const cache of caches) {
+ yield* fs.makeDirectory(path.dirname(path.join(android, cache)), { recursive: true });
+ yield* fs.writeFileString(path.join(android, cache), cache);
+ }
+ const cleanPrebuild = Effect.gen(function* () {
+ yield* fs.remove(android, { recursive: true });
+ yield* fs.makeDirectory(path.join(android, "app"), { recursive: true });
+ yield* fs.writeFileString(path.join(android, "app/build.gradle"), "regenerated");
+ });
+ const assertCachesRestored = Effect.gen(function* () {
+ for (const cache of caches) {
+ assert.strictEqual(yield* fs.readFileString(path.join(android, cache)), cache);
+ }
+ assert.deepStrictEqual(yield* fs.readDirectory(mobile), ["android"]);
+ });
+ return { fs, path, mobile, android, cleanPrebuild, assertCachesRestored };
+});
+
+it.layer(NodeServices.layer)("prebuildAndroid", (it) => {
+ it.effect("keeps Gradle and native caches through a clean prebuild", () =>
+ Effect.gen(function* () {
+ const { fs, path, mobile, android, cleanPrebuild, assertCachesRestored } = yield* fixture;
+
+ yield* prebuildAndroid(mobile, cleanPrebuild);
+
+ assert.strictEqual(
+ yield* fs.readFileString(path.join(android, "app/build.gradle")),
+ "regenerated",
+ );
+ yield* assertCachesRestored;
+ }),
+ );
+
+ it.effect("restores the caches and returns the error when prebuild fails", () =>
+ Effect.gen(function* () {
+ const { mobile, cleanPrebuild, assertCachesRestored } = yield* fixture;
+ const failure = new NativeClientError({ message: "prebuild failed" });
+
+ const error = yield* prebuildAndroid(
+ mobile,
+ cleanPrebuild.pipe(Effect.andThen(Effect.fail(failure))),
+ ).pipe(Effect.flip);
+
+ assert.strictEqual(error, failure);
+ yield* assertCachesRestored;
+ }),
+ );
+});
diff --git a/scripts/mobile-native-client.ts b/scripts/mobile-native-client.ts
index ce526196019..7db1bd20031 100644
--- a/scripts/mobile-native-client.ts
+++ b/scripts/mobile-native-client.ts
@@ -284,6 +284,45 @@ export const installedBinary = Effect.fn("installedBinary")(function* (
return yield* digest(hashes.sort().join("\n"));
});
+/**
+ * Run `expo prebuild --clean` without losing `android/.gradle` (task history) and
+ * `android/app/.cxx` (native compile outputs). Both invalidate by input, so a clean
+ * rebuild only redoes changed work. `app/build` stays out: its autolinking and JS
+ * bundle tasks do not declare every input. Each step is best-effort, because a lost
+ * cache costs one slower build and must never cost the build.
+ */
+export const prebuildAndroid = Effect.fn("nativeClient.prebuildAndroid")(function* <E, R>(
+ mobile: string,
+ prebuild: Effect.Effect<void, E, R>,
+) {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const android = path.join(mobile, "android");
+ // A sibling of android/ keeps the renames on one filesystem. It is gitignored, and a
+ // run killed mid-prebuild leaves it for the next run to clear.
+ const saved = path.join(mobile, ".android-cache");
+ const caches = [".gradle", "app/.cxx"];
+ const clear = fs.remove(saved, { recursive: true, force: true }).pipe(Effect.ignore);
+ const move = (from: string, to: string) =>
+ Effect.forEach(
+ caches,
+ (cache) =>
+ Effect.gen(function* () {
+ const source = path.join(from, cache);
+ const target = path.join(to, cache);
+ if (!(yield* fs.exists(source)) || (yield* fs.exists(target))) return;
+ yield* fs.makeDirectory(path.dirname(target), { recursive: true });
+ yield* fs.rename(source, target);
+ }).pipe(Effect.ignore),
+ { discard: true },
+ );
+ yield* Effect.acquireUseRelease(
+ clear.pipe(Effect.andThen(move(android, saved))),
+ () => prebuild,
+ () => move(saved, android).pipe(Effect.andThen(clear)),
+ );
+});
+
const main = Command.make(
"mobile-native-client",
{
@@ -333,11 +372,12 @@ const main = Command.make(
message:
"Native directory contains tracked files; clean prebuild would overwrite them.",
});
- yield* command(
+ const prebuild = command(
"vp",
["exec", "expo", "prebuild", "--clean", "--platform", platform, "--no-install"],
true,
- );
+ ).pipe(Effect.asVoid);
+ yield* platform === "android" ? prebuildAndroid((yield* roots).mobile, prebuild) : prebuild;
if (platform === "ios") {
const output = yield* fs.makeTempDirectoryScoped({ prefix: "t3-native-client-" });
const { mobile } = yield* roots;