Setting Up the Development Environment

The Android SDK is flexible, and it integrates well with several development environments. Purists might choose to go hard core with command-line tools. We want things to be a little bit more comfortable, though, so we'll go for the simpler, more visual route using an IDE (integrated development environment).

Here's the list of software you'll need to download and install in the given order:

  1. The Java Development Kit (JDK), version 5 or 6. We suggest using 6.
  2. The Android Software Development Kit (Android SDK).
  3. Eclipse for Java Developers, version 3.4 or newer.
  4. The Android Development Tools (ADT) plug-in for Eclipse.

Let's go through the steps required to set up everything properly.

NOTE: As the Web is a moving target, we don't provide URLs here. Fire up your favorite search engine and find the appropriate places to get the items listed above.

Setting Up the JDK

Download the JDK with one of the specified versions for your operating system. On most systems, the JDK comes in an installer or package, so there shouldn't be any hurdles. Once you have installed the JDK, you should add a new environment variable called JDK_HOME pointing to the root directory of the JDK installation. Additionally, you should add the $JDK_HOME/bin (%JDK_HOME%in on Windows) directory to your PATH environment variable.

Setting Up the Android SDK

The Android SDK is also available for the three mainstream desktop operating systems. Choose the one for your platform and download it. The SDK comes in the form of a ZIP or tar gzip file. Just uncompress it to a convenient folder (for example, c:android-sdk on Windows or /opt/android-sdk on Linux). The SDK comes with several command-line utilities located in the tools/ folder. Create an environment variable called ANDROID_HOME pointing to the root directory of the SDK installation, and add $ANDROID_HOME/tools (%ANDROID_HOME% ools on Windows) to your PATH environment variable. This way you can easily invoke the command-line tools from a shell later on if the need arises.

After performing the preceding steps, you'll have a bare-bones installation that consists of the basic command-line tools needed to create, compile, and deploy Android projects, as well as the SDK and AVD manager, a tool for installing SDK components and creating virtual devices used by the emulator. These tools alone are not sufficient to start developing, so you need to install additional components. That's where the SDK and AVD manager comes in. The manager is a package manager, much like the package management tools you find on Linux. The manager allows you to install the following types of components:

Android platforms: For every official Android release, there's a platform component for the SDK that includes the runtime libraries, a system image used by the emulator, and any version-specific tools.

SDK add-ons: Add-ons are usually external libraries and tools that are not specific to a platform. Some examples are the Google APIs that allow you to integrate Google maps in your application.

USB driver for Windows: This driver is necessary for running and debugging your application on a physical device on Windows. On Mac OS X and Linux, you don't need a special driver.

Samples: For each platform, there's also a set of platform-specific samples. These are great resources for seeing how to achieve specific goals with the Android runtime library.

Documentation: This is a local copy of the documentation for the latest Android framework API.

Being the greedy developers we are, we want to install all of these components to have the full set of this functionality at our disposal. Thus, first we have to start the SDK and AVD manager. On Windows, there's an executable called SDK manager.exe in the root directory of the SDK. On Linux and Mac OS X, you simply start the script android in the tools directory of the SDK.

Upon first startup, the SDK and AVD manager will connect to the package server and fetch a list of available packages. The manager will then present you with the dialog shown in Figure 2–1, which allows you to install individual packages. Simply check Accept All, click the Install button, and make yourself a nice cup of tea or coffee. The manager will take a while to install all the packages.

images

Figure 2–1. First contact with the SDK and AVD manager

You can use the SDK and AVD manager at anytime to update components or install new ones. The manager is also used to create new AVDs, which will be necessary later on when we start running and debugging our applications on the emulator.

Once the installation process is finished, you can move on to the next step in setting up your development environment.

Installing Eclipse

Eclipse comes in several different flavors. For Android developers, we suggest using Eclipse for Java Developers version 3.6. Similar to the Android SDK, Eclipse comes in the form of a ZIP or tar gzip package. Simply extract it to a folder of your choice. Once the package is uncompressed, you can create a shortcut on your desktop to the eclipse executable in the root directory of your Eclipse installation.

