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.
Before you run your app, you shoshould be aware of a few directories and files in the android project:
-
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.
-
src/
-
Directory for your app's main source files. By default, it has des
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 varous XML files that contain a collection of resources, such as string and color definitions.
When you build and run the default Android app, the defaultActivity
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.
Run on a real device
If you have a real Android-powered device, here's how you can install and run your 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
Oem usb Drivers document.
- EnableUSB debuggingOn 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 inSettings> developer options.
Note:On Android 4.2 and newer,Developer optionsIs hidden by default. To make it available, go
Settings> about phoneAnd tapBuild numberSeven times. Return to the previous screen to find
Developer options.
To run the app from eclipse, open one of your project's files and click Run from the toolbar. Eclipse INSTALLThe app on your connected device and starts it.
Or to run your app from a command line:
- Change directories to the root of your android project and execute:
ant debug
- Make sure the android SDK
platform-tools/
Directory is already ded in your
PATH
Environment variable, then execute:adb install bin/MyFirstApp-debug.apk
- On your device, locateMyfirstactivityAnd open it.
That's how you build and run your Android app on a device! To start developing, continue to
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
Android Virtual Device (AVD). An AVD is a device configuration for the android emulator that allows you to model different devices.
Figure 1.The AVD manager showing a few virtual devices.
To create an AVD:
- Launch the android Virtual Device Manager:
- In eclipse, click Android Virtual Device Manager from the toolbar.
- From the command line, change directories
<sdk>/tools/
And execute:android avd
- InAndroid Virtual Device ManagerPanel, clickNew.
- Fill in the details for the AVD. give it a name, a platform target, an SD card size, and a skin (hvga is default ).
- ClickCreate AVD.
- Select the new AVD fromAndroid Virtual Device ManagerAnd click
Start.
- After the emulator boots up, unlock the emulator screen.
To run the app from eclipse, open one of your project's files and click Run from the toolbar. Eclipse INSTALLThe app on your AVD and starts it.
Or to run your app from the command line:
- Change directories to the root of your android project and execute:
ant debug
- Make sure the android SDK
platform-tools/
Directory is already ded in your
PATH
Environment variable, then execute:adb install bin/MyFirstApp-debug.apk
- On the emulator, locateMyfirstactivityAnd open it.