diff --git a/snapshot/android/project-snapshot-generator.js b/snapshot/android/project-snapshot-generator.js index 32749337..3230613e 100644 --- a/snapshot/android/project-snapshot-generator.js +++ b/snapshot/android/project-snapshot-generator.js @@ -148,8 +148,6 @@ ProjectSnapshotGenerator.prototype.generate = function (generationOptions) { shelljs.mkdir("-p", this.getBuildPath()); const snapshotToolsPath = resolveRelativePath(generationOptions.snapshotToolsPath) || CONSTANTS.SNAPSHOT_TMP_DIR; - const androidNdkPath = generationOptions.androidNdkPath || process.env.ANDROID_NDK_HOME; - console.log("Snapshot tools path: " + snapshotToolsPath); // Generate snapshots @@ -178,7 +176,7 @@ ProjectSnapshotGenerator.prototype.generate = function (generationOptions) { preprocessedInputFile: generationOptions.preprocessedInputFile, useLibs: generationOptions.useLibs || false, inputFiles: generationOptions.inputFiles || [join(this.options.projectRoot, "__snapshot.js")], - androidNdkPath, + androidNdkPath: generationOptions.androidNdkPath, mksnapshotParams: mksnapshotParams, snapshotInDocker: generationOptions.snapshotInDocker, recommendedAndroidNdkRevision diff --git a/snapshot/android/snapshot-generator.js b/snapshot/android/snapshot-generator.js index 9a15e6ee..dac5e4cf 100644 --- a/snapshot/android/snapshot-generator.js +++ b/snapshot/android/snapshot-generator.js @@ -210,6 +210,7 @@ SnapshotGenerator.prototype.buildSnapshotLibs = function (androidNdkPath, recomm SnapshotGenerator.prototype.getAndroidNdkBuildPath = function (androidNdkPath, recommendedAndroidNdkRevision) { const ndkBuildExecutableName = "ndk-build"; + let hasNdk = false; // fallback for Android Runtime < 6.2.0 with the 6.1.0 value recommendedAndroidNdkRevision = recommendedAndroidNdkRevision || "20.0.5594570"; let androidNdkBuildPath = ""; @@ -220,29 +221,52 @@ SnapshotGenerator.prototype.getAndroidNdkBuildPath = function (androidNdkPath, r if (!fs.existsSync(androidNdkBuildPath)) { throw new Error(`The provided Android NDK path does not contain ${ndkBuildExecutableName} executable.`); } else if (localNdkRevision !== recommendedAndroidNdkRevision) { - warn(`The provided Android NDK is v${localNdkRevision} while the recommended one is v${recommendedAndroidNdkRevision}`); + warn(this.getRecommendedNdkWarning(localNdkRevision, recommendedAndroidNdkRevision)); } + + hasNdk = true; + console.log("Using Android NDK from webpack.config."); } else { - // available globally - let hasAndroidNdkInPath = true; - androidNdkBuildPath = ndkBuildExecutableName; - try { - child_process.execSync(`${androidNdkBuildPath} --version`); - console.log(`Cannot determine the version of the global Android NDK. The recommended versions is v${recommendedAndroidNdkRevision}`); - } catch (_) { - hasAndroidNdkInPath = false; + if (process.env.ANDROID_NDK_HOME) { + // check ANDROID_NDK_HOME + const localNdkRevision = this.getAndroidNdkRevision(process.env.ANDROID_NDK_HOME); + androidNdkBuildPath = join(process.env.ANDROID_NDK_HOME, ndkBuildExecutableName); + if (fs.existsSync(androidNdkBuildPath)) { + hasNdk = true; + console.log("Using Android NDK from ANDROID_NDK_HOME."); + } + + if (localNdkRevision !== recommendedAndroidNdkRevision) { + warn(this.getRecommendedNdkWarning(localNdkRevision, recommendedAndroidNdkRevision)); + } + } + + if (!hasNdk) { + // available globally + androidNdkBuildPath = ndkBuildExecutableName; + try { + child_process.execSync(`${androidNdkBuildPath} --version`, { stdio: "ignore" }); + hasNdk = true; + console.log("Using Android NDK from PATH."); + console.log(`Cannot determine the version of the global Android NDK. The recommended versions is v${recommendedAndroidNdkRevision}`); + } catch (_) { + } } - if (!hasAndroidNdkInPath) { + if (!hasNdk) { // installed in ANDROID_HOME - const androidHome = process.env.ANDROID_HOME; - androidNdkBuildPath = join(androidHome, "ndk", recommendedAndroidNdkRevision, ndkBuildExecutableName); - if (!fs.existsSync(androidNdkBuildPath)) { - 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`); + androidNdkBuildPath = join(process.env.ANDROID_HOME, "ndk", recommendedAndroidNdkRevision, ndkBuildExecutableName); + if (fs.existsSync(androidNdkBuildPath)) { + hasNdk = true; + console.log("Using Android NDK from ANDROID_HOME."); } } } + if (!hasNdk) { + 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`); + } + return androidNdkBuildPath; } @@ -369,6 +393,10 @@ SnapshotGenerator.prototype.buildCSource = function (androidArch, blobInputDir, }); } +SnapshotGenerator.prototype.getRecommendedNdkWarning = function (localNdkRevision, recommendedAndroidNdkRevision) { + return `The provided Android NDK is v${localNdkRevision} while the recommended one is v${recommendedAndroidNdkRevision}`; +} + SnapshotGenerator.prototype.runMksnapshotTool = function (tool, mksnapshotParams, inputFile, snapshotInDocker, snapshotToolsPath, buildCSource) { const toolPath = tool.path; const androidArch = this.convertToAndroidArchName(tool.arch);