Phonegap Android development environment for Windows

Assuming you want to develop HTML5 apps to run on mobile devices using Phonegap/Cordova then the easiest place to start is Android if you’re a Windows user as you don’t need a separate Mac, you don’t even need a device to test on as there’s an emulator.

Anyway, I’ve had to do this three times on various machines now, so here’s a step by step guide for next time!

Android Developer Tools

Extract the tools somewhere, the zip should contain 2 folders, the SDK and Eclipse. Using Eclipse is optional (I don’t) but the `SDK manager.exe` will be needed at some point. It allows you to manage the versions of Android you have available for testing via the emulator and also allows you configure various emulators.

For the sake of this document we just need to ensure that we have the USB driver installed so fire up the SDK Manager and find the correct entry. Install this and any updates to the default installed Android APIs.

Untitled picture

Java & Ant

Install the JDK (not just the JRE)

Note where it installed it, for me this was `C:Program FilesJavajdk1.8.0_05`

Install ANT

I extracted it to the same top level Java folder, so my `C:Program FilesJava` folder looks like:

Untitled picture

Paths

You need to set some paths so you and Cordova can access the tools:

Untitled pictur1e
Untitled pict2ure

Also edit, the path to add this:
Untitled pictu3re

Keep that last windows open, for Cordova we need to ensure that the android dev tools are also in the path, for me I added:
Untitled pic4ture

Restart your cmd/powershell and type ant, you should get a complaint that build.xml does not exist.

Also try running `adb devices` you should get sensible output.

Install GIT

I already had this, I suggest you make sure git is in your path before you continue further, some of the install commands below use git cloning.

Install nodejs

Select defaults (one is to add it to the path)

Running `npm` in powershell you should see:

Untitled pictur5e

Install Cordova

You could install phonegap and this all will still probably work, but as they’ve now branched from each other and may change in time I fancy sticking with the explicit cordova tree for now. All the docs mention cordova and phonegap interchangeably anyway.

`npm install -g cordova`

Install Ionic

Ionic is a JS and CSS framework that utilises AngularJS. As a package it gives you an MVC framework and, most importantly for me, an out of the box great UI to start you off.

`npm install -g ionic`

Hello World

Let’s build a demo app and run it on a device so we know we have a full end-to-end setup working.

If you don’t have an Android device use the SDK manager to ‘manage AVDs‘ and create a virtual device. If you’re using the emulator on Intel then I stongly suggest that you read this and make sure you install HAXM installer using the SDK manager (see above).

Firstly enable USB debugging on the device, I’m using my Moto G, then run `adb devices` on the CLI. If nothing shows up try seeing if there are any drivers for your phone, I had to install these.

Once installed I unplugged and plugged back in my phone and on the phone was asked to trust the computer. A re-run of `adb devices` now shows:

Untitled p6icture

Sweet.

Now let’s show a vanilla app on the phone

Type:

`ionic start myApp tabs
cd myApp
ionic platform add android
ionic run android`

This last command will build the app then automatically copy it over onto your device.

Untitled pic7ture

Ongoing dev

Now we know we have a working end-to-end system we can focus on the app, not messing around with the build environment. Your options are to develop against the emulator or keep copying onto the test device whenever you make changes but this does introduce a bit of a lag so the obvious option, particularly when just doing UI layout and styling, is to use your web browser

On the CLI navigate to your project’s root (myApp in this demo) and change to the www folder

Then run this:
`python -m SimpleHTTPServer 8000`

Untitled pictu8re

If you don’t have Python installed and in your path you might want to go install and set it up first.

When you see the Serving …. Line, open your browser and navigate to localhost:8000

Untitled pictu9re

Job done. Code away.

Next on the list is iOS then WP8, we’ll leave those for another day.


Comments

One response to “Phonegap Android development environment for Windows”

  1. preeti sharma Avatar
    preeti sharma

    Hi Darren,
    I followed the step but I am getting the following error
    C:UsersPreetiworkspaceMyApp>phonegap local build android
    [warning] The command `phonegap local ` has been DEPRECATED.
    [warning] The command has been delegated to `phonegap `.
    [warning] The command `phonegap local ` will soon be removed.
    [phonegap] executing ‘cordova build android’…
    cp: no such file or directory: C:UsersPreetiworkspaceMyAppicon.png

    Running command: cmd “/s /c “C:UsersPreetiworkspaceMyAppplatformsandroidcordovabuild.bat””

    [Error: Failed to run “java -version”, make sure that you have a JDK installed.
    You can get it from: http://www.oracle.com/technetwork/java/javase/downloads.
    Your JAVA_HOME is invalid: C:Program FilesJavajdk1.7.0_79
    ]

    ERROR building one of the platforms: Error: cmd: Command failed with exit code 2
    You may not have the required environment or OS to build this project

    Error: cmd: Command failed with exit code 2
    at ChildProcess.whenDone (C:UsersPreetiAppDataRoamingnpmnode_modulesphonegapnode_modulescordovanode_modulescordova-libsrccordovasuperspawn.js:139:23)
    at ChildProcess.emit (events.js:110:17)
    at maybeClose (child_process.js:1015:16)
    at Process.ChildProcess._handle.onexit (child_process.js:1087:5)

    The path variables are as follows
    ANT_HOME = C:apache-ant-1.9.6
    JAVA_HOME = C:Program FilesJavajdk1.7.0_79

    PATH = C:Program FilesJavajdk1.7.0_79bin;C:apache-ant-1.9.6bin;C:UsersPreetiAppDataLocalAndroidsdktools;

    Can you please advise what I have missed?

Leave a Reply

Your email address will not be published. Required fields are marked *