27 November 2013

 

Compiling PhoneGap 3.1 hello-world to Android

Installing PhoneGap 3.1 was really a breeze - just install NodeJS then npm install -g phonegap.

Getting my first PhoneGap project just to compile, on the other hand, has been a sheer headache. For other adventurers there are quite a few incongruities to beware:

Firstly, you probably won't have setup your PATH and aligned the planets correctly for the local Android SDK to be used, so a simple phonegap run android tries to use the Adobe PhoneGap Build web service for building your app. But you need to have a Build account, for which you need an AdobeID or a GitHub account. Oh, but PhoneGap won't support GitHub accounts, so you need an AdobeID. And a paid Build account if you want to build more than 1 app.

So PhoneGap Build isn't what I really wanted. I wanted to build and test locally. For free.

It turns out you can force a local build with phonegap local build android which results in

[phonegap] adding the Android platform...
   [error] The command `android` failed. Make sure you have the latest Android SDK installed, and the `android` command (inside the tools/ folder) added to your path. Output: 'android' is not recognized as an internal or external command, operable program or batch file.

Checking back with the (out-of-date) PhoneGap/Android doco, discover I need adt-bundle/sdk/platform-tools and adt-bundle/sdk/tools in my PATH.

Done.

[phonegap] adding the Android platform...
   [error] Please install Android target 17 (the Android 4.2 SDK). Make sure you have the latest Android tools installed as well. Run `android` from your command-line to install/update any missing SDKs or tools.

Oh. Well the latest ADT (20131030) I just installed has API 19 (4.4), and in fact I would prefer to target API 14 (4.0) so installed that too. Other leads suggested setting up the project with an android target: phonegap local install android

[phonegap] trying to install app onto device
[phonegap] no device was found
[phonegap] trying to install app onto emulator
   [error] No platforms added to this project. Please use `cordova platform add <platform>`.

"cordova ..."? I'm using PhoneGap! Ok, in hindsight I should have paid attention to the first line that said "trying to install app onto device" and realised that was the completely wrong command, but instead I followed the error message's advice, intelligently replacing "cordova" with "phonegap", summoning phonegap platform add android, which threw me waaay off track with this:

[error] 'platform add android' is not a node path\to\npm\node_modules\phonegap\bin\phonegap.js command. See 'node path\to\npm\node_modules\phonegap\bin\phonegap.js help'

How does one target a different API? More leads suggested I edit AndroidManifest.xml under platforms\android... a folder and file that did not exist. How to create them? phonegap build android of course! Aaaaargh!

At this point I just bit the bullet and installed API 17. Cool, so you don't even need "local" in your command now, a simple phonegap build android will suffice.

[phonegap] detecting Android SDK environment...
[phonegap] using the local environment
[phonegap] adding the Android platform...
   [error] An error occured during creation of android sub-project. ERROR : executing command 'ant', make sure you have ant installed and added to your path.

...unless ant is not installed. But it's included with ADT, so just add path\to\adt\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\bin to your PATH then you're done.

[phonegap] adding the Android platform...
   [error] An error occured during creation of android sub-project. Creating Cordova project for the Android platform: Command failed to execute : ant jar

Well, not done-done. You (possibly) need Android SDK Build Tools v17 (via the Android SDK Manager) which is of course not installed by default in the latest ADT.

And you need to have the JDK in your path, not the JRE, or at least have the JDK earlier in the path order than JRE.

And you can't have any spaces in the path to your PhoneGap project folder.

And finally after all this, you can phonegap run android to compile your app and launch it in the Android emulator, hooray!

As it turns out, the AndroidManifest.xml reveals that minSdkVersion=10, so my attempt to use API 14 was not necessary. So the actual steps to getting PhoneGap to locally compile to Android are:


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?