O'Reilly logo

Programming Android by Zigurd Mednieks, G. Blake Meike, Masumi Nakamura, Laird Dornin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 1. Your Toolkit

This chapter shows you how to install the Android software development kit (SDK) and all the related software you’re likely to need. By the end, you’ll be able to run a simple “Hello World” program on an emulator. Windows, Mac OS X, and Linux systems can all be used for Android application development. We will load the software, introduce you to the tools in the SDK, and point you to sources of example code.

Throughout this book, and especially in this chapter, we refer to instructions available on various websites for installing and updating the tools you will use for creating Android programs. The most important place to find information and links to tools is the Android Developers site:

http://developer.android.com

Our focus is on guiding you through installation, with explanations that will help you understand how the parts of Android and its developer tools fit together, even as the details of each part change.

The links cited in this book may change over time. Descriptions and updated links are posted on this book’s website. You can find a link to the website on this book’s catalog page. You may find it convenient to have the book’s website open as you read so that you can click through links on the site rather than entering the URLs printed in this book.

Installing the Android SDK and Prerequisites

Successfully installing the Android SDK requires two other software systems that are not part of the Android SDK: the Java Development Kit (JDK) and the Eclipse integrated development environment (IDE). These two systems are not delivered as part of the Android SDK because you may be using them for purposes outside of Android software development, or because they may already be installed on your system, and redundant installations of these systems can cause version clashes.

The Android SDK is compatible with a range of recent releases of the JDK and the Eclipse IDE. Installing the current release of each of these tools will usually be the right choice. The exact requirements are specified on the System Requirements page of the Android Developers site: http://developer.android.com/sdk/requirements.html.

One can use IDEs other than Eclipse in Android software development, and information on using other IDEs is provided in the Android documentation at http://developer.android.com/guide/developing/other-ide.html. We chose Eclipse as the IDE covered in this book because Eclipse supports the greatest number of Android SDK tools and other plug-ins, and Eclipse is the most widely used Java IDE, but IntelliJ IDEA is an alternative many Java coders prefer.

The Java Development Kit (JDK)

If your system has an up-to-date JDK installed, you won’t need to install it again. The JDK provides tools, such as the Java compiler, used by IDEs and SDKs for developing Java programs. The JDK also contains a Java Runtime Environment (JRE), which enables Java programs, such as Eclipse, to run on your system.

If you are using a Macintosh running a version of Mac OS X supported by the Android SDK, the JDK is already installed.

If you are using Ubuntu Linux, you can install the JDK using the package manager, through the following command:

sudo apt-get install sun-java6-jdk

Caution

If this command reports that the JDK package is not available, you may need to enable the “partner” repositories using the Synaptic Package Manager utility in the SystemAdministration menu. The “partner” repositories are listed on the Other Software tab after you choose SettingsRepositories.

This is one of the very few places in this chapter where you will see a version number, and it appears here only because it can’t be avoided. The version number of the JDK is in the package name. But, as with all other software mentioned in this chapter, you should refer to up-to-date online documentation to determine the version you will need.

If you are a Windows user, or you need to install the JDK from Oracle’s site for some other reason, you can find the JDK at http://www.oracle.com/technetwork/java/javase/downloads/index.html.

The Downloads page will automatically detect your system and offer to download the correct version. The installer you download is an executable file. Run the executable installer file to install the JDK.

To confirm that the JDK is installed correctly, issue this command from the command line (Terminal on Linux and Mac; Command Prompt on Windows):

javac -version

Caution

If the javac command is not in your PATH, you may need to add the bin directory in the JDK to your path manually.

It should display the version number corresponding to the version of the JDK you installed. If you installed revision 20 of the Java 6 JDK, the command would display:

javac 1.6.0_20

Depending on the current version of the JDK available when you read this, version numbers may differ from what you see here.

Note

If it is unclear which JRE you are running, or if you think you have the wrong JRE running on a Debian-derived Linux system, such as Ubuntu, you can use the following command to display the available JREs and select the right one:

sudo update-alternatives --config java

The Eclipse Integrated Development Environment (IDE)

Eclipse is a general-purpose technology platform. It has been applied to a variety of uses in creating IDEs for multiple languages and in creating customized IDEs for many specialized SDKs, as well as to uses outside of software development tools, such as providing a Rich Client Platform (RCP) for Lotus Notes and a few other applications.

Eclipse is usually used as an IDE for writing, testing, and debugging software, especially Java software. There are also several derivative IDEs and SDKs for various kinds of Java software development based on Eclipse. In this case, you will take a widely used Eclipse package and add a plug-in to it to make it usable for Android software development. Let’s get that Eclipse package and install it.

