Getting started with PhoneGap/Cordova with CentOS, First Helloworld android app using PhoneGap/Cordova

Install Cordova:

On Linux/CentOS:

To install Cordova you will first need to install the npm utility of  Node.js.
  1. yum install nodejs
  2. yum install npm
Then install cordova:
  1. npm install -g cordova

Install Android-SDK:
If you haven't yet installed Android SDK do the following:
Install prequisite libs:
  1. yum install glibc.i686 glibc-devel.i686 libstdc++.i686 zlib-devel.i686 ncurses-devel.i686 libX11-devel.i686 libXrender.i686 libXrandr.i686
Download android-sdk from http://developer.android.com/sdk/index.html
(For CentOS 32bit:
Unzip it to a directory.It will be a good idea to rename it.(e.g. /usr/local/android/).Run the following commands:
  1. cd /usr/local/
  2. wget http://dl.google.com/android/adt/22.6.2/adt-bundle-linux-x86-20140321.zip
  3. unzip adt-bundle-linux-x86-20140321.zip
  4. #backup can be deleted now, your choice
  5. rm adt-bundle-linux-x86-20140321.zip
  6. mv adt-bundle-linux-x86-20140321/ android/
Browse it and make sure that the following directories exist and contain all files:
/usr/local/android/sdk/platforms
/usr/local/android/sdk/platform-tools
/usr/local/android/sdk/tools
Now add path variables to environment:
  1. echo "export PATH=$PATH:/usr/local/android/sdk/platforms" >> ~/.profile
  2. echo "export PATH=$PATH:/usr/local/android/sdk/platform-tools" >> ~/.profile
  3. echo "export PATH=$PATH:/usr/local/android/sdk/tools" >> ~/.profile
Now load them for using in current session:
  1. source ~/.profile
Now install AntAnt version 1.8+ is needed for this purpose.


Uninstall previous old ant version:
  1. #uninstall if already old versions are there
  2. yum remove ant

Run the script from here to install ant 1.9:
http://mohiplanet.blogspot.com/2014/06/centos-ant-19-install-scriptsh.html

You will also need ant apache regex utility.Install it by running :
  1. yum install ant-apache-regexp

Create Helloworld Cordova Project:
cd to a convenient directory and run:
  1. cordova create hello com.example.hello HelloWorld
This should create a hello directory in current location.Navigate to hello directory where the config.xml file is located:
  1. cd hello/ && ls
You will see something like:
config.xml  hooks  merges  platforms  plugins  www
Run the following command to prepare the helloworld project for android platform:
  1. cordova platform add android
This will generate required files for helloworld project to run in android.
Run the follwing command to see what platforms are already added in the project:
  1. cordova platforms ls
You will see something like this:
Installed platforms: android 3.5.0
Available platforms: amazon-fireos, blackberry10, firefoxos, ubuntu
Build the project:
Build the project by following command:
  1. cordova build
Run the project:
Run the following command to run the app on android emulator:
  1. cordova emulate android

Now what you just see is the html page located at:
YOUR_PROJECT_ROOT_DIR/www/index.html
You can edit the file and run 'cordova emulate android' again to see the changes.
Debugging the application (Seeing logs):
You can run:
  1. adb logcat
on a seperate window to see android logs.This will produce huge logs, so I would recommend using:
  1. adb logcat|grep Cordova
This will produce logs created by only Cordova.Sample cordova logs:
 adb logcat|grep Cordova
- waiting for device -
I/CordovaLog(  958): Changing log level to DEBUG(3)
I/CordovaLog(  958): Found start page location: index.html
D/CordovaActivity(  958): CordovaActivity.onCreate()
D/CordovaWebView(  958): CordovaWebView is running on device made by: unknown
D/CordovaActivity(  958): CordovaActivity.init()
D/CordovaWebView(  958): >>> loadUrl(file:///android_asset/www/index.html)
W/System.err(  958):  at org.apache.cordova.CordovaWebView.loadUrlIntoView(CordovaWebView.java:457)
W/System.err(  958):  at org.apache.cordova.CordovaWebView.loadUrlIntoView(CordovaWebView.java:444)
W/System.err(  958):  at org.apache.cordova.CordovaWebView.loadUrl(CordovaWebView.java:414)
W/System.err(  958):  at org.apache.cordova.CordovaActivity.loadUrl(CordovaActivity.java:395)
D/CordovaWebView(  958): >>> loadUrlNow()

Distributing your app(Generating .apk):

When you run:
  1. cordova build

A distributable .apk is genarated at  YOUR_PROJECT_ROOT_DIR/platforms/android/ant-build/
In this example the auto-generated .apk will  be YOUR_PROJECT_ROOT_DIR/platforms/android/ant-build/HelloWorld-debug.apk
But this a debug version.To build release version simply do:

  1. cordova build --release

This will generate an unsigned release version of your app like YOUR_PROJECT_ROOT_DIR/platforms/android/ant-build/HelloWorld-release-unsigned.apk 
To sign your app with your company profile please visit:

http://developer.android.com/tools/publishing/app-signing.html

References:-
http://cordova.apache.org/docs/en/3.0.0/guide_cli_index.md.html
https://wiki.echocat.org/display/ECHOCAT/2012/08/17/Configure+Android+SDK+on+CentOS+6+x86_64

No comments:

Post a Comment