How to setup Cordova on Ubuntu for Android app development

Follow the belows steps to configure cordova with Android SDK on Ubuntu machine:

Install Java & ANT:

First, check you’ve got Java and Ant installed – you’ll need those later:

sudo apt-get install default-jre
sudo apt-get install default-jdk
sudo apt-get install ant

Install Nodejs:

Run the following command to install NodeJS:

sudo apt-get install nodejs

If that doesn’t work, you can install NodeJS through a PPA repository:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

Okay, let’s check to make sure NodeJS is now installed:

nodejs -v

You should get a result like this:

v0.10.25

Install NPM:

To install NPM, simply run the following command:

sudo apt-get install npm

To make sure NPM is installed, run:

npm -v

The result should look like:

1.3.10

Install Git:

Cordova uses Git in the background to download assets when creating a new project. I have created a Cordova project without Git installed and assets were still downloaded, but let’s be safe. Execute the following command:

sudo apt-get install git

Install Apache Cordova:

We will use Node Packaged Modules (NPM) to install Apache Cordova.

sudo npm install -g cordova

That’s it! Let’s make sure it’s installed and to check the version:

cordova -v

Which should give a result like this:

3.5.0-0.2.6

After installing the cordova utility, you can always update it to the latest version by running the following command:

sudo npm update -g cordova

Use this syntax to install a specific version:

sudo npm install -g cordova@2.8.0

Run the info command for a listing that includes the current version along with other available version numbers:

npm info cordova

Install Android SDK:

Download the Android sdk from http://developer.android.com/sdk/index.html, and unpack it somewhere (I had problems with file permissions when I put it under /opt, so I ended up putting it under my home directory, as ~/android):

mv ~/Downloads/adt-bundle-linux-x86.zip ~/android
cd ~/android
unzip adt-bundle-linux-x86.zip

Update PATH in ~/.bashrc to include the phonegap tools:

# Add android sdk
PATH=$PATH:~/android/adt-bundle-linux-x86/sdk/platform-tools:~/android/adt-bundle-linux-x86/sdk/tools

and then ‘source ~/.bashrc’ to make sure you’ve got the new path setup.

Creating Your First Cordova Project:

Now let’s take a moment to quickly go over creating your first Apache Cordova app project.

To create your app, use the following command:

cordova create

To add platforms, we will need to switch to the app’s folder:

cd

Where is the folder where the source is held.

Now that we are in the folder, we can add platforms like so:

cordova platform add <platform-name>

cordova platform add android

Where is the platform you want to add. Platforms include: ios, amazon-fireos, android, blackberry10, firefoxos, wp8, and windows8. For wp8 and windows8, you should be on a Windows machine.

Run this to check your current set of platforms:

cordova platforms ls

Run either of the following synonymous commands to remove a platform:

cordova platform remove <platform-name>

Once you are ready to build your project, make sure you are in the project folder and run the following:

cordova build

To build the project for specific platforms, use the following example command:

cordova build <platform-name>

cordova build android

Where is the platform you want to build. Platforms include: ios, amazon-fireos, android, blackberry10, firefoxos, wp8, and windows8. For wp8 and windows8, you should be on a Windows machine.

The cordova build command is a shorthand for the following:

cordova prepare <platform-name>

cordova compile <platform-name>

Test the App on an Android Emulator or Device:

Run a command such as the following to rebuild the app and view it within a specific platform’s emulator:

cordova emulate android

Some mobile platforms emulate a particular device by default, such as the android projects. For other platforms, you may need to first associate a device with an emulator.

Happy blogging and please drop your comments and queries to improvise the details in the post. Thanks for reading my post.

Permanent link to this article: https://blog.openshell.in/2014/12/how-to-setup-cordova-on-ubuntu-for-android-app-development/