Skip to content

Commit

Permalink
android: support new platform project structure
Browse files Browse the repository at this point in the history
Supports the new Android project structure introduced in
cordova-android 7.X .
  • Loading branch information
jaimecbernardo committed Mar 4, 2019
1 parent 94ff478 commit ce81228
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
27 changes: 18 additions & 9 deletions install/hooks/android/after-prepare-build-node-assets-lists.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
var fs = require('fs');

const NODEJS_PROJECT_ROOT = 'www/nodejs-project';
const FILE_LIST_PATH = 'platforms/android/assets/file.list';
const DIR_LIST_PATH = 'platforms/android/assets/dir.list';
var path = require('path');

var fileList = [];
var dirList = [];
Expand All @@ -27,11 +24,19 @@ function enumFolder(folderPath) {
}
}

function createFileAndFolderLists(callback) {
enumFolder(NODEJS_PROJECT_ROOT);
function createFileAndFolderLists(context, callback) {
try {
fs.writeFileSync(FILE_LIST_PATH, fileList.join('\n'));
fs.writeFileSync(DIR_LIST_PATH, dirList.join('\n'));
var cordovaLib = context.requireCordovaModule('cordova-lib');
var platformAPI = cordovaLib.cordova_platforms.getPlatformApi('android');
var nodeJsProjectRoot = path.join('www', 'nodejs-project');
// The Android application's assets path will be the parent of the application's www folder.
var androidAssetsPath = path.join(platformAPI.locations.www,'..');
var fileListPath = path.join(androidAssetsPath,'file.list');
var dirListPath = path.join(androidAssetsPath,'dir.list');

enumFolder(nodeJsProjectRoot);
fs.writeFileSync(fileListPath, fileList.join('\n'));
fs.writeFileSync(dirListPath, dirList.join('\n'));
} catch (err) {
console.log(err);
callback(err);
Expand All @@ -41,10 +46,14 @@ function createFileAndFolderLists(callback) {
}

module.exports = function(context) {
if (context.opts.platforms.indexOf('android') < 0) {
return;
}

var Q = context.requireCordovaModule('q');
var deferral = new Q.defer();

createFileAndFolderLists(function(err) {
createFileAndFolderLists(context, function(err) {
if (err) {
deferral.reject(err);
} else {
Expand Down
10 changes: 5 additions & 5 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@

<source-file src="src/android/java/com/janeasystems/cdvnodejsmobile/NodeJS.java" target-dir="src/com/janeasystems/cdvnodejsmobile/" />

<source-file src="src/common/cordova-bridge/cordova-bridge.h" target-dir="src/com/janeasystems/cdvnodejsmobile/jni/" />
<source-file src="src/common/cordova-bridge/cordova-bridge.cpp" target-dir="src/com/janeasystems/cdvnodejsmobile/jni/" />
<source-file src="src/android/jni/native-lib.cpp" target-dir="src/com/janeasystems/cdvnodejsmobile/jni/" />
<source-file src="src/common/cordova-bridge/cordova-bridge.h" target-dir="libs/cdvnodejsmobile/" />
<source-file src="src/common/cordova-bridge/cordova-bridge.cpp" target-dir="libs/cdvnodejsmobile/" />
<source-file src="src/android/jni/native-lib.cpp" target-dir="libs/cdvnodejsmobile/" />

<source-file src="libs/android/libnode/" target-dir="src/com/janeasystems/cdvnodejsmobile/jni/" />
<source-file src="libs/android/libnode/" target-dir="libs/cdvnodejsmobile/" />

<source-file src="install/nodejs-mobile-cordova-assets/" target-dir="assets/" />

<framework src="src/android/build.gradle" custom="true" type="gradleReference" />
<source-file src="src/android/CMakeLists.txt" target-dir="src/com/janeasystems/cdvnodejsmobile/jni/"/>
<source-file src="src/android/CMakeLists.txt" target-dir="libs/cdvnodejsmobile/"/>

</platform>

Expand Down
28 changes: 19 additions & 9 deletions src/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ android {

externalNativeBuild {
cmake {
path "src/com/janeasystems/cdvnodejsmobile/jni/CMakeLists.txt"
path "libs/cdvnodejsmobile/CMakeLists.txt"
}
}

Expand All @@ -43,13 +43,23 @@ cdvPluginPostBuildExtras += { ->
};

import org.gradle.internal.os.OperatingSystem;
String projectWWW; // www assets folder from the Application project.
if ( file("${project.projectDir}/src/main/assets/www/").exists() ) {
// www folder for cordova-android >= 7
projectWWW = "${project.projectDir}/src/main/assets/www";
} else if (file("${project.projectDir}/assets/www/").exists()) {
// www folder for cordova-android < 7
projectWWW = "${project.projectDir}/assets/www";
} else {
throw new GradleException('nodejs-mobile-cordova couldn\'t find the www folder in the Android project.');
}

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");
def nativeModulesPreferenceFile = file("${projectWWW}/NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt");
if (nativeModulesPreferenceFile.exists()) {
shouldRebuildNativeModules=nativeModulesPreferenceFile.text;
}
Expand All @@ -59,7 +69,7 @@ if (shouldRebuildNativeModules==null) {
// If build native modules preference is not set, try to find .gyp files to turn it on.
shouldRebuildNativeModules="0";
def gyp_files_tree = fileTree(
dir: "${project.projectDir}/assets/www/nodejs-project/",
dir: "${projectWWW}/nodejs-project/",
include: "**/*.gyp"
);
gyp_files_tree.visit { gypFile ->
Expand Down Expand Up @@ -166,7 +176,7 @@ if ("1".equals(shouldRebuildNativeModules)) {
task "CopyNodeProjectAssets${abi_name}" {
description = "Copying node assets to build native modules for ${abi_name}."
inputs.files fileTree (
dir: "${project.projectDir}/assets/www/nodejs-project/"
dir: "${projectWWW}/nodejs-project/"
).exclude({
details -> // We shouldn't need to rebuild native code if there are only changes in the Node.js project javascript files.
!details.isDirectory() &&
Expand All @@ -177,7 +187,7 @@ if ("1".equals(shouldRebuildNativeModules)) {
doLast {
delete "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/"
copy {
from "${project.projectDir}/assets/www/nodejs-project/"
from "${projectWWW}/nodejs-project/"
into "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
// Symlinks to binaries are resolved by cordova prepare and Gradle during the copy, causing build time errors.
// The original project's .bin folder will be added to the path while building in the BuildNpmModules tasks.
Expand All @@ -186,7 +196,7 @@ if ("1".equals(shouldRebuildNativeModules)) {
if (OperatingSystem.current().isMacOsX()) {
// Copy the helper script for calling npm when building in Android Studio on macOS.
copy {
from "${project.projectDir}/assets/www/build-native-modules-MacOS-helper-script.sh"
from "${projectWWW}/build-native-modules-MacOS-helper-script.sh"
into "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/"
}
}
Expand All @@ -211,7 +221,7 @@ if ("1".equals(shouldRebuildNativeModules)) {
commandLine npmCommandName, '--verbose', 'rebuild', '--build-from-source'
//environment ('PATH', "${npm_toolchain_add_to_path}" + System.getProperty("path.separator") + "${System.env.PATH}")
environment ('npm_config_node_engine', 'v8' )
environment ('npm_config_nodedir', "${project.projectDir}/src/com/janeasystems/cdvnodejsmobile/jni/libnode/" )
environment ('npm_config_nodedir', "${project.projectDir}/libs/cdvnodejsmobile/libnode/" )
String npm_gyp_path_to_use;
if( file("${rootProject.projectDir}/../../plugins/nodejs-mobile-cordova/node_modules/nodejs-mobile-gyp/bin/node-gyp.js").exists() ) {
npm_gyp_path_to_use = "${rootProject.projectDir}/../../plugins/nodejs-mobile-cordova/node_modules/nodejs-mobile-gyp/bin/node-gyp.js";
Expand Down Expand Up @@ -279,12 +289,12 @@ if ("1".equals(shouldRebuildNativeModules)) {
}
}
cdvPluginPostBuildExtras += { ->
tasks.getByPath(":preBuild").dependsOn "GenerateNodeNativeAssetsLists${abi_name}";
tasks.getByPath(":${(project.getParent()==null)?"":(project.name+":")}preBuild").dependsOn "GenerateNodeNativeAssetsLists${abi_name}";
project.android.sourceSets.main.assets.srcDirs+="${rootProject.buildDir}/nodejs-native-assets/";
};
}
}

cdvPluginPostBuildExtras += { ->
android.sourceSets.main.jniLibs.srcDirs += 'src/com/janeasystems/cdvnodejsmobile/jni/libnode/bin/';
android.sourceSets.main.jniLibs.srcDirs += 'libs/cdvnodejsmobile/libnode/bin/';
};

0 comments on commit ce81228

Please sign in to comment.