JaneaSystems / nodejs-mobile-samples

Repository for demo applications that use Node.js on Mobile
MIT License
172 stars 110 forks source link

Cordova build to Android (clean) #3

Open subversivo58 opened 6 years ago

subversivo58 commented 6 years ago

Hi @orangemocha,

I tried to use cordova-angularjs as an example to do a build for Android abstracting the use of AngularJS because I only wanted to follow the Node execution in addition to that I do not have a Mac to test ... I confess that the attempt was flawed (I believe that for my lack of knowledge of the process).

Is there any limitation to the Windows build or idea of a basic example for Android?

I have an app I use on build.phonegap.com, how could I integrate this plugin to the application source?

I was surprised to see the project and enthusiastic about the possibilities of using Node. Congratulations on the project.

jaimecbernardo commented 6 years ago

Thanks for your issue, @subversivo58 .

The cordova-angularjs is configured for iOS only and some of the configuration commands assume you are on a Linux-like bash.

It's possible, however, to use the sample that comes with the plugin on Windows by adapting the usage instructions of the plugin's README.md

Create your app:

cordova create MyApp
cd MyApp
cordova platform add android
cordova plugin add nodejs-mobile-cordova

Copy the sample project files that are included in the plugin, you will be asked to overwrite the default ./www/js/index.js file:

copy plugins\nodejs-mobile-cordova\install\sample-project\www\js\index.js www\js\index.js
copy plugins\nodejs-mobile-cordova\install\sample-project\www\nodejs-project\main.js www\nodejs-project\main.js
copy plugins\nodejs-mobile-cordova\install\sample-project\www\nodejs-project\package.json www\nodejs-project\package.json

Next, you could open MyApp\platforms\android in Android Studio to have it configure the ANDROID_NDK_HOME for the project and see if the Event log has any Gradle messages of dependencies you need to install. Check this issue for build errors and troubleshooting for Android: https://github.com/janeasystems/nodejs-mobile/issues/10

After that, you can build and run the sample:

cordova build android
cordova run android

Check adb logcat for the messages sent between cordova and node, since there is no UI. You can use Android Studio's Android Monitor tab for that.

Regarding using build.phonegap.com , we're not familiar with the platform, but there's no reason why it shouldn't build. We encourage you to give it a try. Let us know how it goes.

I hope this was helpful. Thank you for the feedback, as well.

subversivo58 commented 6 years ago

Sorry for the delay in returning the presented help.

I'm not familiar with native development or using Android Studio.

I am using the build.phonegap.com tool because this tool abstracts the build process however of course I know the result is a hybrid application and that its performance is inferior to native applications.

Using this tool I can only worry about the following structure:

// root folder (aka www)
      |-- /css
      |-- /img
      |-- /js
      |-- /res
      |-- index.html
      |-- config.xml

Where the config.xml file is indeed "where things happen"...

To add plugins using this tool is something as simple as:

<!-- example use native dialog UI elements -->
<plugin name="cordova-plugin-dialogs"           source="npm" spec="~1.3.4" />

Use as (in /js/index.js):

// after Cordova is fully loaded.
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    console.log(navigator.notification);
}

As several Cordova plugins are supported by this tool I am confused at this point, I know this is not an error in the plugin but rather incompatibility with a specific tool.

Is it possible to get this "compatibility" by doing some adjustment or just "creating a new plugin"? I'm looking at the documentation of Cordova plugins but that far outweighs my knowledge.

Although it is a relatively new tool, it is an official PhoneGap tool and contributes greatly to the development of applications.

I wonder if this can be overcome or if I should open a new issue asking for this "compatibility" (feature)?

jaimecbernardo commented 6 years ago

Hi @subversivo58 , What error are you getting when trying to add nodejs-mobile-cordova to build.phonegap.com ?

The plugin is published in npm as well.

subversivo58 commented 6 years ago

Basically the error informs that the NDK has not been configured.

