http://wiki.phonegap.com/w/page/30862722/phonegap-android-eclipse-quickstart

This is a tutorial for working with PhoneGap for Android on Eclipse with the latest version of PhoneGap.

 

Requirements

 

It is assumed that you have the Android SDK installed and working. If not, then you need to install it before proceeding. You can find the download and installation instructions here. Also, ensure that you have created at least one Android virtual device (AVD). You will need an AVD to run your project in the Android emulator.

 

You also need to:

 

In Eclipse, ensure that you have told Eclipse where the Android SDK is installed in Preferences > Android. (See below.)

 

 

Now you can either build the sample project or skip down to create a new project.

 

Building The Sample Project

 

File > New > Android Project

 

  • Select Create new project from existing source
  • Click Browse and point it to the location of the sample app provided with your PhoneGap 0.9.4 download

Don't bother using older versions of Android. Use the highest SDK target available. Phonegap will take care of backwards compatibility for you.  Note: You may experience issues with Android 3.0 so try using Android 2.2 or 2.3 first.

 

(You might experience an error here, where Eclipse can't find phonegap.jar. In this case, right click on the /libs folder and go to Build Paths/ > Configure Build Paths. Then, in the Libraries tab, add phonegap.jar to the Project. If Eclipse is being temperamental, you might need to refresh (F5) the project.)

 

You can now run you project as an Android Application. Right click the project and go to Run As and click Android Application.  Eclipse might ask you to select an appropriate AVD. If there isn't one, then you'll need to create it before you can continue.

 

 

 

Creating A New Project

 

File > New > Android Project

 

 

And give it some sensible defaults. 

 

 

Don't bother using older versions of Android. Use the highest SDK target available. Phonegap will take care of backwards compatibility for you.

 

From the PhoneGap download earlier, we need the following two files:

  1. Android/phonegap-1.0.0.jar
  2. Android/phonegap-1.0.0.js

 

In the root directory of the project you created in Eclipse, create two new directories:

  1. /libs
  2. /assets /www

 

Now copy

  1. Android/phonegap.0.9.4.jar to /libs 
  2. Android/phonegap.0.9.4.js  to /assets/www

 

In Eclipse, select the project in the Package Explorer and refresh (F5) the project. The copied file will appear in the project.

 

Now, create index.html in your www folder and add some code like so:

 

<!DOCTYPE HTML>
<html>

  <head>
    <title>PhoneGap</title>

  <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>      

  <script type="text/javascript" charset="utf-8">

     function onLoad(){

          document.addEventListener("deviceready", onDeviceReady, true);

     }

     function onDeviceReady(){

          navigator.notification.alert("PhoneGap is working");

     }

  </script>

  </head>

  <body onload="onLoad();">
       <h1>Welcome to PhoneGap</h1>

       <h2>Edit assets/www/index.html</h2>

  </body>

</html>

 

 

Make a few adjustments to the project's main Java file found in the src folder in Eclipse.

  1. Change the class's extend from Activity to DroidGap
  2. Replace the setContentView() line with super.loadUrl("file:///android_asset/www/index.html");
  3. Add import com.phonegap.*;

 

 

(You might experience an error here, where Eclipse can't find phonegap-1.0.0.jar. In this case, right click on the /libs folder and go to Build Paths/ > Configure Build Paths. Then, in the Libraries tab, add phonegap-1.0.0.jar to the Project. If Eclipse is being temperamental, you might need to refresh (F5) the project once again.)

 

Finally, lets add some permissions to the AndroidManifest.xml file to allow phonegap to run properly.

Open up your manifest file in your favourite editor and paste the following information after the versionName but before the application tag:

 

<supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:resizeable="true"
        android:anyDensity="true"
        />
        <uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="android.permission.VIBRATE" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
        <uses-permission android:name="android.permission.RECORD_AUDIO" />
        <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <uses-permission android:name="android.permission.WRITE_CONTACTS" />   
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

 

 

 

In AndroidManifest.xml, make sure the activity tag contains the following attribute:

 

    android:configChanges="orientation|keyboardHidden"

 

This tells android not to reload index.html when these events happen; phonegap will handle these events appropriately.

 

Last step is to copy the xml folder that came with the PhoneGap download into the res folder of your project.

 

You can now run you project as an Android Application. Right click the project and go to Run As and click Android Application.  Eclipse might ask you to select an appropriate AVD. If there isn't one, then you'll need to create it before you can continue.

 

If all goes well then the AVD will load in the emulator -- be prepared to wait for this -- and you application will be uploaded, installed, and run.

 

 

Have fun!

+ Recent posts