Testing Your Development Environment
The best way to make sure you configured your development environment correctly is to take an existing Android application and run it.You can do this easily by using one of the sample applications provided as part of the Android SDK in the /samples subdirectory.
Within the Android SDK sample applications, you can find a classic game called Snake.
To build and run the Snake application,
The best way to make sure you configured your development environment correctly is to take an existing Android application and run it.You can do this easily by using one of the sample applications provided as part of the Android SDK in the /samples subdirectory.
Within the Android SDK sample applications, you can find a classic game called Snake.
To build and run the Snake application,
you must create a new Android project in your Eclipse workspace, create an appropriate Android Virtual Device (AVD) profile, and configure a launch configuration for that project.After you have everything set up correctly,you can build the application and run it on the Android emulator or an Android device.
Adding the Snake Application to a Project in Your Eclipse Workspace The first thing you need to do is add the Snake project to your Eclipse workspace.To do this, follow these steps:
1. Choose File,New, Project.
2. Choose Android,Android Project Wizard (see Figure 3.1).
3. Change the Contents to Create Project from Existing Source.
4. Browse to your Android samples directory.
5. Choose the Snake directory. All the project fields should be filled in for you from the Manifest file (see Figure 3.2).You might want to set the Build Target to whatever version of Android your test device is running.
6. Choose Finish.You now see the Snake project files in your workspace (see Figure 3.3).
Warning
Occasionally Eclipse shows the error “Project ‘Snake’ is missing required source folder:‘gen’” when you’re adding an existing project to the workspace. If this happens, simply navigate to the project file called R.java under the /gen directory and delete it. The R.java file is automatically regenerated and the error should disappear.
Creating an Android Virtual Device (AVD) for Your Snake Project
The next step is to create an AVD that describes what type of device you want to emulate when running the Snake application.This AVD profile describes what type of device you want the emulator to simulate, including which Android platform to support.You can specify different screen sizes and orientations, and you can specify whether the emulator has an SD card and, if it does, what capacity the card is.
Testing Your Development Environment
Occasionally Eclipse shows the error “Project ‘Snake’ is missing required source folder:‘gen’” when you’re adding an existing project to the workspace. If this happens, simply navigate to the project file called R.java under the /gen directory and delete it. The R.java file is automatically regenerated and the error should disappear.
Creating an Android Virtual Device (AVD) for Your Snake Project
The next step is to create an AVD that describes what type of device you want to emulate when running the Snake application.This AVD profile describes what type of device you want the emulator to simulate, including which Android platform to support.You can specify different screen sizes and orientations, and you can specify whether the emulator has an SD card and, if it does, what capacity the card is.
Testing Your Development Environment
Figure 3.2 The Snake project details. Figure 3.3 The Snake project files.
For the purposes of this example, an AVD for the default installation of Android 2.2 suffices. Here are the steps to create a basic AVD:
1. Launch the Android Virtual Device Manager from within Eclipse by clicking the little Android icon with the downward arrow on the toolbar. If you cannot find the icon, you can also launch the manager through the Window menu of Eclipse.
2. On the Virtual Devices menu, click the New button.
3. Choose a name for your AVD. Because we are going to take all the defaults, give this AVD a name of Android_Vanilla2.2.
4. Choose a build target.We want a basic Android 2.2 device, so choose Android 2.2 from the drop-down menu.
5. Choose an SD card capacity.This can be in kilobytes or megabytes and takes up space on your hard drive. Choose something reasonable, such as 1 gigabyte (1024M). If you’re low on drive space or you know you won’t need to test external storage to the SD card, you can use a much smaller value, such as 64 megabytes.
6. Choose a skin.This option controls the different resolutions of the emulator. In this case, use the WVGA800 screen.This skin most directly correlates to the popular Android handsets, such as the HTC Nexus One and the Evo 4G, both of which are currently sitting on my desk.
Your project settings will look like Figure 3.4.
7. Click the Create AVD button, and wait for the operation to complete.
8. Click Finish. Because the AVD manager formats the memory allocated for SD card images, creating AVDs with SD cards could take a few moments.
Creating a Launch Configuration for Your Snake Project
Next, you must create a launch configuration in Eclipse to configure under what circumstances the Snake application builds and launches.The launch configuration is where you configure the emulator options to use and the entry point for your application.
Figure 3.4 Creating a new AVD in Eclipse.
You can create Run Configurations and Debug Configurations separately, each with different options.These configurations are created under the Run menu in Eclipse (Run,Run Configurations and Run, Debug Configurations).
Follow these steps to create a basic Run Configuration for the Snake application:
1. Choose Run, Run Configurations (or right-click the Project and choose Run As).
2. Double-click Android Application.
3. Name your Run Configuration SnakeRunConfiguration (see Figure 3.5).
4. Choose the project by clicking the Browse button and choosing the Snake project.
5. Switch to the Target tab and, from the preferred AVD list, choose the Android_Vanilla2.2 AVD created earlier, as shown in Figure 3.5.
You can set other options on the Target and Common tabs, but for now we are leaving the defaults as they are.
Running the Snake Application in the Android Emulator Now you can run the Snake application using the following steps:
1. Choose the Run As icon drop-down menu on the toolbar (the green circle with
the triangle).
2. Pull the drop-down menu and choose the SnakeRunConfiguration you created.
3. The Android emulator starts up; this might take a moment.
Figure 3.5 The Snake project launch configuration,Target tab with the AVD selected.
4. If necessary, swipe the screen from left to right to unlock the emulator, as shown in
Figure 3.6.
5. The Snake application now starts, as shown in Figure 3.7.
You can interact with the Snake application through the emulator and play the game.You
can also launch the Snake application from the Application drawer at any time by clicking
on its application icon.
This comment has been removed by the author.
ReplyDelete