However build.phonegap.com says: you do not need the SDK configuration.

Managed Compilation and Signing (No SDKs)

Local development involves installing and maintaining multiple native SDKs as well as the Cordova/PhoneGap SDK. PhoneGap Build takes this headache away! Get app-store ready apps without the headache of maintaining native SDKs. PhoneGap Build will always build against the required SDK for the platform you are targeting.

Yes the reference I used describes the origin (source) as npm and the version (spec).

<plugin name="nodejs-mobile-cordova" source="npm" spec="~0.1.1" />

I have tried to test with a basic model to see if the error could have been caused by some specific feature of my application in the settings but the result (error) was identical, I did not save this log but I will perform the same procedure again to display the log error here in a later comment.

subversivo58 commented 6 years ago

When using:

<plugin name="nodejs-mobile-cordova" source="npm" spec="~0.1.1" />

The build fails and I get the following error:

Build Date: 2017-11-13 11:21:48 +0000
--------------------------------------------------------------------------------
PLUGIN OUTPUT
--------------------------------------------------------------------------------
Fetching plugin "cordova-plugin-battery-status@~1.2.4" via npm
Installing "cordova-plugin-battery-status" at "1.2.5" for android
Fetching plugin "cordova-plugin-camera@~2.4.1" via npm
Installing "cordova-plugin-camera" at "2.4.1" for android
Fetching plugin "cordova-plugin-compat@^1.1.0" via npm
Installing "cordova-plugin-compat" at "1.2.0" for android
Subproject Path: CordovaLib
Fetching plugin "cordova-plugin-media-capture@~1.4.3" via npm
Installing "cordova-plugin-media-capture" at "1.4.3" for android
Fetching plugin "cordova-plugin-file@^4.0.0" via npm
Installing "cordova-plugin-file" at "4.3.3" for android
Plugin dependency "cordova-plugin-compat@1.2.0" already fetched, using that version.
Dependent plugin "cordova-plugin-compat" already installed on android.

The Android Persistent storage location now defaults to "Internal". Please check this plugin's README to see if your application needs any changes in its config.xml.

If this is a new application no changes are required.

If this is an update to an existing application that did not specify an "AndroidPersistentFileLocation" you may need to add:

      "<preference name="AndroidPersistentFileLocation" value="Compatibility" />"

to config.xml in order for the application to find previously stored files.

Plugin dependency "cordova-plugin-compat@1.2.0" already fetched, using that version.
Dependent plugin "cordova-plugin-compat" already installed on android.
Fetching plugin "cordova-plugin-device@~1.1.6" via npm
Installing "cordova-plugin-device" at "1.1.7" for android
Fetching plugin "cordova-plugin-dialogs@~1.3.3" via npm
Installing "cordova-plugin-dialogs" at "1.3.4" for android
Plugin "cordova-plugin-file" already installed on android. Making it top-level.
Fetching plugin "cordova-plugin-geolocation@~2.4.3" via npm
Installing "cordova-plugin-geolocation" at "2.4.3" for android
Plugin dependency "cordova-plugin-compat@1.2.0" already fetched, using that version.
Dependent plugin "cordova-plugin-compat" already installed on android.
Fetching plugin "cordova-plugin-globalization@~1.0.7" via npm
Installing "cordova-plugin-globalization" at "1.0.8" for android
Fetching plugin "cordova-plugin-inappbrowser@~1.7.1" via npm
Installing "cordova-plugin-inappbrowser" at "1.7.2" for android
Fetching plugin "cordova-plugin-network-information@~1.3.3" via npm
Installing "cordova-plugin-network-information" at "1.3.4" for android
Fetching plugin "cordova-plugin-vibration@~2.1.5" via npm
Installing "cordova-plugin-vibration" at "2.1.6" for android
Fetching plugin "nodejs-mobile-cordova@~0.1.1" via npm
Installing "nodejs-mobile-cordova" at "0.1.0" for android
Subproject Path: CordovaLib
Installing "com.phonegap.plugins.hydration" at "6.6.0" for android
Plugin dependency "cordova-plugin-network-information@1.3.4" already fetched, using that version.
Dependent plugin "cordova-plugin-network-information" already installed on android.
Fetching plugin "cordova-plugin-whitelist" via npm
Installing "cordova-plugin-whitelist" at "1.3.3" for android

               This plugin is only applicable for versions of cordova-android greater than 4.0. If you have a previous platform version, you do *not* need this plugin since the whitelist will be built in.

