How to build Cordova mobile app (using nodejs)

Apache Cordova/PhoneGap@wiki is a platform for building native mobile applications using HTML, CSS and JavaScript.

  • Install SDKs
# debian/ubuntu, see http://askubuntu.com/questions/318246/complete-installation-guide-for-android-sdk-adt-bundle-on-ubuntu
$ sudo apt-get install nodejs ant openjdk-8-jdk (or openjdk-7-jdk)
$ cat ~/.bash_cordova
#!/bin/bash
export JAVA_HOME=/usr/lib/jvm/default-java/
# export ANT_HOME=/usr/bin
# download and unzip android-sdk from http://developer.android.com/sdk/index.html, run "Android SDK Manager" to install platforms and add to path
export ANDROID_SDK="~/android-sdk"
export PATH=$PATH:$JAVA_HOME/bin:$ANDROID_SDK/platform-tools:$ANDROID_SDK/tools

# cygwin using chocolatery
$ cinst node ant java.jdk (doesnt work due to license)
$ cat ~/.bash_cordova
#!/bin/bash
export JAVA_HOME="C:\Program Files\Java\jdk1.7.0_51"
export ANT_HOME="C:\apache-ant-1.9.3"
# download and unzip android-sdk from http://developer.android.com/sdk/index.html, run "Android SDK Manager" to install platforms and add to path
export ANDROID_SDK="$LOCALAPPDATA\Android\android-sdk"
export PATH=$PATH:`cygpath $JAVA_HOME`/bin:`cygpath $ANT_HOME`/bin:`cygpath $ANDROID_SDK`/platform-tools:`cygpath $ANDROID_SDK`/tools

from Cordova android platform guide

$ mkdir myapp && cd myapp
$ yo cordova (yes to webapp generator)
$ cd yeoman
$ cordova platform add android
$ grunt run (connect phone, enable usb debug and MTP storage)
# other commands
$ grunt build (both buildweb and cordova-build) or grunt emulate (needs avd device)
# note: from source control, keep yeoman/ only, mkdir {platforms,plugins,www}, cp yeoman/app/config.xml www/ or grunt build, add plugins and platforms
# e.g.: pre-existing web project using grunt
$ npm install -g generator-angular|generator-webapp
$ mkdir myapp
$ yo angular|webapp
$ bower install && npm install

# add npm's cordova
$ npm install cordova --save-dev
$ (add  to index.html)
# edit Gruntfile.js
var cordova = require('cordova');
var cordovaConfig = { platform: grunt.option('platform') };
yeoman: { ..., cordova: 'www'
clean: { dist: { files: [{ src: [ ...., '/*'
copy: { ..., cordova: { expand: true, cwd: '', dest: '', src: '**' }
grunt.registerTask('cordova-prepare', 'Prepare the native application', function() {
    var done = this.async();
    if (cordovaConfig.platform === null) { cordova.prepare(done); } 
    else { cordova.prepare(cordovaConfig.platform, done); }
});
grunt.registerTask('cordova-build', 'Build the native application', function() {
    var done = this.async();
    if (cordovaConfig.platform === null) { cordova.build(done); } 
    else { cordova.build(cordovaConfig.platform, done); }
});
grunt.registerTask('cordova-emulate', 'Emulate the application', function(){
    var done = this.async();
    if (cordovaConfig.platform === null) { cordova.emulate(done); } 
    else { cordova.emulate(cordovaConfig.platform, done); }
});
grunt.registerTask('cordova-run', 'Run the application on a device', function() {
    var done = this.async();
    if (cordovaConfig.platform === null) { cordova.run(done); } 
    else { cordova.run(cordovaConfig.platform, done); }
});
# build build cordova project
$ grunt build copy:cordova cordova-build

note 'cordova build' is shofthand for `cordova prepare && cordova compile`
use `grunt cordova-run` to run (assuming called cordova-build previously) on installed devices. Needs MTS (storage) and USB debug enabled
use `grunt cordova-emulate` to run or emulator (needs `android create avd --name  --target <targetID`)  

from Mobile Apps with Phonegap and Yeoman

$ cordova plugin add https://github.com/mkuklis/phonegap-websocket
$ cordova plugin add https://github.com/alongubkin/phonertc.git
$ cordova build android
$ cordova run android
Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s