Saturday, September 21, 2013

Start Developing Mobile Apps - Phonegap Hello World! - Setting up Eclipse for Android on Windows

Last updated 21 Sept 2013

Creating a mobile app is easier than I thought thanks to the latest technologies at your disposal.
Web developers can now translate their skills into  mobile apps.

PhoneGap allows developers to build apps in HTML, Javascript and CSS deployable on most platforms including Android, iOS, Windows and Blackberry. Use this tutorial if you are using Window and want to build for Android.

Step 1 - Download Eclipse + Android SDK

Eclipse is a free development environment and the Android SDK contains all the files necessary to code for Android. Google provides this with its ADT - Android Developer Tools

Download Android ADT

Follow the below steps to setup:

1. Unpack the ZIP file and save it to desktop

2. Open the adt-bundle-/eclipse/ directory and launch eclipse.exe

It will ask you where to set up your workspace. Default is C://username/workspace
That’s it! The IDE is already loaded with the Android Developer Tools plugin and the SDK is ready to go.



Step 2 - Download and Install Phonegap

Phonegap is a set of files that you include into your Android app to translate from HTML to different mobile app formats

Download Phonegap

Unpack and we will use it later.

Step 3 - Creating a project in Eclipse

Now lets start creating  our project. Launch Eclipse.

3.1. Select File > New > Android Application Project

3.2. Fill up the name of your app and the package name as com.yourname.yourprojectname. I'll name this MyApp


Click Next

3.3. Here is where you put your app icon if you already have it. Click Next

3.4. Click next to create a new Activity - a working screen containing one thing - in this case your main page.


3.5. Click next - Select None for Navigation

Now you should have a blank Android Project in Eclipse.

Step 4 - Configure Phonegap

4.1. Locate your Project directory where all the above files are sitting.

To find it, right click on the Project Title and click Properties.
The location field will tell you where are the files residing.


Now we need to copy a few files from the Phonegap download into the Android Project file.
Go to Phonegap > lib > Android

4.2. Do the following to copy the required files

a. Create a folder called www under MyApp/assets
b. Copy cordova.js to MyApp/assets/www
c. Copy cordova-x.x.x.jar to the MyApp/libs folder
d. Copy xml to the MyApp/res folder
e. Right click on MyApp in Eclipse and click Refresh

You should have something like so


4.3. Add the Cordova library.
From the MyApp/libs folder, right click cordova-x.x.x.jar and select Build Path > Add to Build Path



4.4. Create a new file in MyApp/assets/www named index.html


This index.html is where you put in all your app's code.
After you have created. right click on the file and open with text editor.
Copy and paste the below code into index.html:

<!DOCTYPE HTML>
<html>
<head>
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

Step 5 - Configure Activity class

A few more configurations to get everything working.
Go to MyApp/src/com.myname.myapp and edit the MainActivity.java file

You should have the following code:

package com.mycompany.myapp;
//** Note this field is unique to your own app

import android.os.Bundle;
import org.apache.cordova.*;
// This tells the project to import Phonegap files

public class MainActivity extends DroidGap {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl(Config.getStartUrl());
        //This points to www/index.html
    }
    
}


Step 6 - Configure Metadata

Go to MyApp/res/AndroidManifest.xml



Open AndroidManifest.xml

Add the below under the <manifest> node for it to support different sized screens


android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true"
/>

Add the below under the <manifest> node for it to support get permission to access device features

<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.INTERNET" />
   <uses-permission android:name="android.permission.RECEIVE_SMS" />
   <uses-permission android:name="android.permission.RECORD_AUDIO" />
   <uses-permission android:name="android.permission.RECORD_VIDEO"/>
   <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" />

Look for the <activity> node and add this

android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"

Your AndroidManifest.xml should look something like the below but you should not copy the below as certain parameters may differ.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<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.INTERNET" />
   <uses-permission android:name="android.permission.RECEIVE_SMS" />
   <uses-permission android:name="android.permission.RECORD_AUDIO" />
   <uses-permission android:name="android.permission.RECORD_VIDEO"/>
   <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" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.mycompany.myapp.MainActivity"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Step 7 - Run the application!

7.1. Now we are two steps away from the final product.
Right click on your root folder in this case MyApp>Run As>1. Android Application


7.2. You will be prompted to run it on a device. To launch a new device, click on the Android Virtual Device Manager to launch your own virtual device.

Click New


You will be prompted to create a device. I chose the following settings


When its created, find the start button to launch the emulator - a virtual device which to test your Android App.

It takes a while for the emulator to startup.

Or you can start it directly from the Run > Run Configurations > Targets



Your final result should look something like this!

Closing notes:

1. With this framework, you can start to create Android Apps the way you make websites.
2. Save this template as a start for other projects!
3. Good luck and happy developing!



Popular Posts