Plugin dependency "cordova-plugin-file@4.3.3" already fetched, using that version.
Dependent plugin "cordova-plugin-file" already installed on android.
Fetching plugin "phonegap-plugin-contentsync" via npm
Installing "phonegap-plugin-contentsync" at "1.4.1" for android
--------------------------------------------------------------------------------
COMPILE OUTPUT
--------------------------------------------------------------------------------
Subproject Path: CordovaLib
Configuration on demand is an incubating feature.
Incremental java compilation is an incubating feature.

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'project'.
> NDK not configured. 
  Download it with SDK manager.)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 1.406 secs
Error: /project/gradlew: Command failed with exit code 1 Error output:
FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'project'.
> NDK not configured. 
  Download it with SDK manager.)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
    at ChildProcess.whenDone (/project/cordova/node_modules/cordova-common/src/superspawn.js:169:23)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:877:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)

When removing the plugin reference and trying to build again ... the build succeeds:

Build Date: 2017-11-13 11:24:48 +0000
--------------------------------------------------------------------------------
PLUGIN OUTPUT
--------------------------------------------------------------------------------
Fetching plugin "cordova-plugin-battery-status@~1.2.4" via npm
Installing "cordova-plugin-battery-status" at "1.2.5" for android
Fetching plugin "cordova-plugin-camera@~2.4.1" via npm
Installing "cordova-plugin-camera" at "2.4.1" for android
Fetching plugin "cordova-plugin-compat@^1.1.0" via npm
Installing "cordova-plugin-compat" at "1.2.0" for android
Subproject Path: CordovaLib
Fetching plugin "cordova-plugin-media-capture@~1.4.3" via npm
Installing "cordova-plugin-media-capture" at "1.4.3" for android
Fetching plugin "cordova-plugin-file@^4.0.0" via npm
Installing "cordova-plugin-file" at "4.3.3" for android
Plugin dependency "cordova-plugin-compat@1.2.0" already fetched, using that version.
Dependent plugin "cordova-plugin-compat" already installed on android.

The Android Persistent storage location now defaults to "Internal". Please check this plugin's README to see if your application needs any changes in its config.xml.

If this is a new application no changes are required.

If this is an update to an existing application that did not specify an "AndroidPersistentFileLocation" you may need to add:

      "<preference name="AndroidPersistentFileLocation" value="Compatibility" />"

to config.xml in order for the application to find previously stored files.

Plugin dependency "cordova-plugin-compat@1.2.0" already fetched, using that version.
Dependent plugin "cordova-plugin-compat" already installed on android.
Fetching plugin "cordova-plugin-device@~1.1.6" via npm
Installing "cordova-plugin-device" at "1.1.7" for android
Fetching plugin "cordova-plugin-dialogs@~1.3.3" via npm
Installing "cordova-plugin-dialogs" at "1.3.4" for android
Plugin "cordova-plugin-file" already installed on android. Making it top-level.
Fetching plugin "cordova-plugin-geolocation@~2.4.3" via npm
Installing "cordova-plugin-geolocation" at "2.4.3" for android
Plugin dependency "cordova-plugin-compat@1.2.0" already fetched, using that version.
Dependent plugin "cordova-plugin-compat" already installed on android.
Fetching plugin "cordova-plugin-globalization@~1.0.7" via npm
Installing "cordova-plugin-globalization" at "1.0.8" for android
Fetching plugin "cordova-plugin-inappbrowser@~1.7.1" via npm
Installing "cordova-plugin-inappbrowser" at "1.7.2" for android
Fetching plugin "cordova-plugin-network-information@~1.3.3" via npm
Installing "cordova-plugin-network-information" at "1.3.4" for android
Fetching plugin "cordova-plugin-vibration@~2.1.5" via npm
Installing "cordova-plugin-vibration" at "2.1.6" for android
Installing "com.phonegap.plugins.hydration" at "6.6.0" for android
Plugin dependency "cordova-plugin-network-information@1.3.4" already fetched, using that version.
Dependent plugin "cordova-plugin-network-information" already installed on android.
Fetching plugin "cordova-plugin-whitelist" via npm
Installing "cordova-plugin-whitelist" at "1.3.3" for android

               This plugin is only applicable for versions of cordova-android greater than 4.0. If you have a previous platform version, you do *not* need this plugin since the whitelist will be built in.

