Professional Documents
Culture Documents
Getting Started With Android PhoneGap in Eclipse
Getting Started With Android PhoneGap in Eclipse
Google seems to be favoring this wiki page over the getting started page,and in more detail here which is more up to date and have more precompiled packages ready for you to use. The information below may still be useful for some, so it is being left as is. This guide will help you s etup your firs t android project with PhoneGap 0.9.1, if you are us ing the lates t from GitHub then refer to the readme there. The firs t thing to do is to download and ins tall all the required dependencies . Please contribute platform specific instructions where needed. Some cleanup wouldn't hurt either.
Prerequisite Software
PhoneGap requires you have the following s oftware ins talled: 1. Eclips e Integrated Des ign Environment (IDE), a program to write code and manage s oftware development 2. Java Development Kit (JDK), the Java compiler and s upport tools 3. The Android Software Development Kit (SDK), bas e code and emulators for developing Android apps 4. Android Development Toolkit (ADT), a plug-in for Eclips e that makes Android development s eamles s 5. Apache Ant, a program that automates s oftware build proces s es (s imilar to 'make') 6. Ruby, a programming language 7. Git Bas h, an alternative command program for Windows environments (Windows only) If you've previous ly done any development in the Android environment, you probably have 1-4 already ins talled. If you have not, it is s trongly recommended you vis it http://developer.android.com/index.html to better unders tand Android development and create your own, "Hello, World" example prior to jumping into PhoneGap. A great res ource exis ts at: http://developer.android.com/s dk/ins talling.html for new developers .
To verify your ins tallation of Eclips e, Android SDK, ADT, and Java, you can build a s ample Hello World application us ing the ins tructions located here: http://developer.android.com/guide/tutorials /hello-world.html.
Windows If you install into a folder containing a space (c:\Program Files), you must refer to it by its truncated name in the command program. In this case "c:\progra~1". The truncated name will be the first six characters + ~ + a sequential number starting with 1 for each instance of the same six characters. Download and unzip binaries add the unzipped folder path to your PATH OS X - it comes with XCode otherwise "sudo port install apache-ant".
1F. Ruby
Vis it: http://rubyins taller.org for an automatic ins tallation (Windows only) Windows - download and ins tall Ruby from the official s ite http://www.ruby-lang.org/en/downloads / or us e the RubyIns taller (recommended) here http://rubyins taller.org/ OS X - nothing to s ee here.
If everything looks good in the environment variables , try opening a command prompt (Start > Run > cmd) and typing in "ant", "ruby", "java", or "android". If you get the error mes s age "x was not recognized as an internal or external command" you need to check your PATH value. If the program continues to execute you can us e ctrl-c to terminate it.
Alternatively, you may us e a dedicated 'git' client to download the code or you may us e Eclips e. This guide will not cover either of thos e methods . 3B. Unzip the files into a folder.
ruby bin/droidgap clas s ic "[android_s dk_path]" [name] [package_name] "[www]" "[path]" android_s dk_path ... The path to your Android SDK ins tall. (Must us e forward s las hes (/) in path) name ...................... The name of your application. package_name ....... The name of your package (For example: com.nitobi.demo). Can not be jus t the app name, mus t contain at leas t one "."
(i.e. "com.example"). www ...................... The path to your www folder. (Where your HTML, CSS and JavaScript code is located) path ...................... The path to generate the application. (CANNOT be inside ANY Eclipse workspace) (Must us e forward s las hes (/) in path)
You s hould get a mes s age in your terminal s aying completed! Example (Windows environment): $ ruby ./droidgap clas s ic "C:/Progra~1/Androi~1/android-s dk-windows " pgtes t com.example.android.pgtes t example/ "c:/Progra~1/Androi~1/phonegap-android/output" In the above Windows example, the following environment s ettings were us ed: android_s dk_path = "c:\Program Files \Android SDK\android-s dk-windows " name = pgtes t path = "c:\Program Files \Android SDK\phonegap-android\output" (This is the output location where the example files were placed) Example (Mac environment): > cd phonegap-android > ruby ./droidgap clas s ic android/android-s dk-mac_86/ tes t_app com.mayerdan.tes tapp example/ ~/projects /tes t_droid_phonegap Note if you get errors like: BUILD FAILED ~/projects /phonegap-android/framework/build.xml:49: tas kdef clas s com.android.ant.SetupTas k cannot be found followed by more errors like: 0:in `s tat': No s uch file or directory - ~/projects /phonegap-android/framework/phonegap.jar To fix this , jus t run the command with the full path to the android SDK "s dk.dir=/andriod/android-s dk-mac_86" => "s dk.dir=/projects /andriod/android-s dk-mac_86" Then run `ruby ./droidgap clas s ic /Us ers /danmayer/projects /android/android-s dk-mac_86 tes t_app com.mayerdan.tes tapp example/ ~/projects /tes t_droid_phonegap` again. Some notes : Your output path s hould not be ins ide your Eclips e works pace. *See note in Step 5 Make s ure the path you s et to generate the application does n't exis t For the www folder, phonegap-android comes with a folder called example that includes s ome s ample html, cs s and js . I found I was getting errors for not having s et android or ant in my path. Make s ure you followed the earlier s teps . When pointing to a directory, you need to have quotes around the path "C:/mobile/......" While in the command prompt, you s hould manually change all the backs las hes ( \ ) to forward s las hes ( / ). This helps the droidgap s cript with its s yntax. E.g. The android_s dk_path argument mus t us e forward s las hes (/). If you ins tall into a folder containing a s pace (c:\Program Files ), you mus t refer to it by its truncated name in the command program. In this cas e "c:\progra~1". Otherwis e, the s cript will look for "c:\Program" and fail. Note: The truncated name will be the firs t s ix characters + '~' + a s equential number s tarting with 1 for each ins tance of the s ame s ix characters . For ins tance if you have two folders c:\PhoneGapA and c:\PhoneGapB, the former's truncated name will become, "c:\phoneg~1" and the latter's will become, "c:\phoneg~2". For Windows us ers : The droidgap s cript is written to expect UNIX-s tyle s yntax, which is why you need to us e 'git bas h' to execute thes e commands . 'Bas h' is a s hell language and the 'git bas h' application can handle the expected s yntax. Alternatively you may us e another bas h-s tyle command application, s uch as 'cygwin', but if you know what that is , you know how to ins tall and us e it. Do not have "tools " as a folder in your path to the android s dk folder; C:\Us ers \Daniel\Dev\tools \android-s dk won't work. If at any point you need to check your PATH variables in git bas h you can us e the command "echo $PATH"
Now, in Eclips e you s hould s ee your project Rightclick phonegap.jar under the libs folder and go to Buildpath -> Add to build path. Note: Now the folder s tructure s hould look like the one s hown below.
Click on the project folder again and then the Run button in Eclips e, it is the button that looks like "Play". When you run this project, you s hould the dialog box to Run As .., choos e Android Application. You may get the unable to launch dialog, this can happen if you have the Android 1.5 or s ome other file highlighted when you try to run the project. Jus t s elect the project folder and click run. If you have done everything correctly, there s hould be an Application that loads . It s hould be noted that tes ting with an actual device is preferable to the Emulator for many reas ons , namely the fact that it's eas ier to tes t the Accelerometer, Geolocation, Beep and Vibrate Functionality. However, for tes ting on the actual brows er brows er events , and various is s ues with Webkit on Android the Emulator does jus t as well.
Notes
Perhaps JAVA_HOME does not point to the JDK. Or maybe JAVA_HOME does n't exis t. To fix this you mus t right click on my computer, go to advanced s ys tem s ettings , click the advanced tab, pres s the environment variables button, Under s ys tem variables click new and add JAVA_HOME if it does n't exis t and make the path to the JDK that was ins talled earlier. ERROR: droidgap:48:in ``': No s uch file or directory android create project -t 7 -k be.progone.tes t -a PG1 -n PG1 -p E:/baud/work/Mobile/Pr_Eclips e/PG1/build (Errno::ENOENT) from droidgap:48:in `create_android' SOLUTION: Add the Android SDK tools path to the s ys tem. Follow s teps above to get to s ys tem environment variables . Eg: C:\Program Files \Android SDK\tools ERROR: "BUILD FAILED C:\phonegap-android\framework\build.xml:49: tas kdef clas s com.android.ant.SetupTas k cannot be found" SOLUTION: Try going to the "framework" folder and check the path in the local.properties file. See if its correct and pointing to your android s dk directory. You MUST us e a forward s las h in the directory (Windows us ers ) otherwis e the paths will not match up. Eg: C:/Program Files (x86)/Android SDK
ERROR: "Click on various buttons on the example app, does n't do anything" or s ome things are working but I can't tell what is going on, or I want more information how do I view the Android logs ? SOLUTION: To get more information about what is going one while phonegap is running, run the Android log viewer. `adb logcat` if adb is n't found you jus t need to do the full path to it s omething like `~/projects /andriod/android-s dk-mac_86/tools /adb logcat`. If you have both a device and a Android emulator running us e -d and -e like s o `adb -e logcat`. This s hould give you more info about what is going on. Here is a s mall example clipping form the android logs .
D/PhoneGapLog( 291): file:///android_as s et/www/phonegap.js : Line 216 : TypeError: Res ult of expres s ion 'accel' [undefined] is not an object. E/Web Cons ole( 291): TypeError: Res ult of expres s ion 'accel' [undefined] is not an object. at file:///android_as s et/www/phonegap.js :216 D/dalvikvm( 117): GC_EXPLICIT freed 1727 objects / 91840 bytes in 110ms D/PhoneGap( 291): The provider network is dis abled D/SntpClient( 61): reques t time failed: java.net.SocketException: Addres s family not s upported by protocol
From this we can s ee that the accel is not working. And s ome information about the location of the Error. This helps debug problems and als o helps you to give more information when as king the PhoneGap community for help.