Wednesday, March 3, 2010

Hello, Android

Some of you may know that I recently purchased the Nexus One, an incredible phone - of which I have completely fallen in love with - which runs the Android operating system. Despite my Java not being my strongest background, I've long been interested in developing for the Android phone. The idea of being able to develop a personalized program to handle my data on the fly wherever I am is personally very intriguing.

And now, at the start of March Madness, I will walk you through my process of creating the simplest of simple programs - an Android Hello World.
First off, I am using Eclipse Galileo. Android will work with either Galileo or Ganymede. I am also using Ubuntu 9.10, though honestly there is very little in this process that should change due to your operating system.

Head straight to the Help menu and select Install New Software.

Now you're going to want to type in the address https://dl-ssl.google.com/android/eclipse into the Work with: bar. Select Developers tool and hit Next until you have it installed. This will set eclipse up with the necessary tools to run and develop Android software in Eclipse. We still need the SDK however.

Head over to the Android SDK site and select the appropriate SDK for your operating system. Simply download it and extract it to its future home. I extracted mine right to my home directory in Ubuntu - Windows users could probably extract it to their Program Files Directory. Just remember where you extract it to.

Once extracted, navigate there through a file manager or through a terminal. Inside, you'll find the tools folder. Inside there is an application called "android". You can either run this program here, or run it through Eclipse if you've installed the Android Development Tools already. Click on the new cell phone button. This is the Android SDK and AVD Manager. Here we can create an Android SDK for the version of Android we want to develop for (I have a Nexus One, which runs 2.1, but is backwards compatible. The Droid, as of this writing, runs 2.0 but is rumored to soon be upgraded. Some Android phones still run 1.6). We can also create Virtual Devices that have certain features that we can emulate our Android programs on to test them out.

For now, go to the Available Packages Section and click to install the Android API you need. Note - I experienced a bug that wouldn't let me access the repository list. Merely go to Settings and check "Force https://..." - this should fix that.


Once installed, scurry over to the Virtual Devices tab and select New. Make a name for this virtual device - you're essentially making an emulated phone with certain hardware, running Android of a certain version, in which you can test your programs on. Most of these settings don't have to be touched - you can get away with just naming the device and selecting that target.

Go to the Window menu in Eclipse and select Preferences - you will need to point Eclipse to the Android SDK in here.

Now in Eclipse, create a new Android Project.

The project name can be anything - that is Eclipse specific and doesn't affect the end result.

Select the Android version you'll be working with - any that you downloaded in the SDK manager will appear here. Note that Android SDK version numbers have an API Level, also known as an Min SDK Version. Android 2.0, for instance, is 5. Android 2.1 is 7.

The application name is the name of the application to the user on the Android phone.

The package name is the actual package it will be running as on the Android phone, and what package the program will be stored in. Also note that this is how the phone will organize your task. A program's process is identified by that package name on Android. (Or so I think, looking at how some task killers work in Android. I admit, this part is just theory by me.)


The activity name is a one word name for your program as it runs.

The Min SDK Version should auto change when you select your Android version in Build Target.

Hit Finish and behold your code - merely make these tweaks to it and you have an executing Hello World!
package com.example.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText("sup, world?");
        setContentView(tv);
    }
}
Hit Run, and select to run it as an Android application. The emulator takes a minute or so to boot up the first time. If all goes well...
And now that I've opened that door, I have to explore doing so much more with this... Exciting times.

2 comments:

  1. You're close. The package name is a unique identifier on the device, such that no two applications installed may share the same package name. The package name is also used for intent resolution in the case of explicit intents. However, since this is linux, every app has its own user id and process id, with the very special exception of apps that share user ids.

    ReplyDelete