keskiviikko 31. lokakuuta 2012

Installing Android ADT on Flash Builder / Eclipse and making Hello Android & Hello PhoneGap apps


This post describes how to install Android SDK platform and develop your first Android application with PhoneGap. Here's how I did it.

What I used: Adobe Flash Builder (based on Eclipse) on Mac OS X Mountain Lion, Macbook Pro with Retina Display.

You can also use Eclipse on Linux or Windows.

Installing Android SDK and running Hello Android

First install the Android platform for Flash Builder or Eclipse (ADT plugin for Eclipse):
  1. Help > Install New Software. https://dl-ssl.google.com/android/eclipse/
  2. Install Android 2.2 SDK
New Android project:
  1. File > New > Project… Android, Android Application Project
  2. Application Name: Hello Android. Next, next, next...
  3. Install dependencies: Install/Upgrade, Accept All.
Running the Hello Android app in Android emulator:
  1. Project explorer > Hello Android > src > MainActivity.java. Double click to open in editor. Run: Run. Android Application.
  2. New Android Virtual Device? Target: API Level 8. Name: api8.
  3. Run Android Hello World in emulator.

Installing PhoneGap and running Hello PhoneGap

Download PhoneGap: http://phonegap.com/download

(Below is my updated guide based on PhoneGap's Getting Started with Android)

Create new project on Flash Builder / Eclipse
  1. FIle > New > Project > Android, Android Application
  2. Create two new directories to root directory of your project:
    • /libs
    • assets/www
  3. Copy cordova-2.1.0.js from your Cordova download earlier to assets/www
  4. Copy cordova-2.1.0.jar from your Cordova download earlier to /libs
  5. Copy xml folder from your Cordova download earlier to /res
  6. Verify that cordova-2.1.0.jar is listed in the Build Path for your project.
    • Flash Builder: Right click over project name in project explorer, open Properties, go to Java Build Path, open Libraries tab and add cordova-2.1.0.jar to the project.
    • Eclipse: Right click on the /libs folder and go to Build Paths/ > Configure Build Path.... Then, in the Libraries tab, add cordova-2.1.0.jar to the project. If Eclipse is being temperamental, you might need to refresh (F5) the project once again.
  7. Edit src > MainActivity.java
    • Add import org.apache.cordova.*;
    • Change the class's extend from Activity to DroidGap
    • Replace the setContentView() line with super.loadUrl("file:///android_asset/www/index.html");
  8. Edit AndroidManifest.xml and paste the following permissions below tag:
 <supports-screens   
   android:largeScreens="true"   
   android:normalScreens="true"   
   android:smallScreens="true"   
   android:resizeable="true"   
   android:anyDensity="true" />  
 <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" />   
 <uses-permission android:name="android.permission.GET_ACCOUNTS" />  
 <uses-permission android:name="android.permission.BROADCAST_STICKY" />  

Create and run Hello World
  1. Create new file named index.html to assets/www directory. Paste the following:
 <!DOCTYPE HTML>  
 <html>  
 <head>  
 <title>Cordova</title>  
 <script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js"></script>  
 </head>  
 <body>  
 <h1>Hello World</h1>  
 </body>  
 </html>  

Run: Right click project name and go to Run As > Android Application

There you have it!

Issues:
I ran into an issue where the emulator opened to the lock screen but the emulator was not responding. Probably the emulator process was stuck somewhere. Restarting the computer fixed that.

See also: Hello PhoneGap on xUbuntu 12.04

Ei kommentteja:

Lähetä kommentti