原文地址: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 the android: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/
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/
layout/
values/
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是十分重要的。
If you have a real Android-powered device, here's how you can install and run your app:
如果你有一部真实的Android设备,你可以这样安装并运行你的app:
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:
Eclipse installs the app on your connected device and starts it.
Eclipse将在你连接的设备上安装并启动它。
Or to run your app from a command line:
或者从命令行中运行你的app:
ant debug
platform-tools/
directory is included in your PATH
environment variable, then execute: adb install bin/MyFirstApp-debug.apk
That's how you build and run your Android app on a device! To start developing, continue to the next lesson.
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模拟器的一个设备配置,它允许你模拟不同的设备。
Figure 1. The AVD Manager showing a few virtual devices.
To create an AVD:
创建一个AVD:
<sdk>/tools/
and execute: 在命令行中,改变目录到<sdk>/tools/
,并执行: android avd
To run the app from Eclipse:
从Eclipse里运行app:
Eclipse installs the app on your AVD and starts it.
Eclipse在你的AVD上安装你的app并启动它。
Or to run your app from the command line:
或者从命令行中运行你的app:
ant debug
platform-tools/
directory is included in your PATH
environment variable, then execute: 确保Android SDK platform-tools/
目录包含在你的 PATH
环境变量里,然后执行: adb install bin/MyFirstApp-debug.apk
That's how you build and run your Android app on the emulator! To start developing, continue to the next lesson.
这就是如何在模拟器里建立并运行你的Android app!继续开发请学习下一节课。