Converting JavaScript App Into an Android App with PhoneGap

pubnub share

So you’re here because you want to turn your JavaScript application into a mobile Android application. Luckily for you, we’ve written this tutorial just for that, using the PhoneGap framework. In addition to our PhoneGap tutorial, we’ll also walk you through a manual way using Cordova CLI with an Android SDK.

If you want to convert an iOS app, we have a tutorial for that as well.

We’ve built numerous chat demos and tutorials with JavaScript, including 10Chat, a simple chat app written with only 10 lines of JavaScript. These demos do work on mobile, but are not exclusively for building native Android applications. On that note, we’ll change that.

Android PhoneGap app

How to Convert a JavaScript Android App with PhoneGap

PhoneGap is an open source mobile framework that enables you to create cross-platform apps that run on various mobile devices including iOS and Android. You write your web app in HTML5, JavaScript and CSS, and PhoneGap helps you to turn it into native apps, most likely Android or iOS.

You can create mobile, or responsive web apps with any UI frameworks and libraries of your choice- jQuery Mobile, Sencha Touch, Enyo, Kendo UI, Onsen UI… you name it. Or, of course, VanillaJS sans any frameworks can be your choice. The sample code used for this tutorial is available on Github, and this demo has very clean interface with minimal CSS and no images, and using no extra UI libraries.

There are two ways to make your web app into an Android app using PhoneGap. One is a no-pain ultra easy way, and another way is a manual way using Cordova CLI with an Android SDK.

Option 1: Seamless Android Apps with PhoneGap Build

Using PhoneGap Build is absolutely painless. All you need to do is zip up your web app, upload it to Adobe PhoneGap Build cloud, and the service takes care everything for you. You don’t even need to download and set up SDK or emulators.

  1. Write a web app
  2. Zip it up
  3. Upload the zip file to PhoneGap Build cloud. Click Ready to Build. Edit the name of the app and upload an app icon too.
  4. When ready, scan the QR code with your Android phone, and follow the link
  5. Download the apk and install it on your phone
  6. Woo-hoo!
  7. (…Profit!)

Option 2: Full Development with PhoneGap Cordova CLI

Right, that was easy. Or, you can install Cordova command-line interface (CLI) tools and Android SDKs for full development with taking advantage of the plugins that enable for you to use hardware APIs, mobile push notifications and more, and debugging.

Step 1: Install Android Eclipse Bundle

First, you need to download and install Eclipse ADT Bundle. The bundle includes the Android SDK tools, Eclipse IDE with built-in Android Developer Tools.

Step 2: Set Up the Android SDK

Once everything you need is installed, open Eclipse and go to Window > Android SDK Manager. In the dialog, select SDK tools. You probably need:

  • Android SDK Tools
  • Android SDK Platform-tools
  • Android SDK Build-tools (API 19 to work with Cordova, as of Oct. 2014)
  • SDK Platform
  • ARM EABI v7a System Image

For more information on adding SDK packages, please refer, site.

Step 3: Set Up Android Emulator

Now, you need to create an Android Virtual Device (AVD). On Eclipse, go to Window > Android Virtual Device Manager. In the AVD Manager window, at Device Definitions tab, pick an emulator and click Create AVD….

avd manager avd manager

Step 4: Install Cordova CLI

Installation of the command line interface requires node.js. If you have not, you must install it first.

Install cordova with npm:

$ sudo npm install -g cordova

Step 5: Create a New Project in the SDK

Create a new project by typing these commands:

$ cordova create simple-chat SimpleChat
$ cd simple-chat
$ cordova platform add android

Try building it and deploying the app to an emulator, or an device:

$ cordova build
$ cordova run android

It may take a while to launch the emulator, but be patient. Once the emulator is up and running, you should see this Cordova splash screen!


Step 6: Port Your Web App

In your project folder, you should find a /www/ directory. This is where your web app goes.


Your index.html body content goes into the body of /www/index.html. Do not delete or replace head section unless you know what you are doing.

And /js/index.js includes functions that are required for PhoneGap. You just need to call your app functions in onDeviceReady().

var app = {
    initialize: function() ...    
    bindEvents: function() ...    

    onDeviceReady: function() {
        // Your awesome chat app here ---
        var channel = 'chat';
        var p = PUBNUB.init({
            subscribe_key: 'sub-c-f762fb78-2724-...',
            publish_key:   'pub-c-156a6d5f-22bd-...'


Also, you include all your css files and images under /www/.

The application icons can be anywhere, you just need to specify the location in your config.xml file.

<?xml version='1.0' encoding='utf-8'?>
<widget id="" version="0.0.1" xmlns="" xmlns:cdv="">
    <icon src="app-icon.png" />


Deploy to an emulator or device.

$ cordova run android

Now you have your own chat app for Android! Super!

Remote Debugging with Chrome

When you are working on Android apps, you can easily remote-debug on Chrome.

  1. Enable USB debugging mode on your Android device by going to Settings > Developer Options, then turn on USB debugging option.
  2. On your desktop Chrome browser, go to chrome://inspect.
  3. Connect the device and your computer with a USB cable
  4. You should see your device name (in this screenshot below, XT1032 is the name of Moto G that I am testing with), also your app on Chrome screen
  5. Click “inspect”

Once you click “inspect”,  you should see the familiar DevTools window for debugging! If you have a trouble with the tools, go to Google Chrome Developer for more info.

I hope you enjoyed this tutorial. In a week or two, we’ll be releasing the this tutorial for iOS and PhoneGap!