当前位置: 代码迷 >> Android >> Building Your First App-Creating an Android Project
  详细解决方案

Building Your First App-Creating an Android Project

热度:69   发布时间:2016-05-01 11:30:34.0
Building Your First App---Creating an Android Project

原文地址:http://developer.android.com/training/basics/firstapp/creating-project.html

-------------------------------------------------

An Android project contains all the files that comprise the source code for your Android app. The Android SDK tools make it easy to start a new Android project with a set of default project directories and files.

一个Android项目包含了所有你的Android应用代码所需的所有文件。Android SDK工具使用一系列默认的项目目录和文件使得开始一个Android项目更加容易。

This lesson shows how to create a new project either using Eclipse (with the ADT plugin) or using the SDK tools from a command line.

这节课向你展示如何使用Eclipse(安装了ADT插件)或在命令行中使用SDK工具创建一个新的项目。

Note: You should already have the Android SDK installed, and if you're using Eclipse, you should also have the ADT plugininstalled (version 21.0.0 or higher). If you don't have these, follow the guide to Installing the Android SDK before you start this lesson.

注意:你应该已经安装了Android SDK,并且如果你使用Eclipse,你同样应该安装了ADT插件(版本21.0.0及更高)。如果没有这些,在开始这节课之前你应该首先阅读Installing the Android SDK向导。

Create a Project with Eclipse —— 使用Eclipse创建一个项目


  1. Click New  in the toolbar.
  2. In the window that appears, open the Android folder, select Android Application Project, and click Next.

    Figure 1. The New Android App Project wizard in Eclipse.

  3. Fill in the form that appears:
    • Application Name is the app name that appears to users. For this project, use "My First App." 应用的名称

    • Project Name is the name of your project directory and the name visible in Eclipse. 在Eclipse里可见的项目名称
    • Package Name is the package namespace for your app (following the same rules as packages in the Java programming language). Your package name must be unique across all packages installed on the Android system. For this reason, it's generally best if you use a name that begins 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 Google Play using the "com.example" namespace. 你的app的包命名空间(和Java编程中的包规则相同)。你的包名必须在Android系统安装的所有包中是唯一的。因此,你最好使用以你的组织或出版商的反响域名为开头来命名。对于这个项目,你可以使用像"com.example.myfirstapp."这样的名字。但是,你不可以在Google Play上使用"com.example"命名空间来发布你的app。
    • Minimum Required SDK is the lowest version of Android that your app supports, indicated using the API level. To support as many devices as possible, you should set this to the lowest 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 and it's not critical to the app's core feature set, you can enable the feature only when running on the versions that support it (as discussed in Supporting Different Platform Versions). Leave this set to the default value for this project. 你的app支持的最老Android版本,使用API level表明。为了尽可能多的支持不同设备,你应该将该值设置为允许你的app提供它的核心功能的最老版本号。如果你的app中的任何特性只能运行在较新的Android版本中,并且不是app中的核心功能,你可以仅在支持它的系统版本上才开启它(正如在Supporting Different Platform Versions讨论的一样)。对于这个项目请保持默认值不变。
    • Target SDK indicates the highest version of Android (also using the API level) with which you have tested with your application. 表明了你的应用测试过的最高的Android系统(同样使用API level)。

      As new versions of Android become available, you should test your app on the new version and update this value to match the latest API level in order to take advantage of new platform features.

      当新的Android系统可用时,你应该在新的版本上测试你的app,然后更新该值来匹配最新的API等级,以便利用新平台的特性。

    • Compile With is the platform version against which you will compile your app. By default, this is set to the latest version 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 using the SDK Manager). You can still build your app to support older versions, but setting the build target to the latest version allows you to enable new features and optimize your app for a great user experience on the latest devices. 你编译你的app时所用的平台版本。默认下,该值被设为你的SDK上可用的最新安卓版本。(它应该是Android 4.1或更高;如果你没有这样一个可用的版本,你必须使用SDK Manager来安装一个)。你可以仍然使用你的app来支持更老的版本,但是将该值设置为最新版本允许你使用新的特性,并且可以在最新的设备上拥有更高的用户体验来优化你的app。
    • Theme specifies the Android UI style to apply for your app. You can leave this alone. 指定了Android UI风格。你可以先不管这个。

    Click Next.

  4. On the next screen to configure the project, leave the default selections and click Next.
  5. The next screen can help you create a launcher icon for your app. 下一个对话框可以帮助你为你的app创建一个启动图标。

    You can customize an icon in several ways and the tool generates an icon for all screen densities. Before you publish your app, you should be sure your icon meets the specifications defined in the Iconography design guide.

    你可以用很多方法自定义一个图标,然后工具会为所有大小的屏幕生成一个图标。在你发布你的app之前,你应该确保你的图标符合Iconography设计指南中定义的规格。

    Click Next.

  6. Now you can select an activity template from which to begin building your app.

    For this project, select BlankActivity and click Next.

  7. Leave all the details for the activity in their default state and click Finish.

Your Android project is now set up with some default files and you’re ready to begin building the app. Continue to the next lesson.

Create a Project with Command Line Tools —— 使用命令行创建一个项目


If you're not using the Eclipse IDE with the ADT plugin, you can instead create your project using the SDK tools from a command line:

如果你没有使用安装了ADT插件的Eclipse IDE,你可以在命令行中使用SDK来创建你的项目:

  1. Change directories into the Android SDK’s tools/ path. 将目录改变为Android SDK的 tools/路径。
  2. Execute:
    android list targets

    This prints a list of the available Android platforms that you’ve downloaded for your SDK. Find the platform against which you want to compile your app. Make a note of the target id. We recommend that you select the highest version possible. You can still build your app to support older versions, but setting the build target to the latest version allows you to optimize your app for the latest devices.

    这会打印出你已经下载的可用Android平台的列表。找打你想要编译你的app使用的平台。记住目标id。我们建议你尽可能选择最高版本。你可以仍然建立你的app来支持更高的版本,但是设置为最新版本允许你为最新的设备优化你的app。

    If you don't see any targets listed, you need to install some using the Android SDK Manager tool. See Adding Platforms and Packages.

    如果你可由看到任何目标列表,你需要使用Android SDK Manager工具安装一些。参见Adding Platforms and Packages。

  3. Execute:
    android create project --target <target-id> --name MyFirstApp \--path <path-to-workspace>/MyFirstApp --activity MainActivity \--package com.example.myfirstapp

    Replace <target-id> with an id from the list of targets (from the previous step) and replace <path-to-workspace> with the location in which you want to save your Android projects.

    <target-id>替换为目标列表中的一个id(在上一步中得到),然后将<path-to-workspace>替换为你想要保存你的Android项目的位置。

Your Android project is now set up with several default configurations and you’re ready to begin building the app. Continue to the next lesson.

Tip: Add the platform-tools/ as well as the tools/ directory to your PATH environment variable.

提示:向你的PATH环境变量中添加platform-tools/tools/目录。

  相关解决方案