An Android application is developed and tested on a computer running Windows, Mac OS X, or Linux and then installed on an Android device for further testing, or for real-world use. This chapter walks you through the tools you need to have on your computer and helps you start using them. We use our sample Tic-Tac-Toe application in this chapter as well.
Android applications are mostly developed in Java. (If you're a glutton for punishment — as some of us freely admit that we are — you can develop Android applications in C or C++ instead, but we don't cover that topic in this book.) Also, if you're a Java purist, you can just use a text editor and the command line interface (CLI) to do your development.
Nowadays, however, most folks like to make their lives a little easier (or at least that's the story the software tool developers want developers to believe), so they use an integrated development environment (IDE) for their Java development. The most widely accepted IDE in the Java developer community is Eclipse. Also, the Open Handset Alliance (in particular, Google) has provided a set of tools that can be integrated into Eclipse that significantly helps with Java development for Android. These tools include the Android SDK Starter Package, the Android SDK Components, and the Eclipse plug-in for Android.
In this chapter, we show you how to install these tools in the order you need to do so. Because installing add-ons to Eclipse is often tricky, we also show you how to verify whether you have installed them correctly. Then we show you how to import and run the source code of the Tic-Tac-Toe application. If you're ready, “Read on, MacDuff!”
Your first task in setting up Java is to verify whether you have Version 1.5 or higher of the Java Development Kit (JDK). To do this, you work from the command line interface inside a cmd (Windows), Terminal (Mac), or shell (Linux) window. In order to bring up such a window interface, do as follows:
In this window, enter javac -version at the command prompt. Note: You enter javac (not java, which invokes the Java runtime) to invoke the Java compiler. If you see a message similar to the following, you're good to go:
javac version “1.6.0_22”
If your Java version starts with 1.5, that's also fine. However, if you have an earlier version, you don't have the correct JDK installed on your machine. If you get an error message, that probably means one of two things. It could mean that the JDK is installed but not in your system's path, which are the directories in which the system looks to find programs. It could also mean that the JDK isn't even installed (this is most likely to happen with only Windows and Linux; Macs come pre-installed with Java, and you only have to update the installation).
If the JDK is not on your path, you need to add it as described here: http://www.java.com/en/download/help/path.xml. Again, this problem is unlikely to occur on a Mac because Macs come pre-installed with Java. However, if the problem does occur, follow the instructions for Linux on the link provided.
If the JDK is old, or not installed, you have to download the JDK. Go to www.oracle.com/technetwork/java/javase/downloads/index.html and follow the instructions there. You might be asked to set up an account or log in, but this step is optional.
Be sure to select Development Kit during the installation process because only the Java Runtime Environment (JRE) is installed (by default) and it isn't enough.
After you successfully install the JDK, add to your PATH environment variable the bin subdirectory of the directory in which the Java software has been installed. Incidentally, and especially if you've installed Java in the past, note that after Oracle's purchase of Sun Microsystems, part of the download site and process changed — and might also change later. This information was current only at the time this book was written.
On Mac OS X, Java and the JDK are preinstalled by Apple, so you shouldn't have to do anything. If the version is too old or you want to upgrade to the latest version, choose Software Update from the Apple menu and begin the Java upgrade from there.
After you handle the necessary installations, try the verification step again to make sure that everything is up to snuff.
As we say in the introduction to this chapter, the most widely accepted IDE in the Android developer community is Eclipse. You can download Eclipse from
http://eclipse.org/downloads
Select for your platform the latest version of the Eclipse IDE for Java Developers. (At the time this book was written, the latest version was Helios.) Then complete the download of the package. It's in ZIP format, so extract the files into a suitably named directory. After you extract the files, you see something like what is shown in Figure 2-1.
To verify that Eclipse has been installed correctly, simply navigate to the installed directory, locate the Eclipse.exe program, and double-click to run it. If the program starts up, give it a location in which to store its workspaces and then open the Eclipse Workbench. If the Resource perspective (see Figure 2-2) opens, you're ready to work with Eclipse.
While you're at it, create a shortcut (an alias on the Mac) for Eclipse on your desktop so that you don't have to navigate to its location every time you need to start it up.
We now show you how to install the Eclipse components needed for Android development. These components are discussed in-depth in the following sections.
Follow these steps to download and install the Android SDK Starter Package and the Android SDK Components:
Make a note of the directory path because you will need it later.
The SDK components are installed. Note that this installation may take a while, so be patient.
You can also selectively get and install packages by selecting only the packages you really want (see Figure 2-4).
This part of the process has no separate verification step, so just hang in there for now!
Google provides an Eclipse component (known as a plug-in) for Android development. This Android Development Tools (ADT) plug-in adds powerful extensions that help you create and debug Android applications easily.
Specifically, the ADT plug-in:
For example, ADT lets you take screen shots, manage port forwarding for remote debugging, set breakpoints, and view threads and process information directly from Eclipse.
You can read more about the ADT plug-in at http://developer.android.com/guide/developing/eclipse-adt.html.
Here's how to install the ADT:
The installation continues. It takes some time, so be patient.
To easily verify that everything has been installed correctly, run a sample program on an emulator from a template that ADT provides. The next section shows you how.
In this section we show you how to verify that the development environment has been installed correctly by creating and starting an emulator and then running a sample program on it.
The first part of verifying the development environment is to create an emulator that serves as a (virtual) device on which the test application will run. Follow these steps:
The device is created and you see the Virtual Devices screen again.
The device starts running. Stay patient; starting a new device takes time.
ADT has a simple program example you can use to ensure that your development environment is set up correctly.
You must follow the Java rules for naming classes.
An Android project is created (see Figure 2-7).
Be patient (again). After a while, you see a screen similar to the one shown in Figure 2-8, with the name you gave your main activity in place of the displayed string “Hello World HelloActivity.” Note: If you get an error (for example, something like “Unable to open class file …R.java: No such file or directory,” try restarting Eclipse).
If you completed all the steps in this section, your installation was successful.
Before we discuss our Tic-Tac-Toe example, we want to walk you through the purpose of the various files created in the hierarchy of the sample project (refer to Figure 2-7):
So far, we've walked you through setting up your development environment. That was the complicated part. The rest, as they say, is downhill from here!
The next steps are to install and run the sample Tic-Tac-Toe application, both on the emulator and then on an actual Android device. To do this, first copy the Tic-Tac-Toe project from this book's companion website (www.dummies.com/go/android3sdkprogramming) into a suitable directory. Then follow these steps:
The Tic-Tac-Toe project is imported into your Eclipse workspace.
This step cleans up configuration information in your project so it can run in the Eclipse environment it is in.
Voilà! Tic-Tac-Toe appears on the emulator (see Figure 2-10). Knock yourself out playing the game!
Testing and debugging on a physical device are straightforward tasks although they require a few more configuration steps. You start by declaring your application as “debuggable” in its manifest:
Alternatively, if you prefer to edit the XML directly, add android:debuggable=”true” as an attribute to the <application> element in the AndroidManifest.xml file.
We don't go into the details because the Windows USB driver documentation at http://developer.android.com/sdk/win-usb.html is quite clear.
You can verify that your device is connected by running the command adb devices from your SDK tools/ directory. If your device is properly connected, you see your device name listed.
After declaring your application as debuggable, you install and run the program, running or debugging as usual from Eclipse. You see the Device Chooser dialog box (see Figure 2-11), which lists available emulators and connected devices. Select the device on which you want to install and run the application, and you will see the Tic-Tac-Toe splash screen appear (see Figure 2-12). That's it!
18.220.250.132