Winston Lawrence

Project Manager & Occasional developer

Android Development Continues

Okay, flush with the success of getting my version of "Hello World" running within the Android emulator, I'm ready to try something that ties a web page to the mobile application experience. I had noticed forum references to something called Phonegap and after a little time spent with Google I decided to try out the Phonegap framework.

If you've been following along with my Android development posts then you probably have about fifty percent of the Phonegap prerequisites already installed on your machine e.g. eclipse, jdk. If you followed along from some of my earlier posts then you also have a Ruby environment, GIT and possibly ANT installed as well. If you don't already  have these setup then you will need to install them - you will have to decide if the non-trivial exercise of installing all of these applications is worth the trouble. You can find excellent instructions for installing all of the components over on the Phonegap wiki. I am not going to duplicate the excellent information instructions found on the Wiki in this post, so just go over to the Wiki and scroll down to the Android Tutorials section for the details. If you really don't want to have to install Ruby and Git then the Wiki has instructions for using Phonegap with Eclipse and without Ruby and the Ruby droidgap script.

Once you have the requisite environment built then you need to install the Phonegap framework for android (you can also pull down frameworks for Blackberry, iPhone, Palm etc). In this case create a Phonegap directory and from within that directory issue the command:
git clone git://github.com/phonegap/phonegap-iphone.git

Installing Phonegap on Windows from GIT (GITHUB) repository

C:ApplicationsDevelopmentphonegap>
C:ApplicationsDevelopmentphonegap>git clone git://github.com/phonegap/phonegap-android.git
Initialized empty Git repository in C:/Applications/Development/phonegap/phonegap-android/.git/
remote: Counting objects: 4669, done.
remote: Compressing objects: 100% (1262/1262), done.
remote: Total 4669 (delta 2569), reused 4411 (delta 2409)
Receiving objects: 100% (4669/4669), 15.01 MiB | 85 KiB/s, done.
Resolving deltas: 100% (2569/2569), done.
C:ApplicationsDevelopmentphonegap>dir
 Volume in drive C is OS
 Volume Serial Number is 2E93-8194
  Directory of C:ApplicationsDevelopmentphonegap
10/14/2010  03:49 PM    <DIR>          .
10/14/2010  03:49 PM    <DIR>          ..
10/14/2010  06:32 PM    <DIR>          phonegap-android
               0 File(s)              0 bytes
               1 Dir(s)   6,243,557,376 bytes free
C:ApplicationsDevelopmentphonegap>

C:ApplicationsDevelopmentphonegap>dir phonegap-android
 Volume in drive C is OS
 Volume Serial Number is 2E93-8194

 Directory of C:ApplicationsDevelopmentphonegapphonegap-android

10/14/2010  06:32 PM    <DIR>          .
10/14/2010  06:32 PM    <DIR>          ..
10/14/2010  03:54 PM    <DIR>          bin
10/14/2010  03:54 PM    <DIR>          example
10/14/2010  06:33 PM    <DIR>          framework
10/14/2010  03:54 PM    <DIR>          lib
10/14/2010  03:54 PM             1,099 LICENSE
10/14/2010  06:33 PM    <DIR>          output
10/14/2010  03:54 PM             3,462 README.md
10/14/2010  03:54 PM    <DIR>          util
               2 File(s)          4,561 bytes
               8 Dir(s)   6,243,557,376 bytes free
C:ApplicationsDevelopmentphonegap>

Phonegap build Summary

Pay attention to this section as it may save you some frustration :-)

  • Windows XP, Vista and I'm assuming Windows 7 still have problems with long path names no matter what you may hear about NTFS making these issues a thing of the past. My initial directory structure for development began with c:ApplicationsDevelopment. When I put the eclipse workspace and projects under that hierarchy the project + package names plus generated files created path and file names that began to give me problems - anything over about 220 characters seem to be where problems begin to occur. Keep your path names short.
  • Make sure that the Android SDK and Ant are defined in your PATH and that when you define them you use forward slashes not backslashes. Again this is for Windows users. My ANDROID_HOME variable definition and the PATH entry look like this: C:/Applications/Development/eclipse/android-sdk-windows  
  • Make sure that the phonegap and android_sdk_path arguments use forward slashes (/). This matches what you did in the PATH and variable name definitions. This does not appear to matter for Eclipse and the Android SDK but phonegap is expecting the UNIX style definitions and it will give you various non-obvious errors about missing files/directories/libraries if you do not do this. Eclipse and Android will work with either definition and the main reason I am mentioning this is that you probably have defined the PATH and variable names "windows style" and tested them that way with Eclipse and Android and you have to go back and change them if you want to use Phonegap. 

 

Building the first application

Change to the Phonegap directory and issue the following command to build the example application::

ruby bin/droidgap create example

which results in a directory example-android being created which is the project that will be imported into the Eclipse workspace.

 

Importing the project into Eclipse

Again, the Phonegap Wiki has detailed instructions but essentially, you just

  • Start the Eclipse IDE.
  • Choose New project > Android > Android Project.and select create project from existing source and copy into workspace.
  • Now, in Eclipse you should see your project. Rghtclick phonegap.jar under the libs folder and go to Buildpath -> Add to build path.
  • Click on the project folder again and then the Run button in Eclipse,
  • The application should now come up in the emulator similar to what you see below.
  • You've now got your first Phonegap-Android app running.

 

Winston Lawrence

If you didn't think this article was worth a nickel then send two cents :-)
It's all good and its all very much appreciated!!   Donations and subscriptions gratefully accepted