Plugin dependency "cordova-plugin-file@4.3.3" already fetched, using that version.
Dependent plugin "cordova-plugin-file" already installed on android.
Fetching plugin "phonegap-plugin-contentsync" via npm
Installing "phonegap-plugin-contentsync" at "1.4.1" for android
--------------------------------------------------------------------------------
COMPILE OUTPUT
--------------------------------------------------------------------------------
Subproject Path: CordovaLib
Configuration on demand is an incubating feature.
Incremental java compilation is an incubating feature.
:preBuild UP-TO-DATE
:preDebugBuild UP-TO-DATE
:checkDebugManifest
:preReleaseBuild UP-TO-DATE
:CordovaLib:preBuild UP-TO-DATE
:CordovaLib:preDebugBuild UP-TO-DATE
:CordovaLib:checkDebugManifest
:CordovaLib:prepareDebugDependencies
:CordovaLib:compileDebugAidl
:CordovaLib:compileDebugNdk UP-TO-DATE
:CordovaLib:compileLint
:CordovaLib:copyDebugLint UP-TO-DATE
:CordovaLib:mergeDebugShaders
:CordovaLib:compileDebugShaders
:CordovaLib:generateDebugAssets
:CordovaLib:mergeDebugAssets
:CordovaLib:mergeDebugProguardFiles
:CordovaLib:packageDebugRenderscript UP-TO-DATE
:CordovaLib:compileDebugRenderscript
:CordovaLib:generateDebugResValues
:CordovaLib:generateDebugResources
:CordovaLib:packageDebugResources
:CordovaLib:processDebugManifest
:CordovaLib:generateDebugBuildConfig
:CordovaLib:processDebugResources
:CordovaLib:generateDebugSources
:CordovaLib:incrementalDebugJavaCompilationSafeguard
:CordovaLib:compileDebugJavaWithJavac
:CordovaLib:compileDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
:CordovaLib:processDebugJavaRes UP-TO-DATE
:CordovaLib:transformResourcesWithMergeJavaResForDebug
:CordovaLib:transformClassesAndResourcesWithSyncLibJarsForDebug
:CordovaLib:mergeDebugJniLibFolders
:CordovaLib:transformNative_libsWithMergeJniLibsForDebug
:CordovaLib:transformNative_libsWithSyncJniLibsForDebug
:CordovaLib:bundleDebug
:CordovaLib:preReleaseBuild UP-TO-DATE
:CordovaLib:checkReleaseManifest
:CordovaLib:prepareReleaseDependencies
:CordovaLib:compileReleaseAidl
:CordovaLib:compileReleaseNdk UP-TO-DATE
:CordovaLib:copyReleaseLint UP-TO-DATE
:CordovaLib:mergeReleaseShaders
:CordovaLib:compileReleaseShaders
:CordovaLib:generateReleaseAssets
:CordovaLib:mergeReleaseAssets
:CordovaLib:mergeReleaseProguardFiles
:CordovaLib:packageReleaseRenderscript UP-TO-DATE
:CordovaLib:compileReleaseRenderscript
:CordovaLib:generateReleaseResValues
:CordovaLib:generateReleaseResources
:CordovaLib:packageReleaseResources
:CordovaLib:processReleaseManifest
:CordovaLib:generateReleaseBuildConfig
:CordovaLib:processReleaseResources
:CordovaLib:generateReleaseSources
:CordovaLib:incrementalReleaseJavaCompilationSafeguard
:CordovaLib:compileReleaseJavaWithJavac
:CordovaLib:compileReleaseJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
:CordovaLib:processReleaseJavaRes UP-TO-DATE
:CordovaLib:transformResourcesWithMergeJavaResForRelease
:CordovaLib:transformClassesAndResourcesWithSyncLibJarsForRelease
:CordovaLib:mergeReleaseJniLibFolders
:CordovaLib:transformNative_libsWithMergeJniLibsForRelease
:CordovaLib:transformNative_libsWithSyncJniLibsForRelease
:CordovaLib:bundleRelease
:prepareComAndroidSupportSupportV42411Library
:prepareProjectCordovaLibUnspecifiedDebugLibrary
:prepareDebugDependencies
:compileDebugAidl
:compileDebugRenderscript
:generateDebugBuildConfig
:generateDebugResValues
:generateDebugResources
:mergeDebugResources
:processDebugManifest
:processDebugResourcesWarning: AndroidManifest.xml already defines debuggable (in http://schemas.android.com/apk/res/android); using existing value in manifest.

:generateDebugSources
:incrementalDebugJavaCompilationSafeguard
:compileDebugJavaWithJavac
:compileDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:compileDebugNdk UP-TO-DATE
:compileDebugSources
:mergeDebugShaders
:compileDebugShaders
:generateDebugAssets
:mergeDebugAssets
:transformClassesWithDexForDebug
:mergeDebugJniLibFolders
:transformNative_libsWithMergeJniLibsForDebug
:processDebugJavaRes UP-TO-DATE
:transformResourcesWithMergeJavaResForDebug
:validateSigningDebug
:packageDebug
:assembleDebug
:cdvBuildDebug

BUILD SUCCESSFUL

Total time: 4.853 secs
Built the following apk(s): 
    /project/build/outputs/apk/project-debug.apk
jaimecbernardo commented 6 years ago

Hi @subversivo58 , thanks for the logs.

From the error message, it seems the build system is unable to find the Android Native Development Kit (NDK). The cordova plugin needs to run NDK and CMake while building, since it depends on JNI C code compiling for Android. I tried to look up if build.phonegap.com supports NDK/CMake from gradle build, but I couldn't find a mention for that.

I've tried submitting a sample application to build.phonegap.com that has a plugin depending on NDK as well and I got the same error. Application tested: https://github.com/dpa99c/cordova-plugin-hello-c-test Plugin it depends on: https://github.com/dpa99c/cordova-plugin-hello-c

So, there's the possibility that plugins that include Native Code in Android are not supported in build.phonegap.com or need some special configuration on your application so that the build environment is set correctly.

I'd advise trying to get support from the build.phonegap.com, since this is related to their tool and might be impossible to have access to NDK and CMake in their environment. In the meanwhile, it is possible to build applications for Android that use the plugin, on a Windows development environment.

I hope this was helpful. Please let us know of any further developments.

subversivo58 commented 6 years ago

Thanks @jaimecbernardo.

Officially build.phonegap.com uses a forum in Adobe, I'm looking for help there since plugins can optionally have configuration entries I'll try to know if any optional configuration refers to NDK.

I would like to leave this question open if I find relevant information in the Adobe forum however I understand if the administration of this repository understands that this issue has been overcome.

Thank you again.

jaimecbernardo commented 6 years ago

Thanks @subversivo58 . The issue will probably stay open for a while, since its information may be useful for other users. (closing just now was a misclick)