The Android Tutorial Series Part 1 – Hello World

The mobile world has gained a lot of mind share with the ongoing battle between Apple’s iOS and Google Android operating system. As the world wide use of smart phones or devices with smart phone like convergence capabilities increases, developer interest and demand for application development on such devices will shoot up. I have decided to take the Android operating system for a test drive. This is going to be my maiden foray in the Android world. So here goes my Hello World example for Android.

There are a number of Android ‘Hello World’ examples. My intention is to consolidate all the installation and configuration information along with the typical start-up errors faced by the Android developer. First let’s go over the configuration details. My system configuration is as below:

  • Operating System: Windows 7
  • Java Version: Java 1.6.0_24
  • Eclipse environment: Eclipse Java EE IDE for Web Developers – Indigo Service Release 1 (3.7.1)
  • Android SDK: Android 4.0
  • Eclipse Plugin: ADT (Android Development Tools) Eclipse Plugin 15.0.0

Installation and Configuration

Java is presumed to be already installed. Install the Eclipse environment. Download the eclipse installable. Eclipse ‘Classic’ version is satisfactory, however I am using Jave EE IDE for Web Developers version. Any Eclipse version greater than 3.5 is suitable.

Next proceed to installing the Android SDK. Download the SDK starter package. Run the starter package. On completion of the installation of starter package, run the SDK Mananger executable. The wizard will prompt the user to select Android packages to install. Select the following under Android 4.0 (API14):

  • Documentation
  • SDK Platform
  • Samples for SDK
  • ARM EABI v7a System Image (This is required for Android Virtual Device)
  • Google APIs (optional)

In Tools select the following:

  • Android SDK Tools
  • Android SDK Platform-Tools

For more details on recommended Andriod platform components for installation refer here.

Next, let’s install the Android Development Tools(ADT) eclipse plugin. I tried installing using the update site (both HTTPs and HTTP) but was unsuccessful; so I used the direct download option. For configuring the ADT plugin, refer the instructions mentioned here.

The official installation guide for Android can be found here.

This completes the installation and configuration of the Android platform and ADT plugin on Eclipse platform. Now let’s move on to our first working example Hello World.

Hello World Application/App

To create the Hello World Android project, start Eclipse, then select File -> New -> Other -> Android Project. Enter project name as HelloWorld, select Android 4.0 as build target and com.android.hello as package name. Leave the default selections for Create Activity and Minimum SDK. The HelloWorld project will be created in the eclipse environment. In the created project’s folder structure note two folders namely src and res. The src will have the class com.android.hello.HelloWorldActivity. Open the class. The class extends Activity class. Activity class is responsible for creating the window in which the developer places his/her custom UI contents. Here’s the source code of the HelloWorldActivity class. Note the implementation of onCreate method has been customized. The default implementation will be different.

package com.android.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloWorldActivity extends Activity {
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView view = new TextView(this);
        view.setText("This is my introduction to the world of Andriod.");
        setContentView(view);
    }
}

As the method name suggests the onCreate method is invoked during the creation of the Activity instance. Note the following three lines of the onCreate method implementation.

        TextView view = new TextView(this);
        view.setText("This is my introduction to the world of Andriod.");
        setContentView(view);

The first line creates the Android UI widget TextView which is used for displaying textual content. The second line assigns the text content i.e. ‘This is my introduction to the world of Andriod.’ and the third associates the TextView to the Activity instance.

In the eclipse package explorer examine the res folder. This folder will have two sub folders namely layout and values. The main.xml within layout folder’s use will be elaborated in Part 2 of this series. The strings.xml in values folder holds the message resources in a typical key value pair fashion. The application name is stored under the key app_name. Currently the value is defaulted to the Android project name; you are free to customize.

Now let’s run the Hello World Android application. To run the application we need an Android device or something that emulates an Android device. The ADT plugin provides an Android Virtual Device(AVD) emulator. The AVD can be created by invoking AVD Manager.exe directly within the Android SDK or within the Eclipse environment using the menu Window -> AVD Manager. For instructions on creating AVD refer here. While configuring the AVD, for skin select the built-in HVGA. This helps in reducing the start-up time, in turn improving the developer productivity.

Errors & their fixes while running the Hello World Application

In case one has installed the SDK in the non-standard location like D: drive etc you might encounter the following error.

[2011-11-11 12:34:18 - Emulator] PANIC: Could not open: C:\Users\<username>\.android/avd/myAVD.ini 

One of the ways to fix this problem is to copy the contents on the .android folder to the C:\Users\ folder. The other is run the following command:

mklink /J "C:\Users\<username>\.android" "D:\Users\<username>\.android"

Note the mklink command only works in Vista and Windows 7 operating systems.

The other problem experienced by developers is the following:

Unable to find a 'userdata.img' file for ABI armeabi to copy into the AVD folder.

This error is experienced if the package ‘ARM EABI v7a System Image’ is not installed in the SDK. Install it and the error should go away.

Now to run the app, right click on the HelloWorld project in the package explorer. Select Run As -> Android Application. The Run configuration wizard is displayed. Retain the defaults and select the Automatic option for target. The eclipse console will show the following if the deployment is successful:

[2011-11-15 14:29:01 - HelloWorld] ------------------------------
[2011-11-15 14:29:01 - HelloWorld] Android Launch!
[2011-11-15 14:29:01 - HelloWorld] adb is running normally.
[2011-11-15 14:29:01 - HelloWorld] Performing com.android.hello.HelloWorldActivity activity launch
[2011-11-15 14:29:01 - HelloWorld] Automatic Target Mode: launching new emulator with compatible AVD 'myAVD'
[2011-11-15 14:29:01 - HelloWorld] Launching a new emulator with Virtual Device 'myAVD'
[2011-11-15 14:29:24 - HelloWorld] New emulator found: emulator-5554
[2011-11-15 14:29:24 - HelloWorld] Waiting for HOME ('android.process.acore') to be launched...
[2011-11-15 14:30:42 - HelloWorld] HOME is up on device 'emulator-5554'
[2011-11-15 14:30:42 - HelloWorld] Uploading HelloWorld.apk onto device 'emulator-5554'
[2011-11-15 14:30:43 - HelloWorld] Installing HelloWorld.apk...
[2011-11-15 14:31:42 - HelloWorld] Success!
[2011-11-15 14:31:42 - HelloWorld] Starting activity com.android.hello.HelloWorldActivity on device emulator-5554
[2011-11-15 14:31:44 - HelloWorld] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.android.hello/.HelloWorldActivity }

Note the considerable time spent between the Waiting for HOME and HOME is up on device…. statements. This is primarily because the Android architecture is defined on a different CPU vis-a-vis the Intel or AMD processor architecture. Therefore the emulator takes time to compile code.

Note that once the AVD has been started it should not be shutdown, you can run the application on the started AVD instance. To achieve this in the Run configuration set the target selection in manual mode. The user will be prompted to select the target AVD. On being prompted select the newly started AVD. For details check reference and read answer from ‘spiffly’.

The AVD should open the application with the App name ‘HelloWorld’ and the text as mentioned in the onCreate method prominently displayed on the emulator screen.

That’s all for android development desk. In my next blog post I will create a small working Android application.

Advertisements

4 thoughts on “The Android Tutorial Series Part 1 – Hello World

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s