Android开发第1-2课:运行你的app

本课将会教你:

  1. 在实体设备上运行app
  2. 在模拟器上运行app

如果你已经在第1课中创建了一个工程,它包含了一个默认的  "Hello World" 源文件,允许你立即运行app。

你如何运行你的程序,决定于两件事情:你有一个真实的Android设备还是你正在使用Eclipse。本课将会教你如何在真实android设备上安装你的app,以及如何在模拟器上安装你的app。有两种方法,分别是通过Eclipse和命令行的方式。

在你运行你的app前,你应该注意到一些目录和文件在Android工程中,如下所示。

AndroidManifest.xml
The manifest file describes the fundamental characteristics of the app and defines each ofits components. You'll learn about various declarations in this file as you read more training classes.

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:minSdkVersionandandroid:targetSdkVersionattributes. For your first app, it should look like this:

<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:targetSdkVersionas high as possible and test your app on the corresponding platform version. For more information,readSupporting DifferentPlatform 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.
res/
Contains several sub-directories for app resources. Here are just a few:
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.
values/
Directory for other various XML files that contain a collection of resources, such asstring and color definitions.

When you build and run the default Android app, the default Activityclass starts and loads a layout file that says "Hello World." The result is nothing exciting, but it'simportant that you understand how to run your app before you start developing.

Run on a Real Device


If you have a real Android-powered device, here's how you can install and run your app:

  1. 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 yourdevice. For help installing drivers, see theOEM USBDrivers document.
  2. Enable USB debugging on your device.
    • 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.

      Note: On Android 4.2 and newer, Developer options is hidden by default. To make it available, GotoSettings > About phone and tapBuild number seven times. Return to the previous screen to findDeveloper options.

To run the app from Eclipse:

  1. Open one of your project's files and clickRun from the toolbar.
  2. In the Run as window that appears, select Android Application and clickOK.

Eclipse installs the app on your connected device and starts it.

Or to run your app from a command line:

  1. Change directories to the root of your Android project and execute:
    ant debug
  2. Make sure the Android SDK platform-tools/ directory is included in yourPATH environment variable, then execute:
    adb install bin/MyFirstApp-debug.apk
  3. 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 thenextlesson.

Run on the Emulator


Whether you're using Eclipse or the command line, to run your app on the emulator you need tofirst create anAndroid Virtual Device (AVD). AnAVD is a device configuration for the Android emulator that allows you to model differentdevices.

Figure 1. The AVD Manager showing a few virtualdevices.

To create an AVD:

  1. Launch the Android Virtual Device Manager:
    1. In Eclipse, click Android Virtual Device Manager  from the toolbar.
    2. From the command line, changedirectories to <sdk>/tools/ and execute:
      android avd
  2. In the Android Virtual Device Manager panel, click New.
  3. Fill in the details for the AVD.Give it a name, a platform target, an SD card size, and a skin (HVGA is default).
  4. Click Create AVD.
  5. Select the new AVD from the Android Virtual Device Manager and clickStart.
  6. After the emulator boots up, unlock the emulator screen.

To run the app from Eclipse:

  1. Open one of your project's files and clickRun from the toolbar.
  2. In the Run as window that appears, select Android Application and clickOK.

Eclipse installs the app on your AVD and starts it.

Or to run your app from the command line:

  1. Change directories to the root of your Android project and execute:
    ant debug
  2. Make sure the Android SDK platform-tools/ directory is included in yourPATH environmentvariable, then execute:
    adb install bin/MyFirstApp-debug.apk
  3. On the emulator, locate MyFirstActivity and open it.

That's how you build and run your Android app on the emulator! To start developing, continue to thenextlesson.

 
0
posted @ 2017-07-13 16:31  IT媚娘  阅读(153)  评论(0)    收藏  举报