Skip to content
TypeScript

fix(mobile): fresh Android builds no longer crash on launch (#20118)

Marcel Jiron · MarMar Labscc869a97afc1
diff --git a/apps/mobile/app.config.ts b/apps/mobile/app.config.ts
index b10b00864f2..b0bda47dc32 100644
--- a/apps/mobile/app.config.ts
+++ b/apps/mobile/app.config.ts
@@ -453,6 +453,7 @@ const config: ExpoConfig = {
...(!isIosPersonalTeamBuild ? ["./plugins/withWidgetLogoAsset.cjs", widgetsPlugin] : []),
"./plugins/withAndroidCleartextTraffic.cjs",
"./plugins/withAndroidGradleHeap.cjs",
+ "./plugins/withAndroidFbjniVersion.cjs",
"./plugins/withAndroidInputBackground.cjs",
"./plugins/withAndroidModernPopupMenu.cjs",
"./plugins/withAndroidModernAlertDialog.cjs",
diff --git a/apps/mobile/plugins/withAndroidFbjniVersion.cjs b/apps/mobile/plugins/withAndroidFbjniVersion.cjs
new file mode 100644
index 00000000000..1736f7797f7
--- /dev/null
+++ b/apps/mobile/plugins/withAndroidFbjniVersion.cjs
@@ -0,0 +1,46 @@
+const { withProjectBuildGradle } = require("expo/config-plugins");
+
+// react-native-shiki-engine 0.3.12 asks for `com.facebook.fbjni:fbjni:+`, which Gradle
+// resolves to the newest release on Maven Central. That is now 0.8.x, built
+// with NDK 28, while React Native links the NDK 27.1 C++ runtime and requires
+// 0.7.0. The two disagree on libc++, so the app dies in
+// ReactNativeFeatureFlagsCxxInterop's static initializer before any JavaScript
+// runs ("cannot locate symbol __cxa_init_primary_exception").
+//
+// Keep this equal to `fbjni` in react-native/gradle/libs.versions.toml, and move
+// it with React Native. withAndroidFbjniVersion.test.mjs fails when they differ.
+const FBJNI_VERSION = "0.7.0";
+const MARKER = "withAndroidFbjniVersion";
+
+const BLOCKS = {
+ groovy: `
+// Added by ${MARKER}: keep fbjni on the version React Native ships against.
+allprojects {
+ configurations.all {
+ resolutionStrategy.force 'com.facebook.fbjni:fbjni:${FBJNI_VERSION}'
+ }
+}
+`,
+ kt: `
+// Added by ${MARKER}: keep fbjni on the version React Native ships against.
+allprojects {
+ configurations.all {
+ resolutionStrategy.force("com.facebook.fbjni:fbjni:${FBJNI_VERSION}")
+ }
+}
+`,
+};
+
+module.exports = function withAndroidFbjniVersion(config) {
+ return withProjectBuildGradle(config, (nextConfig) => {
+ const { contents, language } = nextConfig.modResults;
+ const block = BLOCKS[language];
+ if (block === undefined) {
+ throw new Error(`${MARKER}: unsupported root build.gradle language "${language}".`);
+ }
+ if (!contents.includes(MARKER)) {
+ nextConfig.modResults.contents = `${contents.trimEnd()}\n${block}`;
+ }
+ return nextConfig;
+ });
+};
diff --git a/apps/mobile/plugins/withAndroidFbjniVersion.test.mjs b/apps/mobile/plugins/withAndroidFbjniVersion.test.mjs
new file mode 100644
index 00000000000..388afbf3f67
--- /dev/null
+++ b/apps/mobile/plugins/withAndroidFbjniVersion.test.mjs
@@ -0,0 +1,108 @@
+import * as NodeFS from "node:fs";
+import * as NodeModule from "node:module";
+import * as NodeOS from "node:os";
+import * as NodePath from "node:path";
+import { afterEach, describe, expect, it } from "vite-plus/test";
+
+const require = NodeModule.createRequire(import.meta.url);
+const { compileModsAsync } = require("expo/config-plugins");
+const { getConfig } = require("expo/config");
+const withAndroidFbjniVersion = require("./withAndroidFbjniVersion.cjs");
+
+const catalog = NodeFS.readFileSync(
+ NodePath.join(require.resolve("react-native/package.json"), "..", "gradle", "libs.versions.toml"),
+ "utf8",
+);
+const shippedFbjniVersion = /^fbjni\s*=\s*"([^"]+)"/m.exec(catalog)?.[1];
+
+// The root build.gradle generated by Expo SDK 58.
+const ROOT_BUILD_GRADLE = `// Top-level build file where you can add configuration options common to all sub-projects/modules.
+
+buildscript {
+ repositories {
+ google()
+ mavenCentral()
+ }
+ dependencies {
+ classpath('com.android.tools.build:gradle')
+ classpath('com.facebook.react:react-native-gradle-plugin')
+ def kotlinVersion = findProperty('android.kotlinVersion')
+ classpath(kotlinVersion ? "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" : 'org.jetbrains.kotlin:kotlin-gradle-plugin')
+ }
+}
+
+allprojects {
+ repositories {
+ google()
+ mavenCentral()
+ maven { url 'https://www.jitpack.io' }
+ }
+}
+
+apply plugin: "expo-root-project"
+apply plugin: "com.facebook.react.rootproject"
+`;
+
+const projectRoots = [];
+
+afterEach(() => {
+ for (const root of projectRoots.splice(0)) NodeFS.rmSync(root, { recursive: true, force: true });
+});
+
+/** Runs the plugin through Expo's own prebuild mod pipeline against a real file. */
+async function prebuildRootGradle(fileName, contents, plugin = withAndroidFbjniVersion) {
+ const projectRoot = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "fbjni-plugin-"));
+ projectRoots.push(projectRoot);
+ NodeFS.mkdirSync(NodePath.join(projectRoot, "android"));
+ NodeFS.writeFileSync(NodePath.join(projectRoot, "android", fileName), contents);
+ await compileModsAsync(plugin({ name: "t3", slug: "t3" }), {
+ projectRoot,
+ platforms: ["android"],
+ });
+ return NodeFS.readFileSync(NodePath.join(projectRoot, "android", fileName), "utf8");
+}
+
+function expectProjectWideForce(result, language = "groovy") {
+ expect(shippedFbjniVersion).toBeDefined();
+ const coordinate = `com.facebook.fbjni:fbjni:${shippedFbjniVersion}`;
+ const force = language === "groovy" ? ` '${coordinate}'` : `("${coordinate}")`;
+ // Anchor the complete top-level block, including scope, at the end of the file.
+ const expected = `\nallprojects {\n configurations.all {\n resolutionStrategy.force${force}\n }\n}\n`;
+ expect(result.slice(-expected.length)).toBe(expected);
+}
+
+describe("withAndroidFbjniVersion", () => {
+ it("forces the React Native fbjni version for every Gradle project", async () => {
+ const result = await prebuildRootGradle("build.gradle", ROOT_BUILD_GRADLE);
+
+ expect(result.startsWith(ROOT_BUILD_GRADLE.trimEnd())).toBe(true);
+ expectProjectWideForce(result);
+ });
+
+ it("adds the force once when prebuild runs again", async () => {
+ const once = await prebuildRootGradle("build.gradle", ROOT_BUILD_GRADLE);
+ const twice = await prebuildRootGradle("build.gradle", once);
+
+ expect(twice).toBe(once);
+ });
+
+ it("writes Kotlin syntax into a Kotlin build script", async () => {
+ const result = await prebuildRootGradle("build.gradle.kts", "plugins {}\n");
+
+ expectProjectWideForce(result, "kt");
+ });
+
+ it("applies the force through the registered app config", async () => {
+ const { exp } = getConfig(NodePath.resolve(import.meta.dirname, ".."), {
+ isModdedConfig: true,
+ });
+ const projectBuildGradle = exp.mods?.android?.projectBuildGradle;
+ // Run the app's registered root-Gradle mods without its unrelated native-file mods.
+ const result = await prebuildRootGradle("build.gradle", ROOT_BUILD_GRADLE, (config) => ({
+ ...config,
+ mods: { android: projectBuildGradle ? { projectBuildGradle } : {} },
+ }));
+
+ expectProjectWideForce(result);
+ });
+});