Teach yoursef Android appication deveopment - Part I: Creating Android products Page 1 of 7 Part of the EE Times Network A Artices Products Course TechPaper Webinars Login Register Wecome, Guest HOME DESIGN FORUMS E-LEARNING PRODUCTS Tuesday, October 19, 2010 Teach yoursef Android appication deveopment - Part I: Creating Android products Part 1 of a two-part excerpt from Sam's "Teach Yoursef Android Appication Deveopment in 24 Hours" covers a brief history of the Android patform, a quick overview of Ecipse, and creating Android projects. Android is the first compete, open, and free mobie patform. Deveopers enjoy a comprehensive software deveopment kit, with ampe toos for deveoping powerfu, feature-rich appications. The patform is open source, reying on tried-and-true open standards deveopers wi be famiiar with. And best of a, there are no costy barriers to entry for deveopers: no required fees. (A modest fee is required to pubish on third-party distribution mechanisms such as the Android Market.) Android deveopers have numerous options for distributing and commerciaizing their appications. Introducing Android To understand where Android fits in with other mobie technoogies, et's take a minute to tak about how and why this patform came about. Googe and the Open Handset Aiance In 2007, a group of handset manufacturers, wireess carriers, and software deveopers (notaby, Googe) formed the Open Handset Aiance, with the goa of deveoping the next generation of wireess patform. Unike existing patforms, this new patform woud be nonproprietary and based on open standards, which woud ead to ower deveopment costs and increased profits. Mobie software deveopers woud aso have unprecedented access to the handset features, aowing for greater innovation. As proprietary patforms such as RIM BackBerry and Appe iphone gained traction, the mobie deveopment community eagery istened for news of this potentia game-changing patform. Android Makes Its Entrance In 2008, the Open Handset Aiance announced the Android patform and aunched a beta program for deveopers. Android went through the typica revisions of a new patform. Severa prereease revisions of the Android Software Deveopment Kit (SDK) were reeased. The first Android handset (the T-Mobie G1) began shipping in ate 2008. Throughout 2009, more Android handsets and diverse types of devices powered by Android reached word markets. As of this writing, there are more than 36 Android phones avaiabe from carriers around the word. This number does not incude the numerous Android tabet and e-book readers aso avaiabe, nor the dozens of upcoming devices aready announced, nor http://www.embeddedinternetdesign.com/dhander.jhtm;jsessionid=oczrbn3kyouurqe1g...
Teach yoursef Android appication deveopment - Part I: Creating Android products Page 2 of 7 the consumer eectronics running Android. The rate of new Android devices reaching the word markets has continued to increase. In the United States, a major carriers now incude Android phones in their product ines. Googe has been a contributing member of the Open Handset Aiance from the beginning. The company hosts the Android open source project as we as the Android deveoper program at http://deveoper.android.com. This deveoper website is your go-to site for downoading the Android SDK, getting the atest patform documentation, and browsing the Android deveoper forums. Googe aso runs the most popuar service for seing Android appications to end users: the Android Market. The Android mascot is the itte green robot shown in Figure 1.1. The Android Deveoper Chaenge Googe has hosted severa contests, caed Deveoper Chaenges, to encourage deveopers to write Android appications. The first two rounds saw $10 miion in prize money awarded! Cheap and Easy Deveopment If there's one time when "cheap and easy" is a benefit, it's with mobie deveopment. Wireess appication deveopment, with its ridicuousy expensive compiers and preferentia deveoper programs, has been notoriousy expensive to break into compared to desktop deveopment. Here, Android breaks the proprietary mod. Unike with other mobie patforms, there are virtuay no costs to deveoping Android appications. The Android SDK and toos are freey avaiabe on the Android deveoper website, http://deveoper.android.com. The freey avaiabe Ecipse program has become the most popuar integrated deveopment environment (IDE) for Android appication deveopment; there is aso a powerfu pug-in avaiabe on the Android deveoper site for faciitating Android deveopment. So we've covered cheap; now et's tak about why Android deveopment is easy. Because Android appications are written in Java, deveopers wi be famiiar with many of the packages provided as part of the Android SDK, such as java.net. Deveopers wi be peased to find that the earning curve for Android is quite reasonabe. So et's get started! Famiiarizing Yoursef with Ecipse Let's begin by writing a simpe Android "Heo, Word" appication that dispays a ine of text to the user. As you do so, you wi aso be taking a tour through the Ecipse environment. Specificay, you wi earn about the features offered by the Android Deveopment Toos (ADT) pug-in for Ecipse. The ADT pug-in provides functionaity for deveoping, compiing, packaging, and depoying Android appications. Specificay, it provides the foowing: The Android project wizard, which generates a the required project fies Android-specific resource editors The Android SDK and AVD (Android Virtua Devices) Manager The Ecipse DDMS perspective for monitoring and debugging Android appications Integration with Android LogCat ogging Automated buids and appication depoyment to Android emuators and handsets Appication packaging and code signing toos for reease depoyment Instaing the Android SDK and Toos You wi find a the detais of how to insta and configure your computer for Android appication deveopment in Appendix A, "Configuring Your Android Deveopment Environment." You wi need to insta and configure Ecipse, the Android SDK, and the ADT pug-in for Ecipse. You may aso need to insta the USB drivers for any Android handsets you wi be using for deveopment. Now et's take some of these features for a spin. Creating Android Projects The Android Project Wizard creates a the required fies for an Android appication. Open Ecipse and foow these steps to create a new project: http://www.embeddedinternetdesign.com/dhander.jhtm;jsessionid=oczrbn3kyouurqe1g...
Teach yoursef Android appication deveopment - Part I: Creating Android products Page 3 of 7 1. Choose Fie, New, Android Project or cick the Android Project creator icon, which ooks ike a foder (with the etter a and a pus sign on the Ecipse toobar: Note: The first time you try to create an Android Project, you might need to choose Fie, New, Project and then seect the Android, Android Project. After you have done this once, it appears in the Ecipse project types and you can use the method described in Step 1. 2. Choose a project name. In this case, name the project Droid1. 3. Choose a ocation for the project. Because this is a new project, seect the Create New Project in Workspace radio button. Check the Use Defaut Location check box. Note: If you prefer to store your project fies in another ocation, simpy uncheck the Use Defaut Location check box and browse to the directory of your choice. 4. Seect a buid target for your appication. For most appications, you want to seect the version of Android most appropriate for the devices used by your target audience and the needs of your appication. If you wi be using the Googe add-ons (for exampe, Googe Maps), be sure to choose the Googe APIs version for your target patform. For this exampe, the Android 2.1 (API eve 7) buid target is sufficient. 5. Specify an appication name. This name is what users wi see. In this case, ca the appication Droid #1. 6. Specify a package name, foowing standard package namespace conventions for Java. Because a code in this book fas under the com.androidbook.* namespace, use the package name com.androidbook.droid1. 7. Check the Create Activity check box. This wi instruct the wizard to create a defaut aunch Activity cass for the appication. Ca your activity DroidActivity. Note: What Is an Activity? An activity is a core component of the Android patform. Each activity represents a task the appication can do, often tied to a corresponding screen in the appication user interface. The Droid #1 appication has a singe activity, caed DroidActivity, which has a singe responsibiity: to dispay a String to the user. We wi tak more about activities in Hour 3, "Buiding Android Appications." Your project settings wi ook as shown in Figure 1.2. 8. Confirm that the Min SDK Version fied is correct. This fied wi be set to the API eve of the buid target by defaut (Android 2.1 is API eve 7). If you want to support oder versions of the Android SDK, you need to change this fied. However, in this case, we can eave it as its defaut vaue. 9. Cick the Next button. 10. The Android project wizard aows you to create a test project in conjunction with your Android appication. For this exampe, a test project is unnecessary. However, you can aways add a test project ater by cicking the Android Test Project creator icon, which is to the right of the Android project wizard icon (with the etter a, etter J and etter: on the Ecipse toobar. Test projects are discussed in detai in Hour 22, "Testing Android Appications." 11. Cick the Finish button. http://www.embeddedinternetdesign.com/dhander.jhtm;jsessionid=oczrbn3kyouurqe1g...
Teach yoursef Android appication deveopment - Part I: Creating Android products Page 4 of 7 Figure 1.2. The Android Project Wizard in Ecipse Exporing the Android Project Fies You wi now see a new Android project caed Droid1 in the Ecipse Fie Exporer. In addition to inking the appropriate Android SDK jar fie, the foowing core fies and directories are created: AndroidManifest.xm - The centra configuration fie for the appication. defaut.properties - A generated buid fie used by Ecipse and the Android ADT pug-in. Do not edit this fie. /src foder - Required foder for a source code. /src/com.androidbook.droid1/droidactivity.java - Main entry point to this appication, named DroidActivity. This activity has been defined as the defaut aunch activity in the Android manifest fie. /gen/com.androidbook.droid1/r.java - A generated resource management source fie. Do not edit this fie. /assets foder - Required foder where uncompied fie resources can be incuded in the project. /res foder - Required foder where a appication resources are managed. Appication resources incude animations, drawabe graphics, ayout fies, data-ike strings and numbers, and raw fies. /res/drawabe - Appication icon graphic resources are incuded in severa different sizes. /res/ayout/main.xm - Layout fie used by DroidActivity to draw onscreen. /res/vaues/strings.xm - The path where string resources are defined. Note: You can aso add existing Android projects to Ecipse by using the Android Project Wizard. To do this, simpy seect Create Project from Existing Source instead of the defaut Create New Project in Workspace in the New Android Project diaog (refer to Figure 1.2). Severa sampe projects are provided in the /sampes directory of the Android SDK, under the specific patform they support. For exampe, the Android SDK sampe projects are found in the directory /patforms/android-xxx/sampes (where xxx is the patform version number). You can aso seect a third option: Create Project from Existing Sampe, which wi do as it says. However, make sure you choose the buid target first option to get the ist of sampe projects you can create. Editing Project Resources By defaut, the Android manifest fie resource editor is opened when you create a new Android project. If you have cicked away from this screen to check out the various project fies, simpy doube-cick the AndroidManifest.xm fie within your new project to return to the Android manifest fie resource editor (see Figure 1.3). http://www.embeddedinternetdesign.com/dhander.jhtm;jsessionid=oczrbn3kyouurqe1g...
Teach yoursef Android appication deveopment - Part I: Creating Android products Page 5 of 7 Figure 1.3 Editing an Android resource fie in Ecipse Editing the Android Manifest Fie The Android manifest fie is the centra configuration fie for an Android appication. The editor organizes the manifest information into a number of tabs: Manifest - This tab, shown in Figure 1.3, is used for genera appication-wide settings such as the package name and appication version information (used for instaation and upgrade purposes). Appication - This tab is used to define appication detais such as the name and icon the appication dispays, as we as the "guts" of the appication, such as what activities can be run (incuding the defaut aunch DroidActivity) and other functionaity and services that the appication provides. Permissions - This tab is used to define the appication's permissions. For exampe, if the appication requires the abiity to read the contacts from the phone, then it must register a Uses-Permission tag within the manifest, with the name android.permission.read_contacts. Instrumentation - This tab is used for unit testing, using the various instrumentation casses avaiabe within the Android SDK. AndroidManifest.xm - This tab provides a simpe XML editor to edit the manifest fie directy. If you switch to the AndroidManifest.xm tab, your manifest fie wi ook something ike this: Note: Because a Android resource fies, incuding the Android manifest fie, are simpy XML fies, you are aways abe to edit the raw resource fies instead of using the resource editors. You can create a new Android XML fie by cicking the Android XML creator icon on the Ecipse toobar, which ooks ike a fie (with the etter a and a pus sign: Edit the Android Manifest Fie Now et's edit the Android manifest fie. One setting you're going to want to know about is the debuggabe attribute. You wi not be abe to debug your appication unti you set this vaue, so foow these steps: http://www.embeddedinternetdesign.com/dhander.jhtm;jsessionid=oczrbn3kyouurqe1g...
Teach yoursef Android appication deveopment - Part I: Creating Android products Page 6 of 7 1. Open the AndroidManifest.xm fie in the resource editor. 2. Navigate to the Appication tab. 3. Pu down the drop-down for the debuggabe attribute and choose true. 4. Save the manifest fie. If you switch to the AndroidManifest.xm tab and ook through the XML, you wi notice that the appication tag now has the debuggabe attribute: Editing Other Resource Fies Most Android appication resources are stored under the /res subdirectory of the project. The foowing subdirectories are aso avaiabe: /drawabe - dpi, /drawabe-hdpi, /drawabe-mdpi-these subdirectories store graphics and drawabe resource fies for different screen densities and resoutions. If you browse through these directories using the Ecipse Project Exporer, you wi find the icon.png graphics fie in each one; this is your appication's icon. You' earn more about the difference between these directories in Hour 20, "Deveoping for Different Devices." /ayout - This subdirectory stores user interface ayout fies. Within this subdirectory you wi find the main.xm screen ayout fie that defines the user interface for the defaut activity. /vaues - This subdirectory organizes the various types of resources, such as text strings, coor vaues, and other primitive types. Here you find the strings.xm resource fie, which contains a the resource strings used by the appication. If you doube-cick on any of resource fies, the resource editor wi aunch. Remember, you can aways edit the XML directy. Try It Yoursef: Edit a String Resource If you inspect the main.xm ayout fie of the project, you wi notice that it dispays a simpe ayout with a singe TextView contro. This user interface contro simpy dispays a string. In this case, the string dispayed is defined in the string resource caed @string/heo. To edit the string resource caed @string/heo, using the string resource editor, foow these steps: 1. Open the strings.xm fie in the resource editor. 2. Seect the String caed heo and note the name (heo) and vaue (Heo Word, DroidActivity!) shown in the resource editor. 3. Within the Vaue fied, change the text to Heo, Dave. 4. Save the fie. If you switch to the strings.xm tab and ook through the raw XML, you wi notice that two string eements are defined within a <resources> bock: The first is the string @string/heo. The second is @string/app_name, which contains the name abe for the appication. If you ook at the Android manifest fie again, you wi see @string/app_name used in the appication configuration. We wi tak much more about project resources in Hour 4, "Managing Appication Resources." For now, et's move on to compiing and running the appication. Next in Part II: Running and debugging appications. Sams Teach Yoursef Android Appication Deveopment in 24 Hours by Lauren Darcey, Shane Conder, Pubished Jun 10, 2010 by Sams. Copyright 2010 o ISBN-10: 0-13-262969-0 o ISBN-13: 978-0-13-262969-0 To order, visit InformIT. Reprinted with the permission of Pearson Education. A materias on this site Copyright 2010 EE Times Group, a Division of United Business Media LLC A rights reserved. Privacy Statement Terms of Service http://www.embeddedinternetdesign.com/dhander.jhtm;jsessionid=oczrbn3kyouurqe1g...
Teach yoursef Android appication deveopment - Part I: Creating Android products Page 7 of 7 Contact http://www.embeddedinternetdesign.com/dhander.jhtm;jsessionid=oczrbn3kyouurqe1g...