Game Development Reference
In-Depth Information
it to the latest SDK version, which is 16 at the time of writing). If we want our game to run on
devices with SDK version 3 (Android 1.5) as well, we specify the
minSdkVersion
, as before, in
the manifest file. Of course, we must be careful not to use any APIs that are not available in the
lower version, at least on a 1.5 device. On a device with a higher version, we can use the newer
APIs as well.
The preceding configuration is usually fine for most games (unless you can't provide a
separate fallback code path for the higher-version APIs, in which case you will want to set the
minSdkVersion
attribute to the minimum SDK version you actually support).
Android Game Project Setup in Eight Easy Steps
Let's now combine all of the preceding information and develop a simple step-by-step method
to create a new Android game project in Eclipse. Here's what we want from our project:
ï?®
It should be able to use the latest SDK version's features while maintaining
compatibility with the lowest SDK version that some devices still run. That
means that we want to support Android 1.5 and above.
ï?®
It should be installed to the SD card when possible so that we don't fill up
the internal storage of the device.
ï?®
It should have a single main activity that will handle all configuration
changes itself so that it doesn't get destroyed when the hardware keyboard
is revealed or when the orientation of the device is changed.
ï?®
The activity should be fixed to either portrait or landscape mode.
ï?®
It should allow us to access the SD card.
ï?®
It should allow us to get a hold of a wake lock.
These are some easy goals to achieve with the information you just acquired. Here are the steps:
1.
Create a new Android project in Eclipse by opening the New Android
Project wizard, as described in Chapter 2.
Once the project is created, open the
AndroidManifest.xml
file.
2.
3.
To make Android install the game on the SD card when available, add
the
installLocation
attribute to the
<manifest>
element, and set it to
preferExternal
.
To fix the orientation of the activity, add the
screenOrientation
attribute
to the
<activity>
element, and specify the orientation you want
(portrait
or
landscape
).
4.
To tell Android that we want to handle the
keyboard
,
keyboardHidden
, and
orientation
configuration changes, set the
configChanges
attribute of
the
<activity>
element to
keyboard|keyboardHidden|orientation
.
5.
Add two
<uses-permission>
elements to the
<manifest>
element, and
specify the
name
attributes
android.permission.WRITE_EXTERNALSTORAGE
and
android.permission.WAKE_LOCK
.
6.