Deconstructing Your Project

The Hello Android project generated by Eclipse is a fresh, clean project with no compiled binary sources. You also need to understand what happens under the hood of Eclipse at a high level. I cover this information in the next few sections.

Responding to error messages

If you were quick enough to look (or if your computer runs on the slower edge of the spectrum), you may have noticed a little red icon hovering over the Hello Android folder icon in the Package Explorer in your Eclipse window after you clicked the Finish button. If you didn't see it, you can see an example in Figure 5-6. Displaying that icon is how Eclipse lets you know that something is wrong with the project in the workspace.

By default, Eclipse is set up to let you know when an error is found within a project with this visual queue. Behind the scenes, Eclipse and the Android Development Tools are doing a few things for you:

  • Providing workspace feedback: This feedback lets you know when a problem exists with any of the projects in the workspace. You receive notification in Eclipse through icon overlays, such as the one shown in Figure 5-6. Another common icon overlay is the small yellow warning icon, which alerts you to some warnings in the contents of the project.
  • Automatically compiling: By default, Eclipse autocompiles the applications in your workspace when any files within them are saved after a change.

    image If you don't want automatic recompilation turned on, choose Projectimage Build Automatically to disable the automatic building of the project. If this option is deselected, you need to build your project manually by pressing Ctrl+B each time you change your source code.

By now you're probably wondering: How can I have an error with this project? I just created it through the New Android Project wizard, so what gives? When the project was added to your workspace, Eclipse took over, and in conjunction with the ADT, it determined that the project in the workspace had an error: The gen folder and all its contents were missing. (I cover the gen folder in “Understanding Project Structure,” later in this chapter.)

The gen folder is automatically generated by Eclipse and the ADT when the compilation takes place. As soon as the New Android Project wizard was completed, a new project was created and saved in Eclipse's workspace. Eclipse recognized this fact and said, “Hey! I see some new files in my workspace. I need to report any errors I find as well as compile the project.” Eclipse reported the errors by placing an error icon over the folder. Immediately thereafter, the compilation step took place. During the compilation step, the gen folder was created by Eclipse, and the project was successfully built. Then Eclipse recognized that the project did not have any more errors. At that time, it removed the error icon from the folder, leaving you with a clean workspace and a clean folder icon, as shown in Figure 5-5.

Figure 5-6: A project with errors in Eclipse.

image

Understanding the Build Target and Min SDK Version settings

So how does the Build Target setting differ from the Min SDK Version setting?

The build target is the operating system you use to write code. If you choose 3.0, you can write code with all the APIs in version 3.0. If you choose 1.6, you can write code only with the APIs that are in version 1.6. You can't use the Action Bar APIs in version 2.2, for example, because they weren't introduced until version 3.0. If you're targeting 3.2, though, you can write with the Action Bar APIs.

image You should know which version you want to target before you start writing your Android application. Identify which Android features you need to use to ensure that your app will function as you expect. If you're positive that you're going to need Bluetooth support, you need to target at least version 2.0. If you're not sure which versions support the features you're looking for, you can find that information on the platform-specific pages in the SDK section of http://d.android.com. The Android 3.0 platform page is at http://d.android.com/sdk/android-3.0.html.

Android operating-system (OS) versions are backward-compatible. If you target Android version 1.6, for example, your application can run on Android 3.0, 2.3.3, 2.2, …, and of course 1.6. The benefit of targeting the 1.6 framework is that your application is exposed to a much larger market share. Your app can be installed on 1.6, 2.0, 2.1, and so on, all the way up to 3.0 devices (and future versions, assuming that no breaking framework changes are introduced in future Android OS releases). Selecting an older version doesn't come without consequences, however. By targeting an older framework, you're limiting the functionality that you can access. By targeting 1.6, for example, you won't have access to the Bluetooth APIs.

The Min SDK Version setting is the minimum version of Android that the user must be running for the application to run properly on his or her device. This field isn't required to build an app, but I highly recommend that you fill it in. If you don't indicate the Min SDK Version, a default value of 1 is used, indicating that your application is compatible with all versions of Android.

image If your application is not compatible with all versions of Android (such as if it uses APIs that were introduced in version code 5 — Android 2.0), and you haven't declared the Min SDK Version, when your app is installed on a system with an SDK version code of less than 5, your application will crash at run time when it attempts to access the unavailable APIs. As best practice, always set the Min SDK Version in your application to prevent these types of crashes.

Version codes and compatibility

The Min SDK Version is also used by the Android Market (which I cover in detail in Chapter 10) to help identify which applications to show you based on which version of Android you're running. If your device is running version code 3 (Android 1.6), you would want to see the apps pertinent to your version, not version code 11 (Android 3.0) apps. The Android Market manages which apps to show to each user through the Min SDK Version setting.

If you're having trouble deciding which version to target, the current version distribution chart can help you decide. That chart is located here: http://developer.android.com/resources/dashboard/platform-versions.html.

A good rule is to analyze the distribution chart at http://developer.android.com to determine which version will give your app the best market share. The more devices you can target, the wider the audience you will have, and the more installs you have, the better your app is doing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.26.149