Eclipse can be downloaded from http://www.eclipse.org/downloads.

You will see a selection of the most commonly used Eclipse packages on this page. An Eclipse “package” is a ready-made collection of Eclipse modules that make Eclipse better suited for certain kinds of software development. Usually, Eclipse users start with one of the Eclipse packages available for download on this page and customize it with plug-ins, which is what you will do when you add the Android Development Tools (ADT) plug-in to your Eclipse installation. The System Requirements article on the Android Developers site lists three choices of Eclipse packages as a basis for an Eclipse installation for Android software development:

  • Eclipse Classic (for Eclipse 3.5 or later)

  • Eclipse IDE for Java Developers

  • Eclipse for RCP/Plug-in Developers

Any of these will work, though unless you are also developing Eclipse plug-ins, choosing either Classic or the Java Developers package (EE or Standard) makes the most sense. The authors of this book started with the Java EE Developers package (“EE” stands for Enterprise Edition), and screenshots of Eclipse used in this book reflect that choice.

The Eclipse download site will automatically determine the available system-specific downloads for your system, though you may have to choose between 32 and 64 bits to match your operating system. The file you download is an archive. To install Eclipse, open the archive and copy the eclipse folder to your home folder. The executable file for launching Eclipse on your system will be found in the folder you just extracted from the archive.

Caution

We really mean it about installing Eclipse in your home folder (or another folder you own), especially if you have multiple user accounts on your system. Do not use your system’s package manager. Your Eclipse installation is one of a wide range of possible groupings of Eclipse plug-ins. In addition, you will probably further customize your installation of Eclipse. And Eclipse plug-ins and updates are managed separately from other software in your system.

For these reasons, it is very difficult to successfully install and use Eclipse as a command available to all users on your system, even if your system can do this from its package manager. To successfully complete an installation as it is described here, you must install Eclipse in a folder managed by one user, and launch it from this location.

If you are using Ubuntu or another Linux distribution, you should not install Eclipse from your distribution’s repositories, and if it is currently installed this way, you must remove it and install Eclipse as described here. The presence of an “eclipse” package in the Ubuntu repositories is an inheritance from the Debian repositories on which Ubuntu is based. It is not a widely used approach to installing and using Eclipse, because most of the time, your distribution’s repositories will have older versions of Eclipse.

To confirm that Eclipse is correctly installed and that you have a JRE that supports Eclipse, launch the executable file in the Eclipse folder. You may want to make a shortcut to this executable file to launch Eclipse more conveniently. You should see the Welcome screen shown in Figure 1-1.

Eclipse is implemented in Java and requires a JRE. The JDK you previously installed provides a JRE. If Eclipse does not run, you should check that the JDK is correctly installed.

Welcome screen that you see the first time you run Eclipse

Figure 1-1. Welcome screen that you see the first time you run Eclipse

The Android SDK

With the JDK and Eclipse installed, you have the prerequisites for the Android SDK, and are ready to install the SDK. The Android SDK is a collection of files: libraries, executables, scripts, documentation, and so forth. Installing the SDK means downloading the version of the SDK for your platform and putting the SDK files into a folder in your home directory.

To install the SDK, download the SDK package that corresponds to your system from http://developer.android.com/sdk/index.html.

The download is an archive. Open the archive and extract the folder in the archive to your home folder.

Warning

If you are using a 64-bit version of Linux, you may need to install the ia32-libs package.

To check whether you need this package, try running the adb command (~/android-sdk-linux_*/platform-tools/adb). If your system reports that adb cannot be found (despite being right there in the platform-tools directory) it likely means that the current version of adb, and possibly other tools, will not run without the ia32-libs package installed. The command to install the ia32-libs package is:

sudo apt-get install ia32-libs

The SDK contains one or two folders for tools: one named tools and, starting in version 8 of the SDK, another called platform-tools. These folders need to be on your path, which is a list of folders your system searches for executable files when you invoke an executable from the command line. On Macintosh and Linux systems, setting the PATH environment variable is done in the .profile (Ubuntu) or .bash_profile (Mac OS X) file in your home directory. Add a line to that file that sets the PATH environment variable to include the tools directory in the SDK (individual entries in the list are separated by colons). For example, you could use the following line (but replace both instances of ~/android-sdk-ARCH with the full path to your Android SDK install):

export PATH=$PATH:~/android-sdk-ARCH/tools:~/android-sdk-ARCH/platform-tools

On Windows systems, click Startright-click Computer, and choose Properties. Then click Advanced System Settings, and click the Environment Variables button. Double-click the path system variable, and add the path to the folders by going to the end of this variable’s value (do not change anything that’s already there!) and adding the two paths to the end, separated by semicolons with no space before them. For example:

;C:\android-sdk-windows\tools;C:\android-sdk-windows\platform-tools

After you’ve edited your path on Windows, Mac, or Linux, close and reopen any Command Prompts or Terminals to pick up the new PATH setting (on Ubuntu, you may need to log out and log in unless your Terminal program is configured as a login shell).

Adding Build Targets to the SDK

Before you can build an Android application, or even create a project that would try to build an Android application, you must install one or more build targets. To do this, you will use the SDK and AVD Manager. This tool enables you to install packages in the SDK that will support multiple versions of the Android OS and multiple API levels.

Once the ADT plug-in is installed in Eclipse, which we describe in the next section, the SDK and AVD Manager can be invoked from within Eclipse. It can also be invoked from the command line, which is how we will do it here. To invoke the SDK and AVD Manager from the command line, issue this command:

android

The screenshot in Figure 1-2 shows the SDK and AVD Manager, with all the available SDK versions selected for installation.

The SDK and AVD Manager, which enables installation of Android API levels

Figure 1-2. The SDK and AVD Manager, which enables installation of Android API levels

The packages labeled “SDK platform” support building applications compatible with different Android API levels. You should install, at a minimum, the most recent (highest numbered) version, but installing all the available API levels, and all the Google API add-on packages, is a good choice if you might someday want to build applications that run on older Android versions. You should also install, at a minimum, the most recent versions of the example applications package. You must also install the Android SDK Platform-Tools package.

The Android Development Toolkit (ADT) Plug-in for Eclipse

Now that you have the SDK files installed, along with Eclipse and the JDK, there is one more critical part to install: the Android Developer Toolkit (ADT) plug-in. The ADT plug-in adds Android-specific functionality to Eclipse.

Software in the plug-in enables Eclipse to build Android applications, launch the Android emulator, connect to debugging services on the emulator, edit Android XML files, edit and compile Android Interface Definition Language (AIDL) files, create Android application packages (.apk files), and perform other Android-specific tasks.

Using the Install New Software Wizard to download and install the ADT plug-in

You start the Install New Software Wizard by selecting HelpInstall New Software (Figure 1-3). To install the ADT plug-in, type this URL into the Work With field and press Return or Enter: https://dl-ssl.google.com/android/eclipse/ (see Figure 1-4).

The Eclipse Add Site dialog

Figure 1-3. The Eclipse Add Site dialog

Note

More information on installing the ADT plug-in using the Install New Software Wizard can be found on the Android Developers site, at http://developer.android.com/sdk/eclipse-adt.html#downloading.

Eclipse documentation on this wizard can be found on the Eclipse documentation site, at http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.platform.doc.user/tasks/tasks-124.htm.

Once you have added the URL to the list of sites for acquiring new plug-ins, you will see an entry called Developer Tools listed in the Available Software list.

The Eclipse Install New Software dialog with the Android Hierarch Viewer plug-in shown as available

Figure 1-4. The Eclipse Install New Software dialog with the Android Hierarch Viewer plug-in shown as available

Select the Developer Tools item by clicking on the checkbox next to it, and click on the Next button. The next screen will ask you to accept the license for this software. After you accept and click Finish, the ADT will be installed. You will have to restart Eclipse to complete the installation.

Configuring the ADT plug-in

One more step, and you are done installing. Once you have installed the ADT plug-in, you will need to configure it. Installing the plug-in means that various parts of Eclipse now contain Android software development-specific dialogs, menu commands, and other tools, including the dialog you will now use to configure the ADT plug-in. Start the Preferences dialog using the WindowPreferences (Linux and Windows) or EclipsePreferences (Mac) menu option. Click the item labeled Android in the left pane of the Preferences dialog.

Note

The first time you visit this section of the preferences, you’ll see a dialog asking if you want to send some usage statistics to Google. Make your choice and click Proceed.

A dialog with the Android settings is displayed next. In this dialog, a text entry field labeled “SDK location” appears near the top. You must enter the path to where you put the SDK, or you can use the file browser to select the directory, as shown in Figure 1-5. Click Apply. Note that the build targets you installed, as described in Adding Build Targets to the SDK, are listed here as well.

Your Android SDK installation is now complete.

Configuring the SDK location into the Eclipse ADT plug-in using the Android Preferences dialog

Figure 1-5. Configuring the SDK location into the Eclipse ADT plug-in using the Android Preferences dialog

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required