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:
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.
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.
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.
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.
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.
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.
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.
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.
Figure 2–3. Adding a repository
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.
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.
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.
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:
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.
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 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 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.
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.