Skip to content

Commit 271458c

Browse files
committed
Ensure REACT_NATIVE_OVERRIDE_HERMES_DIR is set
1 parent eaac915 commit 271458c

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

packages/host/android/build.gradle

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ dependencies {
134134
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
135135
}
136136

137+
task checkHermesOverride {
138+
doFirst {
139+
if (!System.getenv("REACT_NATIVE_OVERRIDE_HERMES_DIR")) {
140+
throw new GradleException([
141+
"React Native Node-API needs a custom version of Hermes with Node-API enabled.",
142+
"Run the following in your terminal, to clone Hermes and instruct React Native to use it:",
143+
"",
144+
"export REACT_NATIVE_OVERRIDE_HERMES_DIR=`npx react-native-node-api vendor-hermes --silent --force`",
145+
"",
146+
"And follow this guide to build React Native from source:",
147+
"https://reactnative.dev/contributing/how-to-build-from-source#update-your-project-to-build-from-source"
148+
].join('\n'))
149+
}
150+
}
151+
}
152+
137153
// Custom task to fetch jniLibs paths via CLI
138154
task linkNodeApiModules {
139155
doLast {
@@ -150,5 +166,5 @@ task linkNodeApiModules {
150166
}
151167
}
152168

153-
preBuild.dependsOn linkNodeApiModules
169+
preBuild.dependsOn checkHermesOverride, linkNodeApiModules
154170

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import assert from "node:assert/strict";
2+
import { describe, it } from "node:test";
3+
import cp from "node:child_process";
4+
import path from "node:path";
5+
6+
const PACKAGE_ROOT = path.join(__dirname, "../..");
7+
const MONOREPO_ROOT = path.join(PACKAGE_ROOT, "../..");
8+
const TEST_APP_ANDROID_PATH = path.join(MONOREPO_ROOT, "apps/test-app/android");
9+
10+
describe("Gradle tasks", () => {
11+
describe("checkHermesOverride task", () => {
12+
it("should fail if REACT_NATIVE_OVERRIDE_HERMES_DIR is not set", () => {
13+
const { status, stdout, stderr } = cp.spawnSync(
14+
"sh",
15+
["gradlew", "react-native-node-api:checkHermesOverride"],
16+
{
17+
cwd: TEST_APP_ANDROID_PATH,
18+
env: {
19+
...process.env,
20+
REACT_NATIVE_OVERRIDE_HERMES_DIR: undefined,
21+
},
22+
encoding: "utf-8",
23+
},
24+
);
25+
26+
assert.notEqual(status, 0, `Expected failure: ${stdout} ${stderr}`);
27+
assert.match(
28+
stderr,
29+
/React Native Node-API needs a custom version of Hermes with Node-API enabled/,
30+
);
31+
assert.match(
32+
stderr,
33+
/Run the following in your terminal, to clone Hermes and instruct React Native to use it/,
34+
);
35+
assert.match(
36+
stderr,
37+
/export REACT_NATIVE_OVERRIDE_HERMES_DIR=`npx react-native-node-api vendor-hermes --silent --force`/,
38+
);
39+
assert.match(
40+
stderr,
41+
/And follow this guide to build React Native from source/,
42+
);
43+
});
44+
});
45+
});

0 commit comments

Comments
 (0)