Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit a23b3a8

Browse files
committed
refactor: improve android NDK logging
1 parent 2b77549 commit a23b3a8

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

Diff for: snapshot/android/project-snapshot-generator.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ ProjectSnapshotGenerator.prototype.generate = function (generationOptions) {
148148
shelljs.mkdir("-p", this.getBuildPath());
149149

150150
const snapshotToolsPath = resolveRelativePath(generationOptions.snapshotToolsPath) || CONSTANTS.SNAPSHOT_TMP_DIR;
151-
const androidNdkPath = generationOptions.androidNdkPath || process.env.ANDROID_NDK_HOME;
152-
153151
console.log("Snapshot tools path: " + snapshotToolsPath);
154152

155153
// Generate snapshots
@@ -178,7 +176,7 @@ ProjectSnapshotGenerator.prototype.generate = function (generationOptions) {
178176
preprocessedInputFile: generationOptions.preprocessedInputFile,
179177
useLibs: generationOptions.useLibs || false,
180178
inputFiles: generationOptions.inputFiles || [join(this.options.projectRoot, "__snapshot.js")],
181-
androidNdkPath,
179+
androidNdkPath: generationOptions.androidNdkPath,
182180
mksnapshotParams: mksnapshotParams,
183181
snapshotInDocker: generationOptions.snapshotInDocker,
184182
recommendedAndroidNdkRevision

Diff for: snapshot/android/snapshot-generator.js

+42-14
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ SnapshotGenerator.prototype.buildSnapshotLibs = function (androidNdkPath, recomm
210210

211211
SnapshotGenerator.prototype.getAndroidNdkBuildPath = function (androidNdkPath, recommendedAndroidNdkRevision) {
212212
const ndkBuildExecutableName = "ndk-build";
213+
let hasNdk = false;
213214
// fallback for Android Runtime < 6.2.0 with the 6.1.0 value
214215
recommendedAndroidNdkRevision = recommendedAndroidNdkRevision || "20.0.5594570";
215216
let androidNdkBuildPath = "";
@@ -220,29 +221,52 @@ SnapshotGenerator.prototype.getAndroidNdkBuildPath = function (androidNdkPath, r
220221
if (!fs.existsSync(androidNdkBuildPath)) {
221222
throw new Error(`The provided Android NDK path does not contain ${ndkBuildExecutableName} executable.`);
222223
} else if (localNdkRevision !== recommendedAndroidNdkRevision) {
223-
warn(`The provided Android NDK is v${localNdkRevision} while the recommended one is v${recommendedAndroidNdkRevision}`);
224+
warn(this.getRecommendedNdkWarning(localNdkRevision, recommendedAndroidNdkRevision));
224225
}
226+
227+
hasNdk = true;
228+
console.log("Using Android NDK from webpack.config.");
225229
} else {
226-
// available globally
227-
let hasAndroidNdkInPath = true;
228-
androidNdkBuildPath = ndkBuildExecutableName;
229-
try {
230-
child_process.execSync(`${androidNdkBuildPath} --version`);
231-
console.log(`Cannot determine the version of the global Android NDK. The recommended versions is v${recommendedAndroidNdkRevision}`);
232-
} catch (_) {
233-
hasAndroidNdkInPath = false;
230+
if (process.env.ANDROID_NDK_HOME) {
231+
// check ANDROID_NDK_HOME
232+
const localNdkRevision = this.getAndroidNdkRevision(process.env.ANDROID_NDK_HOME);
233+
androidNdkBuildPath = join(process.env.ANDROID_NDK_HOME, ndkBuildExecutableName);
234+
if (fs.existsSync(androidNdkBuildPath)) {
235+
hasNdk = true;
236+
console.log("Using Android NDK from ANDROID_NDK_HOME.");
237+
}
238+
239+
if (localNdkRevision !== recommendedAndroidNdkRevision) {
240+
warn(this.getRecommendedNdkWarning(localNdkRevision, recommendedAndroidNdkRevision));
241+
}
242+
}
243+
244+
if (!hasNdk) {
245+
// available globally
246+
androidNdkBuildPath = ndkBuildExecutableName;
247+
try {
248+
child_process.execSync(`${androidNdkBuildPath} --version`, { stdio: "ignore" });
249+
hasNdk = true;
250+
console.log("Using Android NDK from PATH.");
251+
console.log(`Cannot determine the version of the global Android NDK. The recommended versions is v${recommendedAndroidNdkRevision}`);
252+
} catch (_) {
253+
}
234254
}
235255

236-
if (!hasAndroidNdkInPath) {
256+
if (!hasNdk) {
237257
// installed in ANDROID_HOME
238-
const androidHome = process.env.ANDROID_HOME;
239-
androidNdkBuildPath = join(androidHome, "ndk", recommendedAndroidNdkRevision, ndkBuildExecutableName);
240-
if (!fs.existsSync(androidNdkBuildPath)) {
241-
throw new Error(`Android NDK v${recommendedAndroidNdkRevision} is not installed. You can find installation instructions in this article: https://developer.android.com/studio/projects/install-ndk#specific-version`);
258+
androidNdkBuildPath = join(process.env.ANDROID_HOME, "ndk", recommendedAndroidNdkRevision, ndkBuildExecutableName);
259+
if (fs.existsSync(androidNdkBuildPath)) {
260+
hasNdk = true;
261+
console.log("Using Android NDK from ANDROID_HOME.");
242262
}
243263
}
244264
}
245265

266+
if (!hasNdk) {
267+
throw new Error(`Android NDK v${recommendedAndroidNdkRevision} is not installed. Install it from Android Studio or download it and set ANDROID_NDK_HOME or add it to your PATH. You can find installation instructions in this article: https://developer.android.com/studio/projects/install-ndk#specific-version`);
268+
}
269+
246270
return androidNdkBuildPath;
247271
}
248272

@@ -369,6 +393,10 @@ SnapshotGenerator.prototype.buildCSource = function (androidArch, blobInputDir,
369393
});
370394
}
371395

396+
SnapshotGenerator.prototype.getRecommendedNdkWarning = function (localNdkRevision, recommendedAndroidNdkRevision) {
397+
return `The provided Android NDK is v${localNdkRevision} while the recommended one is v${recommendedAndroidNdkRevision}`;
398+
}
399+
372400
SnapshotGenerator.prototype.runMksnapshotTool = function (tool, mksnapshotParams, inputFile, snapshotInDocker, snapshotToolsPath, buildCSource) {
373401
const toolPath = tool.path;
374402
const androidArch = this.convertToAndroidArchName(tool.arch);

0 commit comments

Comments
 (0)