Basic PhoneGap Android app from scratch step-by-step

This tutorial assumes that you have eclipse which is set up for regular android development and you can create new android projects. Phonegap files are now named cordova

  1. Create new Android project in eclipse.
  2. Download package and extract cordova-2.6.0.jar  from zip file from http://phonegap.com/download/
  3. Add cordova-2.6.0.jar to your projects build path. In eclipse open project properties > Java Build Path > Add Jars
  4. Change main Activity superclass to DroidGap
  5. Replace setContentView(R.layout.main); with super.loadUrl(“file:///android_asset/www/index.html”);
  6. Create folder xml into the project res folder. Add there plugins.xml and cordova.xml from the package you download before.
  7. Add into AndroidManifest.xml <uses-permission android:name=”android.permission.INTERNET” /> and     <uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />
  8. Create folder www into projects assets folder.
  9. Add index.html file into assets/www. File content is regular HMTL.

By now you can already run the program. Next steps are starting to use PhoneGap special HTML5 features and try accessing hardware. If It fails to run then make sure you have logcat view activated in eclipse or try in linux commandline:

./adb logcat

 

ERROR:
 W/System.err(24831): ERROR: plugin.xml is missing.  Add res/xml/plugins.xml to your project.
 W/System.err(24831): https://raw.github.com/phonegap/phonegap-android/master/framework/res/xml/plugins.xml
 SOLUTION:
 Make sure you have plugins.xml in projects res/xml folder.

 

ERROR:
 W/System.err(24831): java.net.SocketException: Permission denied
 W/System.err(24831):     at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method)
 W/System.err(24831):     at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335)
 W/System.err(24831):     at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:219)
 W/System.err(24831):     at org.apache.harmony.luni.net.PlainServerSocketImpl.create(PlainServerSocketImpl.java:40)
 W/System.err(24831):     at java.net.ServerSocket.<init>(ServerSocket.java:120)
 W/System.err(24831):     at java.net.ServerSocket.<init>(ServerSocket.java:74)
 W/System.err(24831):     at com.phonegap.CallbackServer.run(CallbackServer.java:208)
 W/System.err(24831):     at java.lang.Thread.run(Thread.java:1027)
 SOLUTION:
 Add <uses-permission android:name="android.permission.INTERNET" /> permission

 

ERROR:
E/AndroidRuntime(24831): FATAL EXCEPTION: main
E/AndroidRuntime(24831): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobiwsale.client/com.mobiwsale.client.MobiwsaleClientActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
E/AndroidRuntime(24831):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
E/AndroidRuntime(24831):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
E/AndroidRuntime(24831):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
E/AndroidRuntime(24831):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
E/AndroidRuntime(24831):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(24831):     at android.os.Looper.loop(Looper.java:150)
E/AndroidRuntime(24831):     at android.app.ActivityThread.main(ActivityThread.java:4277)
E/AndroidRuntime(24831):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(24831):     at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(24831):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(24831):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(24831):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(24831): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
E/AndroidRuntime(24831):     at android.content.res.Resources.getValue(Resources.java:901)
E/AndroidRuntime(24831):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1887)
E/AndroidRuntime(24831):     at android.content.res.Resources.getXml(Resources.java:789)
E/AndroidRuntime(24831):     at com.phonegap.api.PluginManager.loadPlugins(PluginManager.java:82)
E/AndroidRuntime(24831):     at com.phonegap.api.PluginManager.<init>(PluginManager.java:62)
E/AndroidRuntime(24831):     at com.phonegap.DroidGap$1.run(DroidGap.java:414)
E/AndroidRuntime(24831):     at android.app.Activity.runOnUiThread(Activity.java:3878)
E/AndroidRuntime(24831):     at com.phonegap.DroidGap.loadUrlIntoView(DroidGap.java:390)
E/AndroidRuntime(24831):     at com.phonegap.DroidGap.loadUrl(DroidGap.java:356)
E/AndroidRuntime(24831):     at com.mobiwsale.client.MobiwsaleClientActivity.onCreate(MobiwsaleClientActivity.java:11)
E/AndroidRuntime(24831):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
E/AndroidRuntime(24831):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
E/AndroidRuntime(24831):     ... 11 more
SOLUTION:
Check plugin.xml and phonegap.xml files in res/xml
ERROR:
D/DroidGap(24990): DroidGap: GapViewClient.onReceivedError: Error code=-14 Description=The requested file was not found. www/index.html URL=file:///android_asset/www/index.html
SOLUTION
add index.html to to folder assets/www

ERROR:
E/AndroidRuntime( 4320): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x10000000 (has extras) } in org.apache.cordova.NetworkManager$1@4053d928
SOLUTION:
Add <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> permission

See also http://hiediutley.com/2011/10/28/hellophonegap1-0-project-updated-for-phonegap-1-1-0/

5 thoughts on “Basic PhoneGap Android app from scratch step-by-step

  1. Pingback:Add Facebook login to PhoneGap Android app Easiest way « Margus Pala is fixing IT!

  2. Pingback:PhoneGap vs native Android and iPhone app performance and features « Margus Pala is fixing IT!

  3. Pingback:PhoneGap application crashing when rotating « Margus Pala is fixing IT!

Leave a Reply

Your email address will not be published.


*