Beginning Android: Developing Apps with Eclipse
Lesson 7: Develop Apps with Eclipse
Although it’s not too demanding to develop simple Android apps (e.g., Lesson 3’s W2A app) at the command line, you will probably find it tedious to develop more complex apps in this environment. For this reason, you might prefer to use the Eclipse Integrated Development Environment (IDE).
Lesson 7 completes this course by teaching you how to use Eclipse to develop W2A. First, you learn how to install this IDE, and then learn how to install the ADT Plugin, which simplifies Android app development and testing. You can then learn how to create and run W2A in this environment.
According to Google’s system requirements, you must install an Eclipse 3.6 (Helios) or greater package that includes the Java Development Tools (JDT) plugin. Google recommends that you install one of the following packages:
- Eclipse IDE for Java Developers
- Eclipse Classic
- Eclipse IDE for Java EE Developers
The eclipse.org website makes several IDE packages that meet different requirements available for download. I chose to download Eclipse Classic 3.7.1 for the Windows XP platform.
Assuming that you are also running Windows, point your browser to the Eclipse Classic 3.7.1 web page (http://www.eclipse.org/downloads/packages/eclipse-classic-371/indigosr1), and then perform the following steps to download and install this package:
- Select the appropriate distribution file for your platform by clicking one of the links in the Download Links box on the right side of this page. For example, I clicked Windows 32-bit.
- Click a download link, select a download mirror, and save the distribution file to your harddrive. For example, I saved eclipse-SDK-3.7.1-win32.zip to my hard drive.
- Unarchive the distribution file and move the eclipse home directory to a convenient location. For example, I moved eclipse to my C:Program Files directory.
- Create a desktop shortcut to the eclipse application located in the eclipse home directory. This shortcut makes it convenient to launch the Eclipse IDE.
Start Eclipse. Figure 20 reveals a splash screen identifying this IDE. Furthermore it reveals the Workspace Launcher dialog box, which lets you choose a workspace folder for storing the current session’s Eclipse projects.
Note: An Eclipse workspace is a disk location that’s associated with a workbench instance, where a workbench is a runtime instance of the Eclipse IDE. The workspace contains files, folders, and other resources that comprise your projects.
Click OK to close Workspace Launcher. Eclipse responds by performing some initialization, and then it presents its main window, as shown in Figure 21.
The main window is divided into a menubar, a toolbar, a workbench area (initially presenting a Welcome tab), and a statusbar.
Install the ADT Plugin
The Android Development Tools (ADT) Plugin is a special Eclipse plugin that facilitates app development. Although you can develop Android apps without the ADT Plugin, you’d have to work directly with Android’s command-line tools. You’ll find that this plugin makes it much faster and easier to create, debug, and otherwise develop these apps.
The ADT Plugin offers the following benefits:
- It gives you access to other Android development tools from inside the Eclipse IDE. For example, ADT lets you access the many capabilities of the Dalvik Debug Monitor Server (DDMS) tool, which lets you take screenshots, set breakpoints, manage port-forwarding, and view process and thread information directly from Eclipse.
- Its New Project Wizard helps you quickly create and setup all of the fundamental files that you’ll need for a new Android app.
- It automates and simplifies the app-building task.
- Its Android code editor helps you write valid XML for your Android manifest and resource files.
- It facilitates exporting your project as a signed APK, which can then be distributed to users.
Assuming that the Eclipse IDE is running, complete the following steps to install the latest ADT Plugin revision:
- Select Install New Software from the Help menu.
- Click the Add button on the resulting Install dialog box’s Available Software pane.
- On the resulting Add Repository dialog box, enter Android Plugin into the Name textfield and https://dl-ssl.google.com/android/eclipse/ into the Location textfield. Click the OK button.
- After a few moments, you should see Developer Tools in the tree-based list near the middle of the Install dialog box. Expand this node and check the checkbox to its left. You’ll discover that the Android DDMS, Android Development Tools, Android Hierarchy Viewer, and Android Traceview subnodes are also checked. Click the Next button.
- After a few moments, an Install Details pane appears and signifies that these four items will be installed. Click Next, and the Review Licenses pane appears. Read and accept the terms of these license agreements and click the Finish button.
- An Installing Software dialog box appears, giving you the option of installing these items in the background. If you encounter a Security Warning dialog box, click the OK button to close this dialog box and continue.
- Eclipse presents a Software Updates dialog box that prompts you to restart this IDE. Click the Restart Now button. After Eclipse restarts, you will probably see an Android SDK Verification dialog box stating that Eclipse cannot find the C:android-sdk-windows folder. This is not surprising, because the folder is named C:android-sdk (as specified earlier in this course when installing the SDK). Ignore this error; you will fix it shortly. Click the OK button to dismiss this dialog box.
- Select Preferences from the Window menu to open the Preferences dialog box. For Mac OS X, select Preferences from the Eclipse menu.
- Select the Android node in the left panel of this dialog box.
- Click the Browse button beside the SDK Location textfield, and then locate your downloaded SDK’s home directory (e.g., C:android-sdk) via the resulting Browse For Folder dialog box.
- After closing Browse For Folder, click Apply followed by OK on the Preferences dialog box to complete installation.
Develop and Run W2A with Eclipse/ADT
Now that you’ve installed Eclipse and the ADT Plugin, let’s revisit W2A by developing this app in this new environment. We’ll begin by creating a W2A project, which is accomplished by performing the following steps:
- Start Eclipse if not running.
- Select New from the File menu and Project from the resulting popup menu.
- On the resulting New Project dialog box, expand the Android node in the wizard tree (if not expanded), choose the Android Project branch below this node, and click the Next button.
- On the resulting New Android Project dialog box, enter W2A into the Project Name textfield – the entered name identifies the current workspace directory in which this project is stored. Continuing, select the Create new project in workspace radio button if not selected, and click the Next button.
- On the resulting Select Build Target pane, ensure that Android 2.3.3 is checked under Build Target and click Next.
- On the resulting Application Info pane, enter Welcome to Android into the Application name textfield, replacing the default W2A value. This human-readable title appears as the app’s icon’s caption on the Android device’s app launcher screen. (You could leave W2A alone if desired.) Next, enter ca.tutortutor.w2a into the Package Name textfield, check the Create Activity checkbox if not checked, enter W2A as the name of the app’s starting activity in the textfield that appears beside this checkbox, and make sure that 10 (Android 2.3.3) is current in the Minimum SDK drop-down listbox – it probably will be current. Click the Finish button.
Eclipse creates a W2A directory within its workspace directory. W2A contains the following subdirectories and files:
- .settings: This directory contains org.eclipse.jdt.core.prefs. This file records project-specific settings.
- assets: This directory stores an unstructured hierarchy of files. Anything placed in this directory can be retrieved by an app via a raw byte stream.
- bin: This directory stores the created APK file.
- gen: This directory stores the generated R.java file within a subdirectory structure that reflects the package hierarchy (catutortutorw2a).
- res: This directory stores app resources in various subdirectories.
- src: This directory stores app source code according to a package hierarchy.
- .classpath: This file stores the project’s classpath information so that external libraries on which the project depends can be found.
- .project: This file contains project-oriented information such as the name of project and what builders it contains.
- AndroidManifest.xml: This file contains W2A’s manifest information.
- proguard.cfg: This file contains configuration data for the ProGuard obfuscation tool.
- project.properties: This file contains project settings.
Close the Welcome tab. Eclipse responds by presenting Figure 22’s workbench.
The workbench is organized around a menubar, a toolbar, several windows such as Package Explorer and Outline, a blank area that’s reserved for editor windows, and a statusbar.
Package Explorer appears on the left and presents an expandable list of nodes that identify the current workspace’s projects. To learn how Eclipse organizes the W2A project, click the “+” icon on the W2A node’s left. Figure 23 reveals an expanded project hierarchy.
Expand the src node followed by the resulting ca.tutortutor.w2a node. Double-click the resulting W2A.java node and you’ll see Figure 24’s editor window.
Replace Figure 24’s skeletal source code with the contents of Listing 1 (see Lesson 3). Then select Run or Run Last Launched from the menubar’s Run menu (Run appears instead of Run Last Launched if the project has not previously run). On the resulting Run As dialog box (which appears the first time you run the project), select Android Application and click OK. The W2A project is built before it runs.
Figure 25 shows you what you probably will see, especially if you’re running on Windows XP.
Unfortunately, software is often poorly written and leads to program crashes. In this case, the culprit is probably a buggy ADT Plugin.
After some research and experimentation, I was able to solve this problem. I had to change the name of Android’s home directory from android-sdk to android, update my PATH environment variable to reflect this change, and disable the snapshot setting in MyAVD. I accomplished this last task by using AVD Manager to delete MyAVD, recreate MyAVD without enabling the snapshot, and start MyAVD (making sure to check the Wipe user data checkbox on the Launch Options dialog box). I then closed the emulator, started Eclipse, and attempted to run W2A. This time, I was greeted by what you see in Figure 26.
The following review questions help you test your mastery of Lesson 7’s material:
- What Eclipse packages does Google recommend for developing Android apps?
- What benefits does the ADT Plugin provide?