From a603da61900a1127b3c8729e4ae0bd71b760b40b Mon Sep 17 00:00:00 2001 From: Jaime Bernardo Date: Mon, 26 Feb 2018 17:31:46 +0000 Subject: [PATCH] android: cache native modules override preference Saves the NODEJS_MOBILE_BUILD_NATIVE_MODULES environment variable value to a file if it is defined during the Cordova prepare phase so it can read by Gradle. Useful for using Android Studio on macOS, since windowed applications don't have access to the shell environment variables unless they are started from the shell. --- ...ter-prepare-create-macOS-builder-helper.js | 29 ++++++++++++++++--- src/android/build.gradle | 11 +++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/install/hooks/android/after-prepare-create-macOS-builder-helper.js b/install/hooks/android/after-prepare-create-macOS-builder-helper.js index 4fb8887..6c9174f 100644 --- a/install/hooks/android/after-prepare-create-macOS-builder-helper.js +++ b/install/hooks/android/after-prepare-create-macOS-builder-helper.js @@ -1,15 +1,21 @@ const fs = require('fs'); const path = require('path'); +// Gets the platform's www path. +function getPlatformWWWPath(context, platform) +{ + var platformPath = path.join(context.opts.projectRoot, 'platforms', platform); + var platformAPI = require(path.join(platformPath, 'cordova', 'Api')); + var platformAPIInstance = new platformAPI(); + return platformAPIInstance.locations.www; +} + // Adds a helper script to run "npm rebuild" with the current PATH. // This workaround is needed for Android Studio on macOS when it is not started // from the command line, as npm probably won't be in the PATH at build time. function buildMacOSHelperNpmBuildScript(context, platform) { - var platformPath = path.join(context.opts.projectRoot, 'platforms', platform); - var platformAPI = require(path.join(platformPath, 'cordova', 'Api')); - var platformAPIInstance = new platformAPI(); - var wwwPath = platformAPIInstance.locations.www; + var wwwPath = getPlatformWWWPath(context, platform); var helperMacOSBuildScriptPath = path.join(wwwPath, 'build-native-modules-MacOS-helper-script.sh'); fs.writeFileSync( helperMacOSBuildScriptPath,`#!/bin/bash export PATH=$PATH:${process.env.PATH} @@ -18,11 +24,26 @@ function buildMacOSHelperNpmBuildScript(context, platform) ); } +// Adds a file to save the contents of the NODEJS_MOBILE_BUILD_NATIVE_MODULES +// environment variable if it is set during the prepare step. +// This workaround is needed for Android Studio on macOS when it is not started +// from the command line, since environment variables set in the shell won't +// be available. +function saveBuildNativeModulesPreference(context, platform) +{ + var wwwPath = getPlatformWWWPath(context, platform); + var saveBuildNativeModulesPreferencePath = path.join(wwwPath, 'NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt'); + if (process.env.NODEJS_MOBILE_BUILD_NATIVE_MODULES !== undefined) { + fs.writeFileSync(saveBuildNativeModulesPreferencePath, process.env.NODEJS_MOBILE_BUILD_NATIVE_MODULES); + } +} + module.exports = function(context) { if (context.opts.platforms.indexOf('android') >= 0) { if (process.platform === 'darwin') { buildMacOSHelperNpmBuildScript(context, 'android'); } + saveBuildNativeModulesPreference(context, 'android'); } } diff --git a/src/android/build.gradle b/src/android/build.gradle index 61c2609..4f03525 100644 --- a/src/android/build.gradle +++ b/src/android/build.gradle @@ -42,12 +42,23 @@ cdvPluginPostBuildExtras += { -> android.aaptOptions.ignoreAssetsPattern += ":!*.gz"; // The MacOS builder helper script is not needed inside the APK. android.aaptOptions.ignoreAssetsPattern += ":!build-native-modules-MacOS-helper-script.sh"; + // The file that caches the value of NODEJS_MOBILE_BUILD_NATIVE_MODULES is not needed inside the APK. + android.aaptOptions.ignoreAssetsPattern += ":!NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt"; }; import org.gradle.internal.os.OperatingSystem; String shouldRebuildNativeModules = System.getenv('NODEJS_MOBILE_BUILD_NATIVE_MODULES'); +if (shouldRebuildNativeModules==null) { +// If the environment variable is not set right now, check if it has been saved to a file during +// the cordova prepare phase. + def nativeModulesPreferenceFile = file("${project.projectDir}/assets/www/NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt"); + if (nativeModulesPreferenceFile.exists()) { + shouldRebuildNativeModules=nativeModulesPreferenceFile.text; + } +} + if (shouldRebuildNativeModules==null) { // If build native modules preference is not set, try to find .gyp files to turn it on. shouldRebuildNativeModules="0";