Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodejs-mobile-cordova #11

Open
wants to merge 10 commits into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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';
const FILE_LIST_PATH = 'platforms/android/app/src/main/assets/file.list';
const DIR_LIST_PATH = 'platforms/android/app/src/main/assets/dir.list';

var fileList = [];
var dirList = [];
Expand Down Expand Up @@ -30,7 +30,7 @@ function enumFolder(folderPath) {
function createFileAndFolderLists(callback) {
enumFolder(NODEJS_PROJECT_ROOT);
try {
fs.writeFileSync(FILE_LIST_PATH, fileList.join('\n'));
fs.writeFileSync(FILE_LIST_PATH, fileList.join('\n'));
fs.writeFileSync(DIR_LIST_PATH, dirList.join('\n'));
} catch (err) {
console.log(err);
Expand Down
12 changes: 6 additions & 6 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@
<uses-sdk android:minSdkVersion="21" />
</edit-config>

<source-file src="src/android/java/com/janeasystems/cdvnodejsmobile/NodeJS.java" target-dir="src/com/janeasystems/cdvnodejsmobile/" />
<source-file src="src/android/java/com/janeasystems/cdvnodejsmobile/NodeJS.java" target-dir="app/src/main/java/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="app/src/main/java/com/janeasystems/cdvnodejsmobile/jni/" />
<source-file src="src/common/cordova-bridge/cordova-bridge.cpp" target-dir="app/src/main/java/com/janeasystems/cdvnodejsmobile/jni/" />
<source-file src="src/android/jni/native-lib.cpp" target-dir="app/src/main/java/com/janeasystems/cdvnodejsmobile/jni/" />

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

<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="app/src/main/java/com/janeasystems/cdvnodejsmobile/jni/"/>

</platform>

Expand Down
2 changes: 1 addition & 1 deletion src/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ target_link_libraries( # Specifies the target library.

# Links the target library to the log library
# included in the NDK.
${log-lib} )
${log-lib} )
82 changes: 37 additions & 45 deletions src/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

buildscript {
repositories {
jcenter()
Expand Down Expand Up @@ -26,7 +25,7 @@ android {

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

Expand All @@ -51,7 +50,7 @@ String shouldRebuildNativeModules = System.getenv('NODEJS_MOBILE_BUILD_NATIVE_MO
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("${project.projectDir}/src/main/assets/www/NODEJS_MOBILE_BUILD_NATIVE_MODULES_VALUE.txt");
if (nativeModulesPreferenceFile.exists()) {
shouldRebuildNativeModules=nativeModulesPreferenceFile.text;
}
Expand All @@ -61,7 +60,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: "${project.projectDir}/src/main/assets/www/nodejs-project/",
include: "**/*.gyp"
);
gyp_files_tree.visit { gypFile ->
Expand All @@ -74,7 +73,7 @@ if (shouldRebuildNativeModules==null) {
}

if ("1".equals(shouldRebuildNativeModules)) {

logger.error("**** E1 ****");
String npmCommandName = 'npm';
if (OperatingSystem.current().isMacOsX()) {
// On macOS, npm's location may not be in the PATH environment variable if gradle is being run
Expand Down Expand Up @@ -147,8 +146,8 @@ if ("1".equals(shouldRebuildNativeModules)) {
}

String ndk_bundle_path = android.ndkDirectory
String standalone_toolchain = "${rootProject.buildDir}/standalone-toolchains/${temp_toolchain_name}"
String npm_toolchain_add_to_path = "${rootProject.buildDir}/bin"
String standalone_toolchain = "${project.projectDir}/build/standalone-toolchains/${temp_toolchain_name}"
String npm_toolchain_add_to_path = "${project.projectDir}/build/bin"
String npm_toolchain_ar = "${standalone_toolchain}/bin/${temp_suffix}-ar"
String npm_toolchain_cc = "${standalone_toolchain}/bin/${temp_suffix}-clang"
String npm_toolchain_cxx = "${standalone_toolchain}/bin/${temp_suffix}-clang++"
Expand All @@ -165,35 +164,28 @@ if ("1".equals(shouldRebuildNativeModules)) {
throw new GradleException("Unsupported opperating system for nodejs-mobile native builds: ${OperatingSystem.current().getName()}")
}

task "CopyNodeProjectAssets${abi_name}" {
task "CopyNodeProjectAssets${abi_name}" (type: Copy){
description = "Copying node assets to build native modules for ${abi_name}."
inputs.files fileTree (
dir: "${project.projectDir}/assets/www/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() &&
details.getPath().endsWith('.js') &&
!details.getPath().startsWith('node_modules/')
})
outputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp"
doLast {
delete "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/"
copy {
from "${project.projectDir}/assets/www/nodejs-project/"
into "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"

delete "${project.projectDir}/build/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/"
//copy {
from "${project.projectDir}/src/main/assets/www/nodejs-project/"
into "${project.projectDir}/build/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.
exclude "**/.bin"
}
//}
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"
into "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/"
from "${project.projectDir}/src/main/assets/www/build-native-modules-MacOS-helper-script.sh"
into "${project.projectDir}/build/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/"
}
}
new File("${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp").text = "${new Date().format('yyyy-MM-dd HH:mm:ss')}"
}
outputs.file "${project.projectDir}/build/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp"

new File("copy.timestamp").text = "${new Date().format('yyyy-MM-dd HH:mm:ss')}"
//}
}

task "MakeToolchain${abi_name}" (type:Exec) {
Expand All @@ -206,14 +198,14 @@ if ("1".equals(shouldRebuildNativeModules)) {
task "BuildNpmModules${abi_name}" (type:Exec) {
dependsOn "MakeToolchain${abi_name}"
dependsOn "CopyNodeProjectAssets${abi_name}"
inputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp"
outputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
inputs.file "${project.projectDir}/build/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp"
outputs.file "${project.projectDir}/build/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
description = "Building native modules for ${abi_name}."
workingDir "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
workingDir "${project.projectDir}/build/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
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}/src/main/src/com/janeasystems/cdvnodejsmobile/jni/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 @@ -244,49 +236,49 @@ if ("1".equals(shouldRebuildNativeModules)) {
task "CopyBuiltNpmAssets${abi_name}" (type:Sync) {
dependsOn "BuildNpmModules${abi_name}"
description = "Copying node assets with build native modules for ${abi_name}."
from "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
into "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/"
from "${project.projectDir}/build/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
into "${project.projectDir}/build/nodejs-native-assets/nodejs-native-assets-${abi_name}/"
includeEmptyDirs = false
include '**/*.node'
}

task "GenerateNodeNativeAssetsLists${abi_name}" {
dependsOn "CopyBuiltNpmAssets${abi_name}"
description "Generates a list for runtime copying"
inputs.file "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/"
outputs.file "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list"
outputs.file "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list"
inputs.file "${project.projectDir}/build/nodejs-native-assets/nodejs-native-assets-${abi_name}/"
outputs.file "${project.projectDir}/build/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list"
outputs.file "${project.projectDir}/build/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list"
doLast{
if(!(new File("${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/")).exists()) {
if(!(new File("${project.projectDir}/build/nodejs-native-assets/nodejs-native-assets-${abi_name}/")).exists()) {
// If the native assets folder doesn't exist from the copy task, skip the creation of the file.list
return;
}
delete "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list"
delete "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list"
delete "${project.projectDir}/build/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list"
delete "${project.projectDir}/build/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list"
String file_list = "";
String dir_list = "";

def assets_tree = fileTree(dir: "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/" )
def assets_tree = fileTree(dir: "${project.projectDir}/build/nodejs-native-assets/nodejs-native-assets-${abi_name}/" )
assets_tree.visit { assetFile ->
if (assetFile.isDirectory()) {
dir_list+="${assetFile.relativePath}\n"
} else {
file_list+="${assetFile.relativePath}\n"
}
}
def file_list_path = new File( "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list")
def file_list_path = new File( "${project.projectDir}/build/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list")
file_list_path.write file_list
def dir_list_path = new File( "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list")
def dir_list_path = new File( "${project.projectDir}/build/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list")
dir_list_path.write dir_list
}
}
cdvPluginPostBuildExtras += { ->
tasks.getByPath(":preBuild").dependsOn "GenerateNodeNativeAssetsLists${abi_name}";
project.android.sourceSets.main.assets.srcDirs+="${rootProject.buildDir}/nodejs-native-assets/";
tasks.getByPath(":app:preBuild").dependsOn "GenerateNodeNativeAssetsLists${abi_name}";
project.android.sourceSets.main.assets.srcDirs+="${project.projectDir}/build/nodejs-native-assets/";
};
}
}

cdvPluginPostBuildExtras += { ->
android.sourceSets.main.jniLibs.srcDirs += 'src/com/janeasystems/cdvnodejsmobile/jni/libnode/bin/';
android.sourceSets.main.jniLibs.srcDirs += '/src/main/src/com/janeasystems/cdvnodejsmobile/jni/libnode/bin/';
};
20 changes: 18 additions & 2 deletions src/android/java/com/janeasystems/cdvnodejsmobile/NodeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,26 @@ public class NodeJS extends CordovaPlugin {
private static boolean nodeIsReadyForAppEvents = false;

static {
System.loadLibrary("nodejs-mobile-cordova-native-lib");
System.out.println("NODEJS L:69");
String libPath = System.getProperty("java.library.path");
System.out.println("java.library.path=" + libPath);
try {
System.loadLibrary("nodejs-mobile-cordova-native-lib");
}catch (RuntimeException e) {
System.out.print("RuntimeException: ");
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.print("Exception: ");
System.out.println(e.getMessage());
}
finally {
System.out.println("try-block entered.");
}
System.loadLibrary("node");
System.out.println("NODEJS L72");
}


public native Integer startNodeWithArguments(String[] arguments, String nodePath, boolean redirectOutputToLogcat);
public native void sendMessageToNodeChannel(String channelName, String msg);
public native void registerNodeDataDirPath(String dataDir);
Expand Down Expand Up @@ -550,4 +566,4 @@ private static boolean getOptionRedirectOutputToLogcat(final JSONObject startOpt
}
return result;
}
}
}