Building for Multiple Versions

First, we'll try running the "Hello, Android" program from Section 1.2, Creating Your First Program, on page 23 in the 1.6 emulator. From the menu, select Run > Run Configurations. Locate the configuration for

HelloAndroid and select it, or create a new Android Application configuration if it doesn't exist. Click the Target tab, and set Deployment Target Selection Mode to Manual. Then click the Run button.

Eclipse will show a dialog asking you to choose a running Android device. You may notice a red X next to the emulator name—ignore it. Select the device called em16, and then click OK. The following error will appear in the Console view:

ERROR: Application requires API version 8. Device API version is 4 (Android 1.6). Launch canceled!

Note: If you get a warning that says "Application does not specify an API level requirement," then you need to specify a minimum SDK version (covered in a moment). If you don't get an error and the application launches in the em16 emulator, it means you've already applied the change in this section. And finally, if Eclipse launches a new emulator window, then you probably have your target selection mode set to Automatic. Change it to Manual, and try again.

The error occurs because we specified version 2.2 as the target version of Android when we created the project. Unless we change something, it won't run at all on older versions. On those devices, it won't even appear in the Android Market. But I happen to know that this program will work just fine on any version of Android. So, how do we tell Android that?

Easy: In the Android manifest, just set your target SDK version to one number and your minimum SDK version to another number. This lets you target a new version of Android but have it still work on phones that have an older version.

To do this, edit the AndroidManifest.xml file, and change the line that says the following:

<uses-sdk android:minSdkVersion="8" /> to read as follows:

<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />

If you don't have this line, then add it right before the </manifest> tag.

This tells Android that your program is built for Android 2.2 (API level 8) but will work fine on Android 1.5 (API level 3). Save the file, and try running the project again. It should work this time without any errors. If you get a warning about "Manifest min SDK version (3) is lower than project target API level (8)," just ignore it.

Figure 13.1: The touch example crashes on Android 1.6.

Unfortunately, for some programs, just saying that we support version 3 doesn't make it so. We'll see an example of that next.

0 0

Post a comment