Skip to content

Commit cc869a9

Browse files
authored
fix(mobile): fresh Android builds no longer crash on launch (pingdotgg#20118)
1 parent 8a94807 commit cc869a9

3 files changed

Lines changed: 155 additions & 0 deletions

File tree

‎apps/mobile/app.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ const config: ExpoConfig = {
453453
...(!isIosPersonalTeamBuild ? ["./plugins/withWidgetLogoAsset.cjs", widgetsPlugin] : []),
454454
"./plugins/withAndroidCleartextTraffic.cjs",
455455
"./plugins/withAndroidGradleHeap.cjs",
456+
"./plugins/withAndroidFbjniVersion.cjs",
456457
"./plugins/withAndroidInputBackground.cjs",
457458
"./plugins/withAndroidModernPopupMenu.cjs",
458459
"./plugins/withAndroidModernAlertDialog.cjs",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { withProjectBuildGradle } = require("expo/config-plugins");
2+
3+
// react-native-shiki-engine 0.3.12 asks for `com.facebook.fbjni:fbjni:+`, which Gradle
4+
// resolves to the newest release on Maven Central. That is now 0.8.x, built
5+
// with NDK 28, while React Native links the NDK 27.1 C++ runtime and requires
6+
// 0.7.0. The two disagree on libc++, so the app dies in
7+
// ReactNativeFeatureFlagsCxxInterop's static initializer before any JavaScript
8+
// runs ("cannot locate symbol __cxa_init_primary_exception").
9+
//
10+
// Keep this equal to `fbjni` in react-native/gradle/libs.versions.toml, and move
11+
// it with React Native. withAndroidFbjniVersion.test.mjs fails when they differ.
12+
const FBJNI_VERSION = "0.7.0";
13+
const MARKER = "withAndroidFbjniVersion";
14+
15+
const BLOCKS = {
16+
groovy: `
17+
// Added by ${MARKER}: keep fbjni on the version React Native ships against.
18+
allprojects {
19+
configurations.all {
20+
resolutionStrategy.force 'com.facebook.fbjni:fbjni:${FBJNI_VERSION}'
21+
}
22+
}
23+
`,
24+
kt: `
25+
// Added by ${MARKER}: keep fbjni on the version React Native ships against.
26+
allprojects {
27+
configurations.all {
28+
resolutionStrategy.force("com.facebook.fbjni:fbjni:${FBJNI_VERSION}")
29+
}
30+
}
31+
`,
32+
};
33+
34+
module.exports = function withAndroidFbjniVersion(config) {
35+
return withProjectBuildGradle(config, (nextConfig) => {
36+
const { contents, language } = nextConfig.modResults;
37+
const block = BLOCKS[language];
38+
if (block === undefined) {
39+
throw new Error(`${MARKER}: unsupported root build.gradle language "${language}".`);
40+
}
41+
if (!contents.includes(MARKER)) {
42+
nextConfig.modResults.contents = `${contents.trimEnd()}\n${block}`;
43+
}
44+
return nextConfig;
45+
});
46+
};
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import * as NodeFS from "node:fs";
2+
import * as NodeModule from "node:module";
3+
import * as NodeOS from "node:os";
4+
import * as NodePath from "node:path";
5+
import { afterEach, describe, expect, it } from "vite-plus/test";
6+
7+
const require = NodeModule.createRequire(import.meta.url);
8+
const { compileModsAsync } = require("expo/config-plugins");
9+
const { getConfig } = require("expo/config");
10+
const withAndroidFbjniVersion = require("./withAndroidFbjniVersion.cjs");
11+
12+
const catalog = NodeFS.readFileSync(
13+
NodePath.join(require.resolve("react-native/package.json"), "..", "gradle", "libs.versions.toml"),
14+
"utf8",
15+
);
16+
const shippedFbjniVersion = /^fbjni\s*=\s*"([^"]+)"/m.exec(catalog)?.[1];
17+
18+
// The root build.gradle generated by Expo SDK 58.
19+
const ROOT_BUILD_GRADLE = `// Top-level build file where you can add configuration options common to all sub-projects/modules.
20+
21+
buildscript {
22+
repositories {
23+
google()
24+
mavenCentral()
25+
}
26+
dependencies {
27+
classpath('com.android.tools.build:gradle')
28+
classpath('com.facebook.react:react-native-gradle-plugin')
29+
def kotlinVersion = findProperty('android.kotlinVersion')
30+
classpath(kotlinVersion ? "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" : 'org.jetbrains.kotlin:kotlin-gradle-plugin')
31+
}
32+
}
33+
34+
allprojects {
35+
repositories {
36+
google()
37+
mavenCentral()
38+
maven { url 'https://www.jitpack.io' }
39+
}
40+
}
41+
42+
apply plugin: "expo-root-project"
43+
apply plugin: "com.facebook.react.rootproject"
44+
`;
45+
46+
const projectRoots = [];
47+
48+
afterEach(() => {
49+
for (const root of projectRoots.splice(0)) NodeFS.rmSync(root, { recursive: true, force: true });
50+
});
51+
52+
/** Runs the plugin through Expo's own prebuild mod pipeline against a real file. */
53+
async function prebuildRootGradle(fileName, contents, plugin = withAndroidFbjniVersion) {
54+
const projectRoot = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "fbjni-plugin-"));
55+
projectRoots.push(projectRoot);
56+
NodeFS.mkdirSync(NodePath.join(projectRoot, "android"));
57+
NodeFS.writeFileSync(NodePath.join(projectRoot, "android", fileName), contents);
58+
await compileModsAsync(plugin({ name: "t3", slug: "t3" }), {
59+
projectRoot,
60+
platforms: ["android"],
61+
});
62+
return NodeFS.readFileSync(NodePath.join(projectRoot, "android", fileName), "utf8");
63+
}
64+
65+
function expectProjectWideForce(result, language = "groovy") {
66+
expect(shippedFbjniVersion).toBeDefined();
67+
const coordinate = `com.facebook.fbjni:fbjni:${shippedFbjniVersion}`;
68+
const force = language === "groovy" ? ` '${coordinate}'` : `("${coordinate}")`;
69+
// Anchor the complete top-level block, including scope, at the end of the file.
70+
const expected = `\nallprojects {\n configurations.all {\n resolutionStrategy.force${force}\n }\n}\n`;
71+
expect(result.slice(-expected.length)).toBe(expected);
72+
}
73+
74+
describe("withAndroidFbjniVersion", () => {
75+
it("forces the React Native fbjni version for every Gradle project", async () => {
76+
const result = await prebuildRootGradle("build.gradle", ROOT_BUILD_GRADLE);
77+
78+
expect(result.startsWith(ROOT_BUILD_GRADLE.trimEnd())).toBe(true);
79+
expectProjectWideForce(result);
80+
});
81+
82+
it("adds the force once when prebuild runs again", async () => {
83+
const once = await prebuildRootGradle("build.gradle", ROOT_BUILD_GRADLE);
84+
const twice = await prebuildRootGradle("build.gradle", once);
85+
86+
expect(twice).toBe(once);
87+
});
88+
89+
it("writes Kotlin syntax into a Kotlin build script", async () => {
90+
const result = await prebuildRootGradle("build.gradle.kts", "plugins {}\n");
91+
92+
expectProjectWideForce(result, "kt");
93+
});
94+
95+
it("applies the force through the registered app config", async () => {
96+
const { exp } = getConfig(NodePath.resolve(import.meta.dirname, ".."), {
97+
isModdedConfig: true,
98+
});
99+
const projectBuildGradle = exp.mods?.android?.projectBuildGradle;
100+
// Run the app's registered root-Gradle mods without its unrelated native-file mods.
101+
const result = await prebuildRootGradle("build.gradle", ROOT_BUILD_GRADLE, (config) => ({
102+
...config,
103+
mods: { android: projectBuildGradle ? { projectBuildGradle } : {} },
104+
}));
105+
106+
expectProjectWideForce(result);
107+
});
108+
});

0 commit comments

Comments
 (0)