Android官方文档翻译 四 1.2Running Your App
Running Your App
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 element. This declares your app’s compatibility with different Android versions using the android:minSdkVersion and android:targetSdkVersion attributes. For your first app, it should look like this:
你的清单文件中应该包含的最重要的元素之一就是元素。这个使用 android:minSdkVersion 和 android:targetSdkVersion属性来声明你的app对于不同Android版本的兼容性。对于你的第一个app,它应该看起来像以下这样:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> ... </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资源的几个子目录。以下列取了几个:
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.
drawable对象(就如位图对象一样)的目录,它是为高密度(hdpi)的屏幕设置的。其它的drawable目录包含的资源是位其它密度的屏幕设置的。
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文件目录,它包含许多资源,比如string(字符)和color(颜色)的定义。
当你创建好并且运行了默认的Android app后,默认的Activity类会开始运行并且加载一个显示“Hello World”的layout文件。这个结果可能没有什么令人兴奋的,但是在你开始开发之前,它对于你了解如何运行你的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数据线把你的Android设备连接到你开发所用的机器上。如果你在Windows上进行开发,你可能需要安装适合你的Android设备的USB驱动。如果你对安装设备不太了解,请看OEM USB Drivers文档。
Enable USB debugging on your device.
在你的Android设备上打开USB调试模式
- On most devices running Android 3.2 or older, you can find the option under Settings > Applications > Development.
在大多数搭载Android 3.2或者更低版本的Android设备上,在 设置 > 应用程序 > 开发 中你可以找到此选项
- On most devices running Android 3.2 or older, you can find the option under Settings > Applications > Development.
- 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 to Settings > 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.
打开一个项目文件,在工具栏上点击 Run。
In the Run as window that appears, select Android Application and click OK.
在运行后出现的窗口中,选择你用来编译的Android设备,然后点击OK。
Eclipse installs the app on your connected device and starts it.
Eclipse将在你已经连接好的设备上安装app并且运行。
Or to run your app from a 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 your PATH environment variable, then execute:
确保你的PATH环境变量中包含 Android SDK platform-tools/ 目录,然后执行:
adb install bin/MyFirstApp-debug.apk
On your device, locate MyFirstActivity and open it.
在你的设备上,找到 MyFirstActivity ,然后打开它。
That’s how you build and run your Android app on a device! To start developing, continue to the next lesson.
这就是如何创建并且在Android设备上运行你的Android app。要开始开发,继续下一节课程吧。
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模拟设备(AVD)。AVD是一个Android模拟器的设备配置,它可以让你去模拟不同的设备。
To create an AVD:
创建一个AVD
Launch the Android Virtual Device Manager:
运行Android 虚拟设备管理器
In Eclipse, click Android Virtual Device Manager from the toolbar.
在Eclipse中,在工具栏里点击 Android Virtual Device Manager。
*. From the command line, change directories to /tools/ and execute:
在命令行里,改变目录到 /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 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.
在运行后出现的窗口中,选择你用来编译的Android设备,然后点击OK。
Eclipse installs the app on your connected device and starts it.
Eclipse将在你已经连接好的设备上安装app并且运行。
Or to run your app from a 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 your PATH environment variable, then execute:
确保你的PATH环境变量中包含 Android SDK platform-tools/ 目录,然后执行:
adb install bin/MyFirstApp-debug.apk
On your device, locate MyFirstActivity and open it.
在你的设备上,找到 MyFirstActivity ,然后打开它。
That’s how you build and run your Android app on a device! To start developing, continue to the next lesson.
这就是如何创建并且在Android设备上运行你的Android app。要开始开发,继续下一节课程吧。
这是我自己翻译的,如果您发现其中有重要翻译错误,敬请指出。万分感谢!