[Andriod官方训练教程]创建你的第一个App之运行你的App
原文地址:http://developer.android.com/training/basics/firstapp/running-app.html
---------------------------------------------------------------------------------
If you followed the previous lesson to create an Android project, it includes a default set of "Hello World" source files that allow you to immediately run the app.
如果你按照上一节课中创建了一个Android项目,它将包含一个默认的“Hello World”源文件,从而允许你立即运行app。
How you run your app depends on two things: whether you have a real Android-powered device and whether you're using Eclipse. This lesson shows you how to install and run your app on a real device and on the Android emulator, and in both cases with either Eclipse or the command line tools.
怎样运行你的app取决于两件事情:你是否有一个真实的Android设备,以及你是否使用Eclipse。这节课向你展示如何在真实的设备上以及Android虚拟机上安装并运行你的app,均可使用Eclipse或命令行。
Before you run your app, you should be aware of a few directories and files in the Android project:
在运行你的app之前,你应该知道Android项目中的一些目录和文件:
AndroidManifest.xml
-
The manifest file describes the fundamental characteristics of the app and defines each of its components. You'll learn about various declarations in this file as you read more training classes.
清单文件描述了app的基本性质,并且定义了它的每一个组件。你将在下面的训练课程中了解到这个文件中更多的声明。
One of the most important elements your manifest should include is the
<uses-sdk>
element. This declares your app's compatibility with different Android versions using theandroid:minSdkVersion
andandroid:targetSdkVersion
attributes. For your first app, it should look like this:在你的清单里,其中一个最重要的元素就是
<uses-sdk>元素。它使用
android:minSdkVersion
和android:targetSdkVersion
属性,声明了你的app对于不同Android版本的兼容性。<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> ... </manifest>
You should always set the
android:targetSdkVersion
as high as possible and test your app on the corresponding platform version. For more information, read Supporting Different Platform Versions.你应该尽可能高得设置
android:targetSdkVersion的值,并且在对应的平台版本上测试你的app。更多的信息,请阅读Supporting Different Platform Versions。
src/
- Directory for your app's main source files. By default, it includes an
Activity
class that runs when your app is launched using the app icon. 你的app的主要源文件目录。默认下,它包含了一个Activity类,它在使用app图标启动时将会运行。
res/
-
Contains several sub-directories for app resources. Here are just a few:
包含了一些app resources的子目录。下面只是其中一些:
drawable-hdpi/
- 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. 为高分辨率设计的可绘对象(例如位图)的目录。其他可绘目录包含了为其他分辨率屏幕设计的元素。
layout/
- Directory for files that define your app's user interface. 定义你的app用户界面的文件目录。
values/
- Directory for other various XML files that contain a collection of resources, such as string and color definitions. 其他各种XML文件目录,包含了一个资源集合,例如字符串和颜色定义等。
When you build and run the default Android app, the default Activity
class
starts and loads a layout file that says "Hello World." The result is nothing exciting, but it's important that you understand how to run your app before you start developing.
当你建立并运行默认的Android app时,默认的Activity类将会启动,并加载一个叫"Hello World"的布局文件。这看起来并不那么令人兴奋,但这对于你在开始开发前,理解如何与运行你的app是十分重要的。
Run on a Real Device —— 在真实的设备上运行
If you have a real Android-powered device, here's how you can install and run your app:
如果你有一部真实的Android设备,你可以这样安装并运行你的app:
- Plug in your device to your development machine with a USB cable. If you're developing on Windows, you might need to install the appropriate USB driver for your device. For help installing drivers, see the OEM USB Drivers document. 使用USB连接线,将你的设备插入你的开发机器上。如果你在Windows平台下开发,你可能需要为你的设备安装合适的USB驱动。安装驱动的帮助,请参见OEM USB Drivers文档。
- Enable USB debugging on your device. 在你的设备上开启USB调试。
- On most devices running Android 3.2 or older, you can find the option under Settings > Applications > Development. 在大多数Android 3.2或更老的设备上,你可以在设置 > 应用程序> 开发下找到这个选项。
- On Android 4.0 and newer, it's in Settings > Developer options. 在Android 4.0及更高版本,它在设置
> 开发人员选项下。
Note: On Android 4.2 and newer, Developer options is hidden by default. To make it available, go toSettings > About phone and tap Build number seven times. Return to the previous screen to find Developer options.
注意:在Android 4.2及更高版本上,开发人员选项默认下是隐藏的。为了使它可用,进入设置 > 关于手机,然后点击版本号7下。返回上一个界面并找到开发人员选项。
To run the app from Eclipse:
从Eclipse中运行app:
- Open one of your project's files and click Run from the toolbar.
- In the Run as window that appears, select Android Application and click OK.
Eclipse installs the app on your connected device and starts it.
Eclipse将在你连接的设备上安装并启动它。
Or to run your app from a command line:
或者从命令行中运行你的app:
- Change directories to the root of your Android project and execute:
ant debug
- Make sure the Android SDK
platform-tools/
directory is included in yourPATH
environment variable, then execute:adb install bin/MyFirstApp-debug.apk
- On your device, locate MyFirstActivity and open it.
That's how you build and run your Android app on a device! To start developing, continue to the next lesson.
Run on the Emulator —— 在模拟器中运行
Whether you're using Eclipse or the command line, to run your app on the emulator you need to first create an Android Virtual Device (AVD). An AVD is a device configuration for the Android emulator that allows you to model different devices.
无论你使用Eclipse或命令行,为了在模拟器中运行你的app,你需要首先创建一个Android Virtual Device (AVD)。一个AVD是Android模拟器的一个设备配置,它允许你模拟不同的设备。
To create an AVD:
创建一个AVD:
- Launch the Android Virtual Device Manager: 打开 Android Virtual Device Manager:
- In Eclipse, click Android Virtual Device Manager from the toolbar. 在Eclipse里,从工具栏中单击Android Virtual Device Manager 。
- From the command line, change directories to
<sdk>/tools/
and execute: 在命令行中,改变目录到<sdk>/tools/
,并执行:android avd
- In the Android Virtual Device Manager panel, click New. 在Android Virtual Device Manager 面板,单击 New。
- Fill in the details for the AVD. Give it a name, a platform target, an SD card size, and a skin (HVGA is default). 填写AVD细节。填写它的名字,一个目标平台,一个SD和大小,以及一个皮肤(默认是HVGA)。
- Click Create AVD. 点击创建AVD
- Select the new AVD from the Android Virtual Device Manager and click Start. 从Android Virtual Device Manager中选择新的AVD并点击Start。
- After the emulator boots up, unlock the emulator screen. 在模拟器启动以后,将模拟器屏幕解锁。
To run the app from Eclipse:
从Eclipse里运行app:
- Open one of your project's files and click Run from the toolbar. 打开你的项目中的一个文件,单后点击工具栏中的Run 。
- In the Run as window that appears, select Android Application and click OK. 在出现的Run as 窗口中,选择Android Application并点击OK。
Eclipse installs the app on your AVD and starts it.
Eclipse在你的AVD上安装你的app并启动它。
Or to run your app from the command line:
或者从命令行中运行你的app:
- Change directories to the root of your Android project and execute: 将目录改变为你的Android项目的根目录,然后执行:
ant debug
- Make sure the Android SDK
platform-tools/
directory is included in yourPATH
environment variable, then execute: 确保Android SDKplatform-tools/
目录包含在你的PATH
环境变量里,然后执行:adb install bin/MyFirstApp-debug.apk
- On the emulator, locate MyFirstActivity and open it. 在模拟器里,找到 MyFirstActivity 并打开它。
That's how you build and run your Android app on the emulator! To start developing, continue to the next lesson.
这就是如何在模拟器里建立并运行你的Android app!继续开发请学习下一节课。