Setting Up the Development Environment

The Android SDK is pretty flexible and integrates well with a couple of development environments. Purists might choose to go all 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 grocery list of software you'll need to download and install in the given order:

■ The Java Development Kit (JDK), version 5 or 6. I suggest going for 6.

■ The Android Software Development Kit (Android SDK).

■ Eclipse for Java Developers, version 3.4 or 3.5.

■ The Android Development Tools (ADT) plug-in for Eclipse. Let's go through the steps required to set everything up properly.

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

Setting Up the JDK

Download the JDK with one of the specified versions for your operating system. On most systems it comes in the form of an installer or package, so there shouldn't be any hurdles. Once the JDK is installed, it is advisable to 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%\bin 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 fitting 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 (e.g., c:\android-sdk on Windows or /opt/android-sdk on Linux). The SDK comes with a couple of 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%\tools 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 were 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 of these are the Google APIs that allow you to integrate Google maps in your application.

USB driver for Windows: These are 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 functionality at our disposal. For this, we first 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. It will then present you with the dialog 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.

Packages v SDK Platform Android 2.2, API 8, revision 2 [

v SDK Platform Android 2. ^ SDK Platform Android 2.

V SDK Platform Android 2.

V SDK Platform Android 1.

V SDK Platform Android 1 ^ SDK Platform Android 1 v- Samples for SDK API 8,

V Samples for SDK API 7, ? Google APIs by Google ? Google APIs by Google ? Google APIs by Google ? Google APIs by Google

1, API 7, revision 2 0.1, API 6, revision 0, API 5, revision 1 6, API 4, revision 3 .5, API 3, revision 4 .1, API 2, revision 1 revision 1 revision 1

Inc., Android API 8. Inc., Android API 7. Inc., Android API 6. Inc., Android API 5, r sfhing depends on this package

Package Description 8c License Package Description Android SDK Platform 2.2_r1 Revision 2

Dependencies

This package is a dependency for: - Google APIs by Google Inc., Android API 8, revision 2

Archive Description

Archive for Windows Size: 73 ME

SHA1 : e80ddc2f8f9929bc8d46bc579be86d43317adb57 Site hNns-vVrll-xsl nnnnle mmyflnHmiHAfinnsitnruAfinnsitnru vml

0 Accept O Reject

O Accept All

Install

Cancel

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

You can use the SDK and AVD manager at any time 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, we can move on to the next step in setting up our development environment.

Installing Eclipse

Eclipse comes in a couple of different flavors. For Android developers, I suggest using Eclipse for Java Developers version 3.6, Like 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 it's uncompressed, you can create a nice little 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 for this.

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 accompanying 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 us with a welcome screen, which we can safely ignore and close. This will leave us with the default Eclipse Java perspective. We'll get to know Eclipse a little better in a later section. For now it suffices to have it running.

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 that is 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. Given 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 will open the installation dialog. In this dialog you can choose from which source to install what plug-in. First, you have to add the plug-in repository from which the ADT plug-in is fetched. Click the Add button, and 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.

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 present you a new dialog that lists all the plug-ins and dependencies that are going to be installed. Confirm that dialog with a click on the Next button.

5. Yet another dialog will pop up, prompting you to accept the licenses of each plug-in to be installed. You should of course accept those licenses, and finally initiate the installation with a click on 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 all this dialog madness, you'll be presented with the same Eclipse window as before. The toolbar features a couple of 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 these new shiny 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 creating a new Android project. The other two buttons will create a new unit test project or Android manifest file (functionality 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 plugin where the Android SDK is located.

1. Open Window > Preferences, and select Android in the tree view in the upcoming dialog.

2. On the right side, click the Browse button to chose the root directory of your Android SDK installation.

3. Click the OK button to close the dialog, and you'll finally able to create your first Android application.

A Quick Tour of Eclipse

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

An instance of Eclipse works with a workspace that holds one or more projects. We defined a workspace at startup earlier. All new projects we create will be stored in the workspace directory, along with 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, which is a single UI component such as a source code editor, an output console, or a project explorer

■ A perspective, which is 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, synchronization with a version control repository, and so on.

Eclipse for Java Developers comes with a couple of predefined perspectives. The ones we are most interested in 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.

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

You are free to rearrange the place 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 that is presented to you or choose Other... to get a list of all views that are available.

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 the active one. 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, the toolbars may change as well. Recall that a couple of new buttons appeared in the toolbar after we installed the ADT plug-in. This is 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 (which is specific to debugging and profiling Android applications) in addition to the standard Java Debug perspective. The ADT plug-in also adds a couple of new views, including the LogCat view, which displays the live logging information of 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. I can't possibly cover all the details of developing with Eclipse, as it is such a huge beast. I therefore advise you to learn more about Eclipse via its extensive help system if the need arises.

0 0

Post a comment