This chapter kicks things off by explaining how to set up your environment to compile hybrid (C/Java) games. This includes the latest versions of the development IDE (Eclipse) and the Android SDK, plus the Native Devlopment Kit (NDK)which are the tools required to build powerful Android games. This information is critical if you wish to learn how to combine the elegant object-oriented features of Java with the raw power of C for maximum performance, and it is required when to build all the native game engines featured in later chapters.
The following software is assumed to be already installed on your desktop:
In the next section we'll go through the process of setting up your machine step by step.
There are a few steps to be completed before we can get to the nitty gritty stuff of building games for Android. I summarize the steps as follows:
Download the Android SDK Starter Package (r14) for windows from http://developer.android.com/sdk/index.html
and unzip it to a working folder such as C:eclipse-SDK
TIP: Try to keep the SDK, NDK, and Eclipse in the same work folder such as C:eclipse-SDK. I find this helpful when working on multiple projects at the same time. Thus my development folder C:eclipse-SDK contains the subfolders: android-sdk-windows (for the SDK), android-ndk-r6b (for the NDK), and eclipse (for Eclipse 3.7). Now let's configure our Eclipse environment.
You are ready to get your IDE up and running with the Android development kit. Let's go through the installation of the Android 4 SDK (available from http://developer.android.com/sdk/index.html
) over Eclipse 3.7 (Indigo, available from http://www.eclipse.org
).
Help
Check for Updates
, as shown in Figure 1–1.
Figure 1–1. Choosing Check for Updates from the Eclipse 3.7 workbench's Help menu
https://dl-ssl.google.com/android/eclipse
for the location, as shown in Figure 1–3.
Figure 1–3. Adding the Android site
Figure 1–4. The Available Software Sites Preferences window shows the recently added Android site.
Figure 1–5. Available Software window with the Android plug-in selected
Figure 1–6. Software license agreement from the installation wizard
Window
Preferences
to open the workbench Preferences window, and select the Android option from the left navigation tree. In the Preferences section, set the location of your SDK, as shown in Figure 1–7. Make sure all the build targets are shown. Then click Apply.
Figure 1–7. Workbench Preferences window showing Android options
Figure 1–8. New Project wizard showing the Android options after final configuration
Our Eclipse is ready for use. Now we must install the NDK.
The NDK is the critical component to create OpenGL games. It provides all the tools (compilers, libraries, and header files) to build apps that access the device natively.
NOTE: The NDK site is a very helpful resource to find step-by-step instructions, API descriptions, changes, and all things related to naïve development. It is a must for all C/C++ developers. http://developer.android.com/sdk/ndk/index.html
The NDK installation requires two simple steps: downloading the NDK and installing Cygwin.
Download and unzip the latest NDK from http://developer.android.com/sdk/ndk/index.html
into your work folder (in my case C:eclipse-SDK).
Android is built on top of Linux which mixes with windows just like oil and water. Cygwin (version 1.7.9-1) is a tool that provides a Linux look and feel environment for Windows. It is necessary to run the NDK compilation scripts, and it is required if you are doing any type of native development in Windows.
NOTE:Cygwin is not required for native development in Linux.
To Install Cywin, download and run the installer (setup.exe) from the Cywin site available at http://www.cygwin.com/
. Follow the wizard instructions. After the installer completes you should see the Cygwin icon in your desktop. Double click it and test by changing to your work folder (type cd /cygdrive/c/eclipse-SDK, see Figure 1–9).
The first step in creating our native app is to create an Android Virtual Device (AVD) we can use to test it. However, if you have a real device such as a phone or tablet you can skip this section and jump to Configure a Real Device.
NOTE: I would encourage you to test all of your code in a real device. For performance reasons it is the best way to do it. AVDs are notoriously slow and lack many advanced features such as a robust implementation of OpenGL. I work in a laptop and running an OpenGL app in the emulator is painfully slow and full of missing API calls.
With version 1.5 and later of the SDK, Google introduced the concept of virtual devices (AVDs). An AVD is simply a set of configuration attributes applied to an emulator image that allows the developer to target a specific version of the SDK. Let's take a look at the Android toolbar in Eclipse (see Figure 1–10).
Figure 1–10. Android toolbar in Eclipse
Press the green robot icon with the white down arrow (see Figure 1–10) to start the AVD Manager shown in Figure 1–11.
Figure 1–11. The AVD manager
The AVD manager provides everything you need to create:
Figure 1–11 shows that we have not created any AVDs. Simply press the New button and lets create a device to target Android 4.0 (for tablets, see Figure 1–12).
Figure 1–12. The new AVD dialog
In the new device dialog enter the device name. In the target box select the latest API level 12 (tablets). The next step is important. Create an SD Card that can be used to store game data and other assets (I chose a size of 200 MB but you can enter the value that best fits your needs). Then press Create AVD.
TIP: You can create as many devices in as many versions or resolutions as you wish. This is helpful for testing in multiple API versions or screen sizes.
The device is now ready for use, as seen in Figure 1–13.
Figure 1–13. AVD manager with brand new tablet device
Our tablet emulator is ready for use. Select the device in the dialog from Figure 1–13 and press Start. The emulator should boot up and after a while you will be able to start playing with your tablet (see Figure 1–14).
Figure 1–14. Tablet emulator (API level 14)
Personally, I think this is the best way to develop games and apps that use OpenGL. A real device gives many advantages the emulator doesn't, namely, Speed and API reliability. The only caveat is that the emulator gives you the chance to test multiple screen sizes and API versions. Before Eclipse can recognize your device you need to install a USB driver required for communications.
More on THE USB DRIVER
To install the driver in your windows host:
c:eclipse-SDKandroid-sdk-windowsextrasgoogleusb_driver.
)TIP: The USB driver can be downloaded or upgraded from the Android AVD Manager: Select Available Packages
Google Inc
Google USB Driver
and press Install Selected as shown in Figure 1–15.
Figure 1–15. Installing the USB driver from the AVD Manager
At this point, your environment should be configured and ready for development.
Congratulations! You have taken the first step toward your mastery of Android game development. In this chapter, you learned how to set up your system to compile hybrid games, which included the following tasks:
This chapter provided the foundation to compile the gamesdescribed throughout this book. In Chapter 2, you will learn how to write and compile a basic native program—a simple shared library—and call the shared library within a Java application.
18.191.52.96