Jump to content

Installation information for 1.6 "Donut" Android SDK

+ 5
  andyo's Photo
Posted Oct 23 2009 12:49 PM

Zigurd Mednieks, ace telephony developer and co-author of Android Application Development, has written new installation information for the 1.6 "Donut" Android SDK. It's also available as a PDF (also attached to this posting). Everything else in the book--the examples and the material about the actual use of the API--has been tested on this and other recent releases of the SDK and is still accurate.

Setting Up Your Development Environment

Android applications, like most mobile phone applications, are developed in a host-target development environment. In other words, you develop your application on a host computer (where resources are abundant), and download it to a target mobile phone for testing and ultimate use. Applications can be tested and debugged either on a real Android device or on an emulator. For most developers using an emulator is easier for initial development and debug, followed by final testing on real devices.

To write your own Android mobile phone applications, you'll first need to collect the required tools and set up an appropriate development environment on your PC or Mac. In this chapter we'll collect the tools you need, download them and install them on your computer, and write a sample application that will let you get the feel of writing and running Android applications on an emulator. Linux, Windows, and OS X are all supported development environments, and we'll show you how to install the latest set of tools on each. Then we'll show you any configuration you need to do after the tools are installed (setting PATH environment variables and the like), again for each of the three operating systems. And finally we'll write a short little "Hello, Android" application that demonstrates what needs to be done to get a generic application running.

This chapter refers to specific versions of the tools you will use for Android programming. Google has released three versions of the SDK since it was first released, and will probably continue a rapid pace of development and new releases. The developers of Java and Eclipse can also issue updates independently of the Android SDK. Therefore, if you find yourself using versions of Java, Eclipse, and the Android SDK newer than those described here, check the installation instructions for Java, Eclipse, and the Android SDK for updated information. Configuration dialogs have changed since Android was first released and may change again.

The Android Software Development Kit (SDK) supports several different integrated development environments (IDEs). For this book we will focus on using Eclipse as the IDE, because it is the best integrated with the SDK, and, hey, it's free. No matter which operating system you are using, you will need essentially the same set of tools:

  • The Eclipse Integrated Development Environment
  • Sun's Java Development Kit (JDK)
  • The Android Software Developer's Kit (SDK)
  • A special Eclipse plug in: the Android Developer Tool (ADT)

Since you're probably going to develop on only one of the host operating systems, skip to the appropriate section that pertains to your selected operating system.

Creating an Android Development Environment

The Android Software Development Kit supports Windows (XP and Vista), Linux (tested on Ubuntu Dapper Drake, but any recent Linux distro should work), and Mac OS X (10.4.8 or later) as host development environments. Installation of the SDK is substantially the same for any of the operating systems, and most of this description applies equally to all of them. Where the procedure differs, we will clearly tell you what to do for each environment.

  • Install the Java Development Kit (JDK): The Android SDK requires JDK version 5 or version 6. If you already have one of those installed, skip to the next step, where you will install Eclipse. Mac OS X comes with the JDK version 5 already installed, and many Linux distributions include an up to date JDK. If the JDK is not installed, go to http://java.sun.com/javase/downloads and you'll see a list of Java products to download. The one you want is JDK 6 Update n for your operating system, where n is 6 at the time this is written.

    Windows (XP and Vista)
    • Select the distribution for "Windows Offline Installation, Multi-language."
    • Read, review, and accept Sun's license for the JDK. (The license has become very permissive, but if you have a problem with it, alternative free JDKs exist.)
    • Once the download is complete, a dialog box will ask you whether you want to run the downloaded executable. When you select "Run," the Windows Installer will start up and lead you through a dialog to install the JDK on your PC.

    • Select the distribution for "Linux self-extracting file."
    • Read, review, and accept Sun's license for the JDK. (The license has become very permissive, but if you have a problem with it, alternative free JDKs exist.)
    • You will need to download the self-extracting binary to the place you want to install the JDK on your filesystem. If that place is a system wide directory (such as /usr/local), you will need root access. After the file is downloaded, make it executable (chmod +x jdk-6version-linux-i586.bin), and execute it. It will self-extract to create a tree of directories.

    Mac OS X
    Mac OS X comes with JDK Version 5 already loaded.

  • Install Eclipse: The Android SDK requires Eclipse, and the SDK version 1.6 is compatible with Eclipse version 3.5, also known as "Galileo." To get Eclipse, you will need to go to http://www.eclipse.org/downloads to get it.

    Eclipse comes in several different, pre-packaged configurations. You want the version of the Eclipse IDE labeled "Eclipse IDE for Java Developers," and, obviously, you want the
    version for your operating system.

    Windows (XP or Vista)
    The Eclipse download comes as a big ZIP file that you install by extracting the files to your favorite directory. For this book, we'll assume that you extracted to C:/eclipse. Eclipse is now installed, but it will not show up in your Start menu of applications. You may want to create a Windows shortcut for C:/eclipse/eclipse.exe and place it on your desktop, in your Start menu, or someplace else where you can easily find it.

    The Eclipse download comes as a large archive file (.tar.gz file, also known as a "tarball") that you install by extracting the files to your any directory you choose. On most Linux systems, opening the archive file starts the archive manager application. You can then drag the Eclipse folder from the archive to where you want to extract it. For this book, we'll assume that you extracted to your home folder. The executable itself is located in that directory and is named eclipse. You may want to create a shortcut to this executable file.

    Mac OS X
    The Eclipse download comes as a large "zip" file that you install by extracting the files to your any directory you choose. Opening the archive file starts the download folder shows the contents of the archive. You can then copy Eclipse folder from the archive and paste it where you want to extract it. The Eclipse executable itself is located in the directory where you extracted the archive, and is named eclipse. You may want to create a shortcut to this executable file.

  • Check for Required Plugins: You can skip this step if you just downloaded a current version of Eclipse as we recommended. If you are using a pre-installed version of Eclipse that was already on your PC, you need to make sure you have the Java Development Tool (JDT) and Web Standard Tools (WST) plug-ins installed. You can easily check to see whether they are installed by starting Eclipse, which if this is the first time you have started Eclipse, will ask you to specify the location of your workspace. Once Eclipse is started, selecting menu options "Window → Preferences..." will bring up the preferences dialog. The list of preferences should include one for "Java" and one for either "XML" or "Web and XML." If they aren't on the list, the easiest thing to do is reinstall Eclipse, as described in the previous step. Installing "Eclipse IDE for Java Developers" will automatically get the needed plugins.

  • Install Android SDK: This is where you would start if you already have the right versions of Eclipse and the JDK loaded. The Android SDK is distributed through
    Google's Android site, http://developer.android.com. The SDK consists of two parts: An archive you will download and extract, much in the way you installed Eclipse, and an Eclipse plug-in that adds Android-specific support to the Eclipse IDE. In the list of downloads, you will see a table of distributions. Select the one for your operating system (XP and Vista use the same distribution). The Android download site provides directions about how to install the plugin through Eclipse's software updates utility for the versions of Eclipse supported by the SDK, and this chapter covers installing the plug in, too, after the description, below, of how to install the SDK files in the archive you are downloading.

    The file you download is another archive file, as with Eclipse: a ZIP file on Windows, or a tarball (.tar.gz) file for Linux and MacOS X. Extract the archive file to a directory where you want to install the Android SDK, such as your home folder, and make a note of the directory name (you'll need it in Step 7). The extraction will create a directory tree containing a bunch of subdirectories, including one called tools. The tools directory contains executable files used by the Eclipse plug and that you will also use directly.

  • Update Environment Variables: To make it easier to launch the Android tools, add the tools directory to the directories listed in your shell's PATH variable, or create a shortcut to the tools folder.

    • Under Windows XP, click on Start, then right-click on My Computer. In the pop-up menu, click on Properties. In the resulting System Properties dialog box, select the Advanced tab. Near the bottom of the Advanced tab is a button, "Environment Variables," that takes you to an Environment Variables dialog. User environment variables are listed in the top half of the box, System environment variables in the bottom half. Scroll down the list of System environment variables until you find "Path," select it, and click the "Edit" button. Now you will be in an Edit System Variable dialog that allows you to change the environment variable "Path." Add the full path of the tools directory to the end of the existing Path variable and click "OK." You should now see the new version of the variable in the displayed list. Click "OK" and then "OK" again to exit the dialog boxes.

    • On Windows Vista, click on the Microsoft "flag" in the lower left of the desktop, then right-click on Computer. At the top of the resulting display, just below the menu bar, click on "System Properties." In the column on the left of the resulting box, click on "Advanced system settings." Vista will warn you with a dialog box that says "Windows needs your permission to continue";- click "Continue." Near the bottom of the "System Properties" is a button labeled "Environment Variables" that takes you to an Environment Variables dialog. User environment variables are listed in the top half of the box, System environment variables in the bottom half. Scroll down the list of System environment variables until you find "Path," select it, and click the "Edit" button. Now you will be in an Edit System Variable dialog that allows you to change the environment variable "Path". Add the full path of the tools directory to the end of the existing Path variable, and click "OK." You should now see the new version of the variable in the displayed list. Click "OK" and then "OK" again to exit the dialog boxes.

    • On Linux, the PATH environment variable can be defined in your ~/.bashrc ~/.bash_profile file. If you have either of those files, use a text editor such as gedit, vi,or Emacs to open the file and look for a line that exports the PATH variable. If you find such a line, edit it to add the full path of the tools directory to the path. If there is no such line, you can add a line like this:

      export PATH=${PATH}:[i]your_sdk_dir[/i]/tools

      where you put the full path in place of your_sdk_dir.

    • On MacOS X, look for a file named .bash_profile in your home directory (note the initial dot in the filename). If there is one, use an editor to open the file and look for a line that exports the PATH variable. If you find such a line, edit it to add the full path of the tools directory to the path. If there is no such line, you can add a line like this:

      export PATH=${PATH}:[i]your_sdk_dir[/i]/tools

      where you put the full path in place of your_sdk_dir.

  • Install the Android Plugin (ADT): Throughout this book, we will make use of the Android Development Tool plugin that Google supplies for use in building Android applications. The Android plugin is key to making the Android SDK work with Eclipse.

    The plugin is installed in much the same way as any other in Eclipse plugin. The instructions here are for Eclipse version 3.5 (Galileo). If you are using a different version of Eclipse, you should check the Eclipse site for documentation on installing plugins and the Android SDK site for any changes in installation instructions:

    • Start Eclipse, if it's not already running.

    • select Help > Install New Software. In the Available Software dialog that now appears, click "Add"....

    • In the Add Site dialog that now appears, enter a name for the Android plug in site, such as "Android Plugin" in the "Name" field. In the "Location" field, enter this URL: https://dl-ssl.googl...ndroid/eclipse/. Click "OK."

    • In the Available Software view of the Install New Software dialog, you should now see "Developer Tools" added to the list. Select the checkbox next to Developer Tools, which will also automatically select Android DDMS and Android Development Tools. Click "Next."

    • In the Install Details dialog that now appears, the Android DDMS and Android Development Tools features are listed. Click "Next" to read and accept the license agreement and install any dependencies, then click "Finish."

    • Restart Eclipse.

    • After Eclipse restarts, you need to tell it where the SDK is located. From the menu bar, select "Window → Preferences." (On Mac OS X, the Preferences menu is on the "Eclipse" menu.) In the Preferences dialog, select "Android" in the left hand column.

    • Use the "Browse" button to navigate to the place you installed the Android SDK, and click on "Apply," then on "OK."

Congratulations - you have installed a complete Android development environment without spending a penny. As you'll see in this and subsequent chapters, the environment includes a very sophisticated set of tools to make Android programming easier, including:

  • An Integrated Development Environment based on Eclipse, the most widely used IDE for Java development. Eclipse itself contributes many valuable features applicable to Android software development. Google and OHA have taken advantage of Eclipse's extensibility to provide features customized for Android, including debugging capabilities that are tuned to the needs of mobile application developers like you.

  • A complete mobile phone emulator that allows you to test your applications without having to download them to a target mobile phone. The emulator includes features for testing your application under different mobile phone communication conditions (fading, dropped connections, etc.).

  • A complete Android operation system running on the emulator, and Dalvik virtual machine that build on Sun's JDK foundation to provide a very sophisticated programming environment for your applications.

  • Test and debugging tools, such as TraceView, which allow you to tune your application to take best advantage of the limited resources available on a mobile phone.

Hello, Android

Enough downloading; let's write a program. A "Hello, World!" program is traditional, and we will start with something similar to demonstrate what you need to do to create, build, and test an Android application. We won't explore much of the Android API for this program - that's left for the following chapters - but here we'll get a taste for the development environment and the steps you go through to create an application for Android, and you can see that your installation of the Android SDK works correctly.

Where We're Going

There isn't much to the functionality of this program. We just want to display some text on the Android emulator window that says "Hello, Android!" (see Figure 1.1, "Hello Android" Screenshot).

Figure 1.1. "Hello Android" Screenshot

Attached Image

Starting a New Android Application: HelloWorld

Several components are needed to build an Android application. Fortunately, the Eclipse IDE with the Android plug-in automates a lot of the work needed to create and maintain these components. We will start by using the IDE to create a project for our application. Start up Eclipse and select "File → New → Project..." from the menu bar (be sure to select "Project...," not "Java Project"). You'll see a list of project types something like Figure 1.2, Eclipse New Project menu.

Figure 1.2. Eclipse New Project menu

Attached Image

Select "Android Project" and click "Next" to get the "New Android Project" dialog box (Figure 1.3, Eclipse New Android Project dialog).

Figure 1.3. Eclipse New Android Project dialog

Attached Image

We'll use "HelloWorld" as the name for both the Project and the Application. You don't need to change the button or checkbox selections, and we'll use the package name com.oreilly.helloworld as shown. You may need to specify a target version of the Android API, if the current version of the SDK supports more than one. Select the most recent version.

Every Android application has to have at least one Activity (an executable that usually has a user interface), so let's say we're going to include an Activity called HelloWorldActivity, as shown in the dialog box. Click "Finish," and the Android Development Kit does a number of things for you, to make your life easier as a developer. In Figure 1.4, Eclipse project listing after creation of HelloWorld project, Expand the tree in the Package Explorer window to show some of the files and directories that the Android SDK created.

Figure 1.4. Eclipse project listing after creation of HelloWorld project

Attached Image

The Android SDK created a HelloWorld directory in the default Eclipse workspace for your project. It also created subdirectories for your source files (.src), references to the Android Library, assets, resources (.res), and a manifest file (AndoidManifest.xml). In each of the subdirectories it created another level of subdirectories as appropriate. Let's take a quick look at them:

Sources (under src)
  • Contains a directory structure that corresponds to the package name you gave for your application: in this case, com.oreilly.helloworld.

  • Contains a Java template for the Activity you indicated was in the application (HelloWorldActivity). A folder named "gen" contains an automatically java source file containing resource references (R.java). R.java is actually generated by the Android SDK the first time you compile your application - it contains the Java version of all the resources you define in the res directory (covered below). We'll come back to R.java later.

Android Library
This is just what it says. If you like, you can expand the android.jar tree and see the names of the modules included in the library. This is where your application will go for Android library references.

Files you want to bundle with your application. We won't have any for HelloWorld.

Resources (under res)
  • Drawable resources are any images, bitmaps, etc. that you need for your application. For HelloWorld, the Android SDK has supplied us with the default Android icon, and that's all we'll need.

  • Layout resources tell Android how to arrange items on the screen when the application runs. These resources are XML files that give you quite a bit of freedom in laying out the screen for different purposes. For HelloWorld, we'll just use the defaults generated by the Android SDK

  • Values are constants, strings, etc. available for use by your application. Keeping them outside the sources makes it easier to customize the application, such as adapting it for different languages.

Manifest (AndroidManifest.xml) This is another XML file that tells the Android build system what it needs to know to build and package your application so it can be installed on an Android phone or the emulator. This file has its own specialized editor, which we'll describe as we get to more complicated applications.

Writing HelloWorld

In the Eclipse Package Explorer window, double-click on HelloWorldActivity.java. This opens the source file of that name in the center window, ready for editing.

package com.oreilly.helloworld;

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

public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

Looking quickly at the template code that the Android SDK has provided for us, we can note several things:

  • The Android SDK has included the package reference we asked for, which is consistent with the directory structure it created.

  • It has also created a (collapsed) set of imports for the library references it knows we need.

  • It created a class definition for the Activity we said we wanted (HelloWorldActivity) including a method called OnCreate. For the moment, don't worry about the parameter passed into OnCreate. The savedInstanceState Bundle is a way of passing data between activities and storing data between times when the Activity is running. We won't need to use this for HelloWorld.

  • One special line of code has been included in OnCreate:

    setContentView (R.layout.main);

    Android uses "layouts" to define how buttons, images, text, and other UI elements on the screen are arranged, and that main.xml was the name of the default layout file that the Android SDK created for us under .res/layout. The R.java file is generated automatically and contains java references for each of the resources under .res. You will never need to edit the R.java file by hand - the Android SDK takes care of it as you add, change or delete resources.

Again in the Eclipse Package Explorer window, double-click on main.xml and you will see the default layout screen in the center window. There are two tabs at the bottom of the panel that say "Layout" and "main.xml". Click on the one that says "main.xml" to bring up the code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


Again, let's look at the key features of this template code:

  • Like any other XML file, this one starts with a reference to the XML version and encoding used.

  • LinearLayout is one of the screen layout formats provided by the Android SDK. There are several others, which can be combined hierarchically to create very complex screen layouts. For our purposes, a simple linear layout is fine. More Layout types are covered later in the book in Chapter 10, A Widget Bestiary.

    Within the LinearLayout definition:

    The code:


    identifies the XML schema being used.

    The code:


    defines an orientation, width and height for the entire scope of the layout.

  • TextView describes an area where text can be displayed and edited. It resembles the text boxes you may have encountered when programming in other graphical environments.

    Within the TextView definition:

    The code:


    define a width and height for the TextView box.

    The code:


    provides some text to display in the TextView. The actual string is defined in a separate file, res/values/strings.xml. If we open that file (again by clicking on it in the Package Explorer), we see a specialized string editor added by ADT. If you select "hello (String)" by clicking on it, you'll see the current value for that string. By a stroke of luck, the Android SDK has already included text that is almost what we wanted to display anyway. Just to show them who's boss, change the value of the String hello to say "Hello, Android!", or something else equally clever:

Save the Project either from the Eclipse File menu (File → Save) or by clicking on the diskette icon in the menu bar.

Believe it or not, we're done. We don't have to write a single line of Java to create this application.

Running HelloWorld

From the Eclipse menu bar, select Run → Run. A "Run As" dialog box will pop up. Select "Android Application" from the list, which displays the dialog shown in Figure 1.5, Eclipse Application Type selection.

Figure 1.5. Eclipse Application Type selection

Attached Image

If this is the first time you have have started an Android application, the SDK will ask you to configure an emulator, or "android virtual device" (AVD). First, a dialog appears informing you that no compatible targets were found. That is, no AVDs are configured for the Android API version and capabilities your application needs to run.

Attached Image

Click "Yes." now the configuration dialog for Android virtual devices appears.

Attached Image

Click "New." Yet another dialog appears, enabling you to name the new AVD, pick the API revision number, specify an SD card (which we won't need to for this application), and specify a "skin" from a choice of skins with a different screen sizes.

Attached Image

For this application, all you need to do is name the AVD and select the same API version as you selected when you created the application - which should be the most recent version. Still another dialog, confirming creation of the AVD will appear.

Attached Image

After clicking "OK," and closing the previous dialog, which now lists the newly created AVD, you should go back to the beginning of this section and follow the instructions for starting your application. This time, the dialog for selecting an AVD - you can configure and choose from multiple AVDs - for running your application appears.

Attached Image

Select the AVD you just created and press "Start."Soon, an emulator window will appear. The emulated phone will then go through its boot sequence, which can take a couple minutes. After the emulated Android device boots, you should see the screen shown in Figure 1.6, First try at HelloAndroid.

Figure 1.6. First try at HelloAndroid

Attached Image

Notice anything different between that screen image and the one we showed in Figure 1.1, Hello Android" Screenshot? The application prints out "Hello, Android!", or whatever you wrote into the android:text line earlier, but it also displays the title of the application as "Hello World". Let's change the title to match our creative change to the application text.

In the Package Explorer in the left panel of the Eclipse workbench, reopen the strings.xml file (the one where we found the String hello before). This will open the file in the editing window. The intent of this file is to give you a place to define strings that will be used by your application, without actually embedding them in the Java source code. The other string that's defined here is app_name. To make things consistent, change the definition of app_name to HelloAndroid as shown in Figure 1.7, HelloWorld String Editing.

Figure 1.7. HelloWorld String Editing

Attached Image

Now when we run the application, we get a screen that looks just like what we set out to do, as shown in Figure 1.1, "Hello Android" Screenshot. Congratulations! You've just created your first Android program by doing nothing more than changing the text in one line of code. There are much greater challenges ahead.

Attached File(s)

  • Attached File  ch02.pdf (967.33K)
    Number of downloads: 1640

0 Replies