Beginning Android: Developing Apps with Eclipse

Share this article

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.

Installing Eclipse

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.


Figure 20: Check the checkbox to avoid being prompted each time you start Eclipse.

Click OK to close Workspace Launcher. Eclipse responds by performing some initialization, and then it presents its main window, as shown in Figure 21.

Figure 21: The Eclipse 3.7.1 IDE main window initially presents a Welcome tab.

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:

  1. Select Install New Software from the Help menu.
  2. Click the Add button on the resulting Install dialog box’s Available Software pane.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. Select Preferences from the Window menu to open the Preferences dialog box. For Mac OS X, select Preferences from the Eclipse menu.
  9. Select the Android node in the left panel of this dialog box.
  10. 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.
  11. 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:

  1. Start Eclipse if not running.
  2. Select New from the File menu and Project from the resulting popup menu.
  3. 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.
  4. 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.
  5. On the resulting Select Build Target pane, ensure that Android 2.3.3 is checked under Build Target and click Next.
  6. 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.

Figure 22: The workbench features the solitary W2A project.

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.

Figure 23: W2A’s hierarchy is organized into src, gen, Android 2.3.3, assets, bin, and res directories; as well as AndroidManifest.xml and two configuration files.

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.

Figure 24: The editor for the W2A.java source file displays skeletal source code.

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.

Figure 25: Attempting to run W2A may produce an unexpected crash.

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.

Figure 26: W2A is successfully installed and run on the emulator in the context of Eclipse.

Review

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?
Jeff FriesenJeff Friesen
View Author

Jeff Friesen is a freelance tutor and software developer with an emphasis on Java and mobile technologies. In addition to writing Java and Android books for Apress, Jeff has written numerous articles on Java and other technologies for SitePoint, InformIT, JavaWorld, java.net, and DevSource.

androidAndroid TutorialsTutorials
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week