转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9822431
Building Your First App
DEPENDENCIESAND PREREQUISITES
·ADT Plugin20.0.0 or higher (if you're usingEclipse)
Welcome toAndroid application development!
This classteaches you how to build your first Android app. You’ll learn how to create anAndroid project and run a debuggable version of the app. You'll also learn somefundamentals of Android app design, including how to build a simple userinterface and handle user input.
Before you startthis class, be sure you have your development environment set up. You need to:
1.Download the Android SDK.
2.Install the ADT plugin for Eclipse (if you’ll use theEclipse IDE).
3.Download the latest SDK tools and platforms using the SDKManager.
If you haven'talready done these tasks, start by downloading theAndroid SDKandfollowing the install steps. Once you've finished the setup, you're ready tobegin this class.
This class usesa tutorial format that incrementally builds a small Android app that teachesyou some fundamental concepts about Android development, so it's important thatyou follow each step.
建立你的第一个应用程序
依赖性和先决条件
·android sdk
·ADT插件20 0 0或更高(如果您使用Eclipse)
欢迎来到Android应用程序开发!
本教程会教你如何建立你的第一个Android应用程序。您将学习如何创建一个Android项目并调试应用程序,你还将学习一些Android应用程序设计的基础,包括如何构建一个简单的用户界面和处理用户输入。
在你开始本教程之前,请确保你已经搭建好开发环境。你需要:
1.下载Android SDK。
2.Eclipse的ADT插件安装 (如果你使用的是Eclipse IDE)。
3.下载最新的SDK工具和平台使用SDK管理器
如果您尚未完成这些任务,开始下载android sdk并完成安装步骤。如果你完成了设置,准备开始本课程的学习。
本课程将通过使用一个小型的android应用,教你一些基本概念和安卓开发过程,所以遵循每一步的操作都至关重要。
开始第一课;
Creating an Android Project
THIS LESSONTEACHES YOU TO
1. Create a Project with Eclipse
2. Create a Project with Command Line Tools
YOU SHOULD ALSOREAD
An Android projectcontains all the files that comprise the source code for your Android app. TheAndroid SDK tools make it easy to start a new Android project with a set ofdefault project directories and files.
This lesson showshow to create a new project either using Eclipse (with the ADT plugin) or usingthe SDK tools from a command line.
Note:You shouldalready have the Android SDK installed, and if you're using Eclipse, you shouldalso have theADT plugininstalled (version 21.0.0 or higher). If you don't have these, follow theguide toInstalling the Android SDKbefore you start this lesson.
Create a Project with Eclipse
1.ClickNewinthe toolbar.
2.In the window thatappears, open theAndroidfolder,selectAndroid Application Project,and clickNext.
Figure 1.The New Android App Project wizard in Eclipse.
3.Fill in the formthat appears:
oApplicationNameis the app name thatappears to users. For this project, use "My First App."
oProjectNameis the name of your projectdirectory and the name visible in Eclipse.
oPackageNameis the package namespacefor your app (following the same rules as packages in the Java programminglanguage). Your package name must be unique across all packages installed onthe Android system. For this reason, it's generally best if you use a name thatbegins with the reverse domain name of your organization or publisher entity.For this project, you can use something like"com.example.myfirstapp." However, you cannot publish your app on GooglePlay using the "com.example" namespace.
oMinimumRequired SDKis the lowest version ofAndroid that your app supports, indicated using theAPI level. To support as many devices as possible, you should set this to thelowest version available that allows your app to provide its core feature set.If any feature of your app is possible only on newer versions of Android andit's not critical to the app's core feature set, you can enable the featureonly when running on the versions that support it (as discussed inSupporting Different Platform Versions). Leave this set to the default value for this project.
oTargetSDKindicates the highestversion of Android (also using theAPI level) with which you have tested with your application.
As new versions of Android become available, you shouldtest your app on the new version and update this value to match the latest APIlevel in order to take advantage of new platform features.
oCompileWithis the platform versionagainst which you will compile your app. By default, this is set to the latestversion of Android available in your SDK. (It should be Android 4.1 or greater;if you don't have such a version available, you must install one usingtheSDK Manager). You can still build your app to support older versions, but setting thebuild target to the latest version allows you to enable new features andoptimize your app for a great user experience on the latest devices.
oThemespecifies the Android UI style to apply for yourapp. You can leave this alone.
ClickNext.
4.On the next screento configure the project, leave the default selections and clickNext.
5.The next screencan help you create a launcher icon for your app.
You can customize an icon in several ways and the toolgenerates an icon for all screen densities. Before you publish your app, youshould be sure your icon meets the specifications defined in theIconographydesign guide.
ClickNext.
6.Now you can selectan activity template from which to begin building your app.
For this project, selectBlankActivityandclickNext.
7.Leave all thedetails for the activity in their default state and clickFinish.
Your Androidproject is now set up with some default files and you’re ready to beginbuilding the app. Continue to thenext lesson.
Create a Project with Command Line Tools
If you're notusing the Eclipse IDE with the ADT plugin, you can instead create your projectusing the SDK tools from a command line:
1.Change directoriesinto the Android SDK’stools/path.
2.Execute:
android list targets
This prints a list of the available Android platformsthat you’ve downloaded for your SDK. Find the platform against which you wantto compile your app. Make a note of the target id. We recommend that you selectthe highest version possible. You can still build your app to support olderversions, but setting the build target to the latest version allows you tooptimize your app for the latest devices.
If you don't see any targets listed, you need to installsome using the Android SDK Manager tool. SeeAdding Platforms and Packages.
3.Execute:
4. android create project --target<target-id> --name MyFirstApp \
5. --path<path-to-workspace>/MyFirstApp --activity MainActivity \
6. --package com.example.myfirstapp
Replace<target-id>with an id from the list of targets (from the previous step) andreplace<path-to-workspace>with the location in which you want to save yourAndroid projects.
Your Androidproject is now set up with several default configurations and you’re ready tobegin building the app. Continue to thenext lesson.
Tip:Addtheplatform-tools/as well as thetools/directory to yourPATHenvironmentvariable.
创建一个Android项目
这节课教你
2. 使用命令行工具创建项目
你也应该阅读
·管理项目
Android项目包含的所有文件,包括你的Android应用程序的源代码。AndroidSDK的工具,可以让构建Android项目与默认项目目录和文件变的更便捷和简单。
本课将说明如何使用Eclipse ADT插件或使用SDK命令行工具创建一个新的项目。
注意:你应该已经安装了Android SDK,如果你使用Eclipse,你应该也有安装ADT插件(21.0.0或更高版本)。如果你没有这些,按照安装指南安装Android SDK,然后再开始本课学习。
使用Eclipse创建一个项目
1.点击“新建”在工具栏。
2.在出现的窗口中,打开Android的文件夹,选择“Android Application,并单击”下一步“。
图1:新的Android应用程序在Eclipse项目向导。
3.填写的形式出现:
oApplication Name给用户展示的应用程序的名称。对于这个项目,使用“My First App”
oProject Name您的项目在Eclipse中可见的目录和名称。
oPackage Name是包的命名空间,为您的应用程序(在Java编程语言遵循相同的包规则)。你的包的名称必须是在Android系统上安装的所有软件包中唯一的。出于这个原因,如果您使用与您的组织或出版实体的反向域名作为包名通常是最好的。对于这个项目,你可以使用例如“com.example.myfirstapp”但是,您不能使用 “com.example”作为命名空间来发布您的应用程序。
oMinimumRequired SDK您的应用程序支持的AndroidSDK是最低版本,表明API的版本高低。为了尽可能的支持多的设备,你应该设定这个最低版本为可以让您的应用程序提供其核心功能的最低版本。如果您的应用程序的核心功能集在新版本的Android和其他版本都支持,当在不同版本可以支持时可以使用该功能(如支持不同的平台版本)。将此设定为这个项目的默认值。
oTarget SDK表明您测试您的应用程序的Android(也使用API级别)的最高版本。
随着新版本Android的使用,您应该在新版本上测试应用程序来更新此值,以符合最新的API级别,以充分利用新的平台功能。
oCompile With编译您的应用程序的平台版本。默认情况下,此设置为您的Android SDK提供的最新版本。(应该是Android 4.1或更高版本,如果你没有这样的版本,您必须安装一个使用SDK管理器)。您也可以建立你的应用程序来支持旧版本,但设置构建目标到最新版本可以让用户体验最新设备的功能和您对应用程序的优化。
oTheme指定申请您的应用程序的Android UI风格。你可以忽略这一项。
单击“下一步”。
4.在下一屏幕配置项目,保留默认选项,并单击“下一步”。
5.下一个屏幕,可以帮助您创建一个启动你的应用程序的图标。
您可以自定义图标在几个方面,该工具会生成一个图标,所有的屏幕密度。在您发布您的应用程序之前,你应该确保你的图标符合设计规范。
单击“下一步”。
6.现在您可以选择构建应用程序的动态模板。
对于这个项目,选择BlankActivity,并单击“下一步”。
7.保留所有为默认,并单击Finish。
现在你的Android项目生成了一些默认的文件,准备好开始构建你的应用程序。继续下一课。
使用命令行工具创建项目
如果你不使用Eclipse IDE ADT插件,可以改为使用SDK命令行工具来创建你的项目:
1.改变当前目录到Android SDK的tools/路径。
2.执行:
Android listtargets
打印你已经下载的SDK可用的Android平台列表。找到编译你的应用程序的平台。请记下平台的id。我们建议您选择可能的最高版本。您也可以建立支持旧版本的应用程序,但设置构建目标到最新版本,可以优化你的应用程序。
如果你没有看到任何平台列表,你需要使用Android SDK管理器安装一些工具。请参阅添加平台和软件包。
3.执行:
android create project --target <target-id> --name MyFirstApp \
--path <path-to-workspace>/MyFirstApp --activity MainActivity \
--package com.example.myfirstapp
用目标平台的id(前面的步骤)更换<target-id>,并更换<path-to-workspace>为要保存你的Android项目的位置。
现在你的Android项目设置了几个默认的配置,你准备好开始构建应用程序。继续下一课。