赞
踩
The Android platform is a software stack for mobile devices including an operating system, middleware and key applications. Developers can create applications for the platform using the Android SDK. Applications are written using the Java programming language and run on Dalvik, a custom virtual machine designed for embedded use which runs on top of a Linux kernel.
If you want to know how to develop applications for Android, you're in the right place. This site provides a variety of documentation that will help you learn about Android and develop mobile applications for the platform.
An early look at the the Android SDK is also available. It includes sample projects with source code, development tools, an emulator, and of course all the libraries you'll need to build an Android application.
To start learning about the Android platform, please read the documentation in the following order:
————————————————————————————————————————————————
Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
The following diagram shows the major components of the Android operating system. Each section is described in more detail below.
Android will ship with a set of core applications including an email client, SMS program, calendar, maps, browser, contacts, and others. All applications are written using the Java programming language.
Developers have full access to the same framework APIs used by the core applications. The application architecture is designed to simplify the reuse of components; any application can publish its capabilities and any other application may then make use of those capabilities (subject to security constraints enforced by the framework). This same mechanism allows components to be replaced by the user.
Underlying all applications is a set of services and systems, including:
For more details and a walkthrough of an application, see Writing an Android Application.
Android includes a set of C/C++ libraries used by various components of the Android system. These capabilities are exposed to developers through the Android application framework. Some of the core libraries are listed below:
Android includes a set of core libraries that provides most of the functionality available in the core libraries of the Java programming language.
Every Android application runs in its own process, with its own instance of the Dalvik virtual machine. Dalvik has been written so that a device can run multiple VMs efficiently. The Dalvik VM executes files in the Dalvik Executable (.dex) format which is optimized for minimal memory footprint. The VM is register-based, and runs classes compiled by a Java language compiler that have been transformed into the .dex format by the included "dx" tool.
The Dalvik VM relies on the Linux kernel for underlying functionality such as threading and low-level memory management.
Android relies on Linux version 2.6 for core system services such as security, memory management, process management, network stack, and driver model. The kernel also acts as an abstraction layer between the hardware and the rest of the software stack.
——————————————————————————————————————————————————
To get started with Android, please read the following sections first:
After reading the sections above, the following Getting Started information is also very useful:
These are the basic packages that make up the Android SDK for writing applications. The packages are organized as layers, listed here from lowest-level to highest.
These packages provide additional domain-specific features of the Android platform. They are not necessary for basic application development.
————————————————————————————————————————————————
This page describes how to install the Android SDK and set up your development environment. If you haven't downloaded the SDK yet, you can use the link below to get started. Then read the rest of this document to learn how to install, configure, and use the SDK to create Android applications.
If you have already downloaded an earlier version of the SDK and developed applications on it, please read the Upgrading the SDK document instead. The Upgrading document explains how to migrate your existing development environment and applications to the new SDK release. The document also provides information that you will find useful during the upgrade, such as lists of framework and API changes.
To develop Android applications using the code and tools in the Android SDK, you need a suitable development computer and development environment, as described below.
Supported Operating Systems
Supported Development Environments
Note: If JDK is already installed on your development computer, please take a moment to make sure that it meets the version requirements listed above. In particular, note that some Linux distributions may include JDK 1.4 or Gnu Compiler for Java, both of which are not supported for Android development.
After downloading the SDK, unpack the .zip archive to a suitable location on your machine. By default, the SDK files are unpacked into a directory named android_sdk_<platform>_<release>_<build>
. The directory contains the subdirectories tools/
, samples/
, and others.
Make a note of the name and location of the unpacked SDK directory on your system — you will need to refer to the SDK directory later, when setting up the Android plugin or using SDK tools.
Optionally, you can add the path to the SDK tools
directory to your path. As mentioned above, the tools/
directory is located in the SDK directory.
tools/
directory to it. If you don't see a line setting the path, you can add one: export PATH=${PATH}:<your_sdk_dir>/tools
tools/
directory to the path.
Adding tools
to your path lets you run Android Debug Bridge (adb) and the other command line tools without needing to supply the full path to the tools directory. Note that, if you update your SDK, you should remember to update your PATH settings to point to the new location, if different.
If you are having trouble downloading the ADT plugin after following the steps listed at left, here are some suggestions:
http
, rather than https
. If you are still unable to use Eclipse to download the ADT plugin, follow these steps to download and install the plugin from your computer:
Note that to update your plugin, you will have to follow these steps again instead of the default update instructions.
If you will be using the Eclipse IDE as your environment for developing Android applications, you can install a custom plugin called Android Development Tools (ADT), which adds integrated support for Android projects and tools. The ADT plugin includes a variety of powerful extensions that make creating, running, and debugging Android applications faster and easier.
If you will not be using the Eclipse IDE, you do not need to download or install the ADT plugin.
To download and install the ADT plugin, follow the steps below.
https://dl-ssl.google.com/android/eclipse/Press OK.
Now press Next.
In some cases, a new ADT plugin may become available for your existing version of the SDK. You can use the steps below to update the ADT plugin from inside Eclipse.
Alternatively,
To begin developing Android applications in the Eclipse IDE, you first create an Android project and then set up a launch configuration. After that, you can write, run, and debug your application.
The sections below provide instructions assuming that you have installed the ADT plugin in your Eclipse environment. If you haven't installed the ADT plugin, you should do that before using the instructions below. See the Installing the Eclipse Plugin (ADT) for more information.
The ADT plugin provides a New Project Wizard that you can use to quickly create an Eclipse project for new or existing code. To create the project, follow these steps:
Enter the project name, the base package name, the name of a single Activity class to create as a stub .java file, and a name to use for your application.
Browse to the directory containing the existing source code and click OK. If the directory contains a valid Android manifest file, the ADT plugin fills in the package, activity, and application names for you.
The ADT plugin creates the these folders and files for you as appropriate for the type of project:
Before you can run and debug your application in Eclipse, you must create a launch configuration for it. A launch configuration specifies the project to launch, the Activity to start, the emulator options to use, and so on.
To create a launch configuration for the application, follow these steps:
Once you've set up the project and launch configuration for your application, you can run or debug it as described below.
From the Eclipse main menu, select Run > Run or Run > Debug as appropriate, to run or debug the active launch configuration.Note that the active launch configuration is the one most recently selected in the Run configuration manager. It does not necessarily correspond to the application that is selected in the Eclipse Navigation pane (if any).
To set or change the active launch configuration, use the Run configuration manager, which you can access through Run > Open Run Dialog... or Run > Open Debug Dialog....
Running or debugging the application triggers these actions:
The recommended way to develop an Android application is to use Eclipse with the Android plugin. This plugin provides editing, building, and debugging functionality integrated right into the IDE. However, the SDK includes tools to enable you to develop with other IDEs, including intelliJ (or if you'd rather use Eclipse without the plugin).
The Android SDK includes activityCreator
, a program that generates a number of stub files for your project, as well as a build file. You can use the program to create an Android project for new code or from existing code, such as the sample applications included in the SDK. For Linux and Mac, the SDK provides activityCreator.py
, a Python script, and for Windows, activityCreator.bat
, a batch script. Regardless of platform, you can use activityCreator
in the same way.
To run activityCreator
and create an Android project, follow these steps:
tools/
directory of the SDK and create a new directory for your project files. If you are creating a project from existing code, change to the root folder of your application instead. Run activityCreator
. In the command, you must specify a fully-qualified class name as an argument. If you are creating a project for new code, the class represents the name of a stub class that the script will create. If you are creating a project from existing code, you must specify the name of one Activity class in the package. Command options for the script include:
--out <folder>
which sets the output directory. By default, the output directory is the current directory. If you created a new directory for your project files, use this option to point to it. --ide intellij
, which generates IntelliJ IDEA project files in the newly created project Here's an example:
~/android_linux_sdk/tools $ ./activityCreator.py --out myproject your.package.name.ActivityName package: your.package.name out_dir: myproject activity_name: ActivityName ~/android_linux_sdk/tools $
The activityCreator
script generates the following files and directories (but will not overwrite existing ones):
AndroidManifest.xml
The application manifest file, synced to the specified Activity class for the project. build.xml
An Ant
file that you can use to build/package the application. src/your/package/name/ActivityName.java
The Activity class you specified on input. your_activity.iml, your_activity.ipr, your_activity.iws
[only with the -ide intelliJ
flag] intelliJ project files. res/
A directory to hold resources. src/
The source directory. bin/
The output directory for the build script. You can now move your folder wherever you want for development, but keep in mind that you'll have to use the adb program in the tools/
folder to send files to the emulator, so you'll need access between your solution and the tools/
folder.
Also, you should refrain from moving the location of the SDK directory, since this will break the build scripts (they will need to be manually updated to reflect the new SDK location before they will work again).
Use the Ant build.xml
file generated by activityCreator
to build your application.
Note: When installing JDK on Windows, the default is to install in the "Program Files" directory. This location will cause ant
to fail, because of the space. To fix the problem, you can specify the JAVA_HOME variable like this: set JAVA_HOME=c:\Prora~1\Java\<jdkdir></jdkdir>
. The easiest solution, however, is to install JDK in a non-space directory, for example: c:\java\jdk1.6.0_02
.
To run a compiled application, you will upload the .apk file to the /data/app/
directory in the emulator using the adb tool as described here:
<your_sdk_dir>/tools/emulator
from the command line) adb install myproject/bin/<appname>.apk
to upload the executable. So, for example, to install the Lunar Lander sample, navigate in the command line to <your_sdk_dir>/sample/LunarLander
and type ../../tools/adbinstallbin/LunarLander.apk
Note: When you install an Activity for the first time, you might have to restart the emulator before it shows up in the application launcher, or other applications can call it. This is because the package manager usually only examines manifests completely on emulator startup.
This section describes how to display debug information on the screen (such as CPU usage), as well as how to hook up your IDE to debug running applications on the emulator.
Attaching a debugger is automated using the Eclipse plugin, but you can configure other IDEs to listen on a debugging port to receive debugging information.
DDMS will assign a specific debugging port to every virtual machine that it finds on the emulator. You must either attach your IDE to that port (listed on the Info tab for that VM), or you can use a default port 8700 to connect to whatever application is currently selected on the list of discovered virtual machines.
Your IDE should attach to your application running on the emulator, showing you its threads and allowing you to suspend them, inspect their state, and set breakpoints. If you selected "Wait for debugger" in the Development settings panel the application will run when Eclipse connects, so you will need to set any breakpoints you want before connecting.
Changing either the application being debugged or the "Wait for debugger" option causes the system to kill the selected application if it is currently running. You can use this to kill your application if it is in a bad state by simply going to the settings and toggling the checkbox.
Android has a fairly extensive set of tools to help you debug your programs:
...
I/MemoryDealer( 763): MemoryDealer (this=0x54bda0): Creating 2621440 bytes heap at 0x438db000
I/Logger( 1858): getView() requesting item number 0
I/Logger( 1858): getView() requesting item number 1
I/Logger( 1858): getView() requesting item number 2
D/ActivityManager( 763): Stopping: HistoryRecord{409dbb20 com.google.android.home.AllApps}
...
Android Log- A logging class to print out messages to a log file on the emulator. You can read messages in real time if you run logcat on DDMS (covered next). Add a few logging method calls to your code.
To use the Log
class, you just call Log.v()
(verbose), Log.d()
(debug), Log.i()
(information), Log.w()
(warning) or Log.e
(error) depending on the importance you wish to assign the log message.
Log.i("MyActivity", "MyClass.getView() — Requesting item number " + position)
You can use logcat to read these messages
Also, see the Troubleshooting section of the doc to figure out why your application isn't appearing on the emulator, or why it's not starting.
Android lets you set a number of settings that will make it easier to test and debug your applications. To get to the development settings page on the emulator, go to Dev Tools > Development Settings. This will open the development settings page with the following options (among others):
These settings will be remembered across emulator restarts.
adb shell
, use "ps" to find the process you want, and then "kill -3 ". The stack trace appears in the log file.
adb shell logcat -b radio
Android requires custom build tools to be able to properly build the resource files and other parts of an Android application. Because of this, you must have a specialized build environment for your application.
Custom Android compilation steps include compiling the XML and other resource files, and creating the proper output format. A compiled Android application is an .apk file, which is a compressed file containing .dex files, resource files, raw data files, and other files. You can create a properly structured Android project either from scratch, or from existing source files.
Android does not currently support development of third party applications in native code (C/C++).
The recommended way to develop an Android application is to use Eclipse with the Android plugin, which provides support for building, running, and debugging Android applications.
If you have another IDE, Android provides tools for other IDEs to build and debug Android applications, but they are not as integrated.
To remove an application that you have installed on the emulator, you will need to run adb and delete the .apk file you sent to the emulator when you installed it. Use adb shell
to drop into a shell on the device as described in the linked topic, navigate to data/app/
, and then remove the file using rm your_app.apk
.
You can execute arbitrary code when paused at a breakpoint in Eclipse. For example, when in a function with a String argument called "zip", you can get information about packages and call class methods. You can also invoke arbitrary static methods: for example, entering android.os.Debug.startMethodTracing()
will start dmTrace.
Open a code execution window, select Window>Show View>Display from the main menu to open the Display window, a simple text editor. Type your expression, highlight the text, and click the 'J' icon (or CTRL + SHIFT + D) to run your code. The code runs in the context of the selected thread, which must be stopped at a breakpoint or single-step point. (If you suspend the thread manually, you have to single-step once; this doesn't work if the thread is in Object.wait().)
If you are currently paused on a breakpoint, you can simply highlight and execute a piece of source code by pressing CTRL + SHIFT + D.
You can highlight a block of text within the same scope by pressing ALT +SHIFT + UP ARROW to select larger and larger enclosing blocks, or DOWN ARROW to select smaller blocks.
Here are a few sample inputs and responses in Eclipse using the Display window.
Input | Response |
---|---|
zip | (java.lang.String) /work/device/out/linux-x86-debug/android/app/android_sdk.zip |
zip.endsWith(".zip") | (boolean) true |
zip.endsWith(".jar") | (boolean) false |
You can also execute arbitrary code when not debugging by using a scrapbook page. Search the Eclipse documentation for "scrapbook".
Although the recommended way to debug is to use the ADT plugin, you can manually run DDMS and configure Eclipse to debug on port 8700. (Note: Be sure that you have first started DDMS).
——————————————————————————————————————————————
First impressions matter, and as a developer you know that the first impression you get of a development framework is how easy it is to write "Hello, World!" Well, in Android, it's pretty easy. Here's how it looks:
The sections below spell it all out in detail.
Let's jump in!
Creating the project is as simple as can be. An Eclipse plugin is available making Android development a snap.
You'll need to have a development computer with the Eclipse IDE installed (see System and Software Requirements), and you'll need to install the Android Eclipse Plugin (ADT). Once you have those ready, come back here.
First, here's a high-level summary of how to build "Hello, World!":
That's it! Next, let's go through each step above in detail.
From Eclipse, select the File > New > Project menu item. If the Android Plugin for Eclipse has been successfully installed, the resulting dialog should have a folder labeled "Android" which should contain a single entry: "Android Project".
Once you've selected "Android Project", click the Next button.
The next screen allows you to enter the relevant details for your project. Here's an example:
Here's what each field on this screen means:
Project Name | This is the name of the directory or folder on your computer that you want to contain the project. |
Package Name | This is the package namespace (following the same rules as for packages in the Java programming language) that you want all your source code to reside under. This also sets the package name under which the stub Activity will be generated. The package name you use in your application must be unique across all packages installed on the system; for this reason, it's very important to use a standard domain-style package for your applications. In the example above, we used the package domain "com.android"; you should use a different one appropriate to your organization. |
Activity Name | This is the name for the class stub that will be generated by the plugin. This will be a subclass of Android's Activity class. An Activity is simply a class that can run and do work. It can create a UI if it chooses, but it doesn't need to. |
Application Name | This is the human-readable title for your application. |
The checkbox for toggling "Use default location" allows you to change the location on disk where the project's files will be generated and stored.
After the plugin runs, you'll have a class named HelloAndroid that looks like this:
public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); } }
The next step is to start modifying it!
Once you've got the project set up, the obvious next step is to get some text up there on the screen. Here's the finished product — next we'll dissect it line by line:
package com.android.hello; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); TextView tv = new TextView(this); tv.setText("Hello, Android"); setContentView(tv); } }
In Android, user interfaces are composed of hierarchies of classes called Views. A View is simply a drawable object, such as a radio button, an animation, or (in our case) a text label. The specific name for the View subclass that handles text is simply TextView.
Here's how you construct a TextView:
TextView tv = new TextView(this);
The argument to TextView's constructor is an Android Context instance. The Context is simply a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. The Activity class inherits from Context. Since our HelloAndroid class is a subclass of Activity, it is also a Context, and so we can pass the 'this' reference to the TextView.
Once we've constructed the TextView, we need to tell it what to display:
tv.setText("Hello, Android");
Nothing too surprising there.
At this point, we've constructed a TextView and told it what text to display. The final step is to connect this TextView with the on-screen display, like so:
setContentView(tv);
The setContentView() method on Activity indicates to the system which View should be associated with the Activity's UI. If an Activity doesn't call this method, no UI is present at all and the system will display a blank screen. For our purposes, all we want is to display some text, so we pass it the TextView we just created.
There it is — "Hello, World" in Android! The next step, of course, is to see it running.
The Eclipse plugin makes it very easy to
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。