The first time you start Eclipse, you will be prompted to specify a workspace directory. Figure 2–2 shows you the dialog.

images

Figure 2–2. Choosing a workspace

A workspace is Eclipse's notion of a folder containing a set of projects. Whether you use a single workspace for all your projects or multiple workspaces that group just a few projects is completely up to you. The sample projects that accompany this book are all organized in a single workspace, which you could specify in this dialog. For now, we'll simply create an empty workspace somewhere.

Eclipse will then greet you with a welcome screen, which you can safely ignore and close. This will leave you with the default Eclipse Java perspective. You'll get to know Eclipse a little better in a later section. For now, having it running is sufficient.

Installing the ADT Eclipse Plug-In

The last piece in our setup puzzle is installing the ADT Eclipse plug-in. Eclipse is based on a plug-in architecture used to extend its capabilities by third-party plug-ins. The ADT plug-in marries the tools found in the Android SDK with the powers of Eclipse. With this combination, we can completely forget about invoking all the command-line Android SDK tools; the ADT plug-in integrates them transparently into our Eclipse workflow.

Installing plug-ins for Eclipse can be done either manually, by dropping the contents of a plug-in ZIP file into the plug-ins folder of Eclipse, or via the Eclipse plug-in manager integrated with Eclipse. Here we'll choose the second route.

  1. To install a new plug-in, go to Help ( Install New Software…, which opens the installation dialog. In this dialog, you can then choose the source from which to install a plug-in. First, you have to add the plug-in repository from the ADT plug-in that is fetched. Click the Add button. You will be presented with the dialog depicted in Figure 2–3.
  2. In the first text field, you can enter the name of the repository; something like “ADT repository” will do. The second text field specifies the URL of the repository. For the ADT plug-in, this field should be https://dl-ssl.google.com/android/eclipse/. Note that this URL might be different for newer versions, so check the ADT plug-in site for an up-to-date link.
    images

    Figure 2–3. Adding a repository

  3. After you've confirmed the dialog, you'll be brought back to the installation dialog, which should now be fetching the list of available plug-ins in the repository. Check the Developer Tools check box and click the Next button.
  4. Eclipse will now calculate all the necessary dependencies, and then it will present you a new dialog that lists all the plug-ins and dependencies that are going to be installed. Confirm by clicking the Next button.
  5. Another dialog will pop up prompting you to accept the license for each plug-in to be installed. You should, of course, accept those licenses and, finally, initiate the installation by clicking the Finish button.

    NOTE: During the installation, you will be asked to confirm the installation of unsigned software. Don't worry, the plug-ins simply do not have a verified signature. Agree to the installation to continue the process.

  6. Finally, Eclipse will ask you whether it should restart to apply the changes. You can opt for a full restart or for applying the changes without a restart. To play it safe, choose Restart Now, which will restart Eclipse as expected.

After Eclipse restarts, you'll be presented with the same Eclipse window as before. The toolbar features several new buttons specific to Android, which allow you to start the SDK and AVD manager directly from within Eclipse as well as create new Android projects. Figure 2–4 shows the new toolbar buttons.

images

Figure 2–4. ADT toolbar buttons

The first button on the left allows you to open the AVD and SDK manager. The next button is a shortcut to create a new Android project. The other two buttons will create a new unit test project or Android manifest file (functionality that we won't use in this book).

As one last step in finishing the installation of the ADT plug-in, you have to tell the plug-in where the Android SDK is located.

  1. Open Window images Preferences, and select Android in the tree view in the dialog that appears.
  2. On the right side, click the Browse button to choose the root directory of your Android SDK installation.
  3. Click the OK button to close the dialog. Now you'll be able to create your first Android application.

A Quick Tour of Eclipse

Eclipse is an open source IDE you can use to develop applications written in various languages. Usually, Eclipse is used in connection with Java development. Given Eclipse's plug-in architecture, many extensions have been created, so it is also possible to develop pure C/C++, Scala, or Python projects as well. The possibilities are endless; even plug-ins to write LaTeX projects exist, for example—something that only slightly resembles your usual code development tasks.

An instance of Eclipse works with a workspace that holds one or more projects. Previously, we defined a workspace at startup. All new projects you create will be stored in the workspace directory, along with a configuration that defines the look of Eclipse when using the workspace, among other things.

The user interface (UI) of Eclipse revolves around two concepts:

  • A view, a single UI component such as a source code editor, an output console, or a project explorer.
  • A perspective, a set of specific views that you'll most likely need for a specific development task, such as editing and browsing source code, debugging, profiling, synchronizing with a version control repository, and so on.

Eclipse for Java Developers comes with several predefined perspectives. The ones in which we are most interested are called Java and Debug. The Java perspective is the one shown in Figure 2–5. It features the Package Explorer view on the left side, a source-editing view in the middle (it's empty, as we didn't open a source file yet), a Task List view to the right, an Outline view, and a tabbed view that contains subviews called Problems view, Javadoc view, and Declaration view.

images

Figure 2–5. Eclipse in action—the Java perspective

You are free to rearrange the location of any view within a perspective via drag and drop. You can also resize views. Additionally, you can add and remove views to and from a perspective. To add a view, go to Window images Show View, and either select one from the list presented or choose Other… to get a list of all available views.

To switch to another perspective, you can go to Window images Open Perspective and choose the one you want. A faster way to switch between already open perspectives is given to you in the top-left corner of Eclipse. There you will see which perspectives are already open and which perspective is active. In Figure 2–5, notice that the Java perspective is open and active. It's the only currently open perspective. Once you open additional perspectives, they will also show up in that part of the UI.

The toolbars shown in Figure 2–5 are also just views. Depending on the perspective you are in at the time, the toolbars may change as well. Recall that several new buttons appeared in the toolbar after we installed the ADT plug-in. This is a common behavior of plug-ins: they will, in general, add new views and perspectives. In the case of the ADT plug-in, we can now also access a perspective called DDMS (Dalvik Debugging Monitor Server, which is specific to debugging and profiling Android applications) in addition to the standard Java Debug perspective. The ADT plug-in also adds several new views, including the LogCat view, which displays the live logging information about any attached device or emulator.

Once you get comfortable with the perspective and view concepts, Eclipse is a lot less intimidating. In the following subsections, we will explore some of the perspectives and views we'll use to write Android games. We can't possibly cover all the details of developing with Eclipse, as it is such a huge beast. We therefore advise you to learn more about Eclipse via its extensive help system if the need arises.

Helpful Eclipse Shortcuts

Every new IDE requires some time to learn and become accustomed to. After using Eclipse for many years, we have found the following shortcuts speed up software development significantly. These shortcuts use Windows terms, so Mac OS X users should substitute Command and Option where appropriate:

  • Ctr+Shift+G with the cursor on a function or field will perform a workspace search for all references to the function or field. For instance, if you want to see where a certain function is called, just click to move the cursor onto the function and press Ctrl+Shift+G.
  • F3 with the cursor on a calling in to function will follow that call and bring you to the source code that declares and defines the function. Use this hotkey in combination with Ctrl+Shift+G for easy Java source code navigation.
  • Ctr+Space autocompletes the function or field name you are currently typing. Start typing and press the shortcut after you have entered a few characters. When there are multiple possibilities, a box will appear.
  • Ctr+Z is undo.
  • Ctr+X cuts.
  • Ctr+C copies.
  • Ctr+V pastes.
  • Ctr+F11 runs the application.
  • F11 debugs the application.
  • Ctr+Shift+O organizes the Java imports of the current source file.
  • Ctr+Shift+F formats the current source file.
  • Ctr+Shift+T jumps to any Java class.
  • Ctr+Shift+R jumps to any resource file; that is, an image, a text file, and so on.

There are many more useful features in Eclipse, but mastering these basic keyboard shortcuts can significantly speed up your game development and make life in Eclipse just a little better. Eclipse is also very configurable. Any of these keyboard shortcuts can be reassigned to different keys in the Preferences.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset