這節課教你
1.建立與Android Studio中的一個項目
2.建立使用指令行工具項目
你也應該閱讀
•管理項目
Creating an Android Project
This lesson teaches you to
1.Create a Project with Android Studio
2.Create a Project with Command Line Tools
You should also read
•Managing Projects
一個Android項目包含了所有構成你的Android應用程式的源代碼檔案。
這節課展示了如何采用Android Studio或使用SDK工具在指令行來建立一個新的項目。
注意:您應該已經安裝了Android SDK,如果你使用的是Android Studio,您還應該安裝Android Studio。如果你沒有這些,按照指導安裝了Android SDK開始這一課前。
An Android project contains all the files that comprise the source code for your Android app.
This lesson shows how to create a new project either using Android Studio or using the SDK tools from a command line.
Note: You should already have the Android SDK installed, and if you're using Android Studio, you should also have Android Studio installed. If you don't have these, follow the guide to Installing the Android SDK before you start this lesson.
Create a Project with Android Studio
--------------------------------------------------------------------------------
1.在Android Studio,建立一個新項目:◦如果你沒有一個項目打開,在歡迎螢幕上,單擊建立項目。
◦如果你有一個項目打開了,從檔案菜單,選擇建立項目。
1.In Android Studio, create a new project: ◦If you don't have a project opened, in the Welcome screen, click New Project.
◦If you have a project opened, from the File menu, select New Project.

圖1.在配置Android Studio中的一個新項目。
Figure 1. Configuring a new project in Android Studio.
2.在配置您的新項目,填補了字段如圖1所示,然後單擊下一步。
它可能會更容易,如果你使用相同的值,如圖所示遵循這些課。
◦應用程式名稱是出現給使用者的應用程式名稱。對于這個項目,使用“我的第一個應用程式。”
◦公司域提供将要附加到包名資格賽; Android Studio會記住這個限定詞為你建立的每一個新項目。
◦包名稱是項目(遵循相同的規則在Java程式設計語言命名的包)的全名。你的包名稱必須在安裝了Android系統上的所有軟體包是唯一的。您可以從應用程式的名稱或公司域名獨立編輯此值。
◦項目的位置是您的系統儲存項目檔案的目錄。
2.Under Configure your new project, fill in the fields as shown in figure 1 and click Next.
It will probably be easier to follow these lessons if you use the same values as shown.
◦Application Name is the app name that appears to users. For this project, use "My First App."
◦Company domain provides a qualifier that will be appended to the package name; Android Studio will remember this qualifier for each new project you create.
◦Package name is the fully qualified name for the project (following the same rules as those for naming packages in the Java programming language). Your package name must be unique across all packages installed on the Android system. You can Edit this value independently from the application name or the company domain.
◦Project location is the directory on your system that holds the project files.
3.在選擇外形您的應用程式将運作,檢查框的手機和平闆電腦。
3.Under Select the form factors your app will run on, check the box for Phone and Tablet.
4.對于最小的SDK,選擇API 8:Android 2.2(更新Froyo)。
所需的最低SDK的是Android的早期版本,您的應用程式支援,使用API級别表示。為了支援盡可能多的裝置盡可能,你應該将其設定為可用,讓您的應用程式,以提供其核心功能集的最低版本。如果你的應用程式中的任何功能隻能在較新版本的Android,它是不是應用程式的核心功能集的關鍵,您可以啟用在支援它(在支援不同平台版本讨論)的版本上運作,隻有當該功能。
4.For Minimum SDK, select API 8: Android 2.2 (Froyo).
The Minimum Required SDK is the earliest 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).
5.将所有其他選項(電視,磨損和玻璃)選中,然後單擊下一步。
5.Leave all of the other options (TV, Wear, and Glass) unchecked and click Next.
活動
Activities
活動是在Android架構的顯着特征之一。活動為使用者提供通路您的應用程式,并可能有很多的活動。一個應用程式通常将具有在使用者啟動應用程式,當她選擇一些内容,以檢視,例如用于另一個活動,并且當她執行該應用中的其它任務的其他活動的主要活動。看到活動的詳細資訊。
An activity is one of the distinguishing features of the Android framework. Activities provide the user with access to your app, and there may be many activities. An application will usually have a main activity for when the user launches the application, another activity for when she selects some content to view, for example, and other activities for when she performs other tasks within the app. See Activities for more information.
6.在添加一個活動項目,選擇空白活動,然後單擊下一步。
6.Under Add an activity to your project, select Blank Activity and click Next.
7.在描述新的活動項目,離開領域,因為它們并單擊Finish。
7.Under Describe the new activity for your project, leave the fields as they are and click Finish.
你的Android項目現在是一個基本的“Hello World”的應用程式,包含了一些預設的檔案。花一點時間來審查最重要的:
Your Android project is now a basic "Hello World" app that contains some default files. Take a moment to review the most important of these:
app/src/main/res/layout/activity_my.xml
這是給你添加的,當你建立 Android Studio中的項目活動的XML布局檔案。繼新項目的工作流程,Android Studio介紹該檔案同時具有文本視圖和螢幕UI的預覽。該檔案包括了一些預設設定和顯示的消息,一個TextView元素“世界,你好!”
This is the XML layout file for the activity you added when you created the project with Android Studio. Following the New Project workflow, Android Studio presents this file with both a text view and a preview of the screen UI. The file includes some default settings and a TextView element that displays the message, "Hello world!"
app/src/main/java/com.mycompany.myfirstapp/MyActivity.java
該檔案頁籤出現在Android Studio時建立項目的工作流程完成。當您選擇的檔案,你看到你建立活動的類定義。當你建構和運作應用程式,活動類開始活動,并加載布局檔案,上面寫着的“Hello World!”
A tab for this file appears in Android Studio when the New Project workflow finishes. When you select the file you see the class definition for the activity you created. When you build and run the app, the Activity class starts the activity and loads the layout file that says "Hello World!"
app/src/res/AndroidManifest.xml
清單檔案描述了應用程式的基本特征,并确定它的每一個組成部分。當你遵循這些教訓,你會再次讨論這個檔案,并給您添加更多的元件
The manifest file describes the fundamental characteristics of the app and defines each of its components. You'll revisit this file as you follow these lessons and add more components to your
app.app/build.gradle
Android Studio使用Gradle編譯和建立自己的應用程式。有對項目中的每個子產品,以及用于整個項目build.gradle檔案build.gradle檔案。通常情況下,你隻關心該子產品的build.gradle檔案,在這種情況下,應用程式或應用程式子產品。這是你的應用程式的建構依賴關系設定,包括defaultConfig設定:
•compiledSdkVersion是平台版本對您将編譯您的應用程式。預設情況下,它被設定為Android系統的最新版本在你的SDK。 (這應該是Android 4.1或更高版本;如果沒有這樣一個版本,你必須使用SDK管理器中安裝一個。)你還可以建立自己的應用程式,以支援舊版本,但這種設定到最新版本,您可以要啟用新功能和優化的應用程式為最新裝置的使用者體驗。
•applicationID是您在建立項目工作流程中指定的應用程式的完全限定包名。
•minSdkVersion是你指定的最低SDK版本的新項目工作流程中。這是您的應用程式支援Android SDK的最早版本。
•targetSdkVersion表示最高版本的Android與您已經測試您的應用程式。由于Android 新版本可用,您應該測試您的應用程式的新版本和更新此值,以比對最新的API等級,進而采取新的平台功能。欲了解更多資訊,請閱讀支援不同平台版本。
Android Studio uses Gradle to compile and build your app. There is a build.gradle file for each module of your project, as well as a build.gradle file for the entire project. Usually, you're only interested in the build.gradle file for the module, in this case the app or application module. This is where your app's build dependencies are set, including the defaultConfig settings: •compiledSdkVersion 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 this to the latest version allows you to enable new features and optimize your app for a great user experience on the latest devices.
•applicationId is the fully qualified package name for your application that you specified during the New Project workflow.
•minSdkVersion is the Minimum SDK version you specified during the New Project workflow. This is the earliest version of the Android SDK that your app supports.
•targetSdkVersion indicates the highest version of Android with which you have tested your application. 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 and thereby take advantage of new platform features. For more information, read Supporting Different Platform Versions.
看到生成項目與Gradle有關Gradle的更多資訊。
See Building Your Project with Gradle for more information about Gradle.
Note also the /res subdirectories that contain the resources for your application:
還需要注意的是包含應用程式的資源/ RES子目錄:
drawable-hdpi/
目錄中可繪制對象(如位圖)這是專為高密度(hdpi)螢幕。其他繪制目錄包含專為其他螢幕密度資産。在這裡,你會發現,當你運作預設顯示的ic_launcher.png
Directory for drawable objects (such as bitmaps) that are designed for high-density (hdpi) screens. Other drawable directories contain assets designed for other screen densities. Here you'll find the ic_launcher.png that appears when you run the default
app.layout/
目錄中定義您的應用程式的使用者界面一樣activity_my.xml,上面所讨論的,它描述了MyActivity類的基本的布局檔案。
Directory for files that define your app's user interface like activity_my.xml, discussed above, which describes a basic layout for the MyActivity class.
values/
目錄中包含資源的集合其他XML檔案,比如字元串和顔色的定義。在strings.xml中檔案定義的“Hello world!”字元串當您運作預設應用程式顯示。
為了運作應用程式,繼續下一課。...
Directory for other XML files that contain a collection of resources, such as string and color definitions. The strings.xml file defines the "Hello world!" string that displays when you run the default app.
To run the app, continue to the next lesson.
Create a Project with Command Line Tools
-------------------------------------------------- ------------------------------
如果你不使用Android Studio IDE,您可以使用SDK工具從指令行來建立項目:
If you're not using the Android Studio IDE, you can instead create your project using the SDK tools from a command line:
1.更改目錄到了Android SDK的工具/路徑。
1.Change directories into the Android SDK’s tools/ path.
2.Execute:Android的目标清單
2.Execute: android list targets
這将列印,你已經下載下傳了你的SDK可用Android 平台清單。找到對您要編譯您的應用程式的平台。記下目标的ID。我們建議您選擇可能的最高版本。你仍然可以建立自己的應用程式,以支援舊版本,但建構目标設定到最新版本,您可以優化您的應用程式的latest裝置。
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 SDK管理器工具安裝。請參閱添加SDK軟體包。
If you don't see any targets listed, you need to install some using the Android SDK Manager tool. See Adding SDK Packages.
3.Execute:Android的建立項目--target<目标ID>--name MyFirstApp\
--path<路徑到工作區>/ MyFirstApp--activity MyActivity\
--package com.example.myfirstapp
3.Execute: android create project --target <target-id> --name MyFirstApp \
--path <path-to-workspace>/MyFirstApp --activity MyActivity \
替換<目标ID>與目标清單(從以前的步驟)的ID和替換<路徑到工作區>與要儲存你的Android項目的位置。
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.
提示:添加的平台工具/以及工具/目錄到PATH環境變量。
Tip: Add the platform-tools/ as well as the tools/ directory to your PATH environment variable.
你的Android項目現在是一個基本的“Hello World”的應用程式,包含了一些預設的檔案。為了運作應用程式,繼續下一課。
Your Android project is now a basic "Hello World" app that contains some default files. To run the app, continue to the next lesson.