本章主要讨论如何建立Android开发环境。
查看 System Requirements 网页,以确认您开发用的计算机性能够强!
1.2 Windows 下建立Android开发环境
为了开发Android,必须安装Java开发包JDK,以及Android开发环境(IDE,如Eclipse等)。当然,还有Android的SDK包。
1. JDK
进入http://java.sun.com/javase/downloads/index.jsp,根据操作系统选择最新的JDK下载。我当时选择的是JDK6 Update 20。
2. Eclipse
进入http://www.eclipse.org/downloads/,根据操作系统选择最新的JDK下载。我当时选择的是eclipse-jee-galileo-SR2-win32.zip。
3. android-sdk
进入http://androidappdocs.appspot.com/sdk/index.html,根据操作系统选择最新的SDK下载。我当时选择的是android-sdk_r06。
4. ADT
进入http://androidappdocs.appspot.com/sdk/eclipse-adt.html ,下载最新的ADT。我当时选择的是ADT 0.9.7。
在下载和安装Eclipse之前,由于Eclipse作为一个程序是用Java写的,它要求JRE(Java Runtime Environment)来运行,因此必须首先安装JRE,否则打开Eclipse会出现错误。JRE允许运行Java基础的应用程序,但并不允许开发者创建Java基础的程序。为在Android上利用Java进行应用程序的开发,需要下载安装JDK(Java Development Kit),JDK 包含了JRE 。
傻瓜式安装JDK后,必须确保JDK的bin目录(如E:/Tech/Java/jre6/bin)包含在PATH环境变量中。
在cmd下输入 java -version ,如果显示如下,则表示安装成功:
java version “1.6.0_20”
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)
注意 :在安装Androoid SDK前一定要先安装JDK,否则SDK安装不了。
Ø 先将sdk解压。
必须将SDK解压到我们的SDK安装目录中。因为SDK安装程序会从网上下载相应package下来安装,而其安装目录正是解压目录。
Ø 安装中会让我们选择要安装的package。不熟悉的话,最好是选用默认值;
Ø 之后安装程序自己从网上下载相应package下来安装,不需我们干预。但费时较长,我这边约略花了2个小时;
Ø 将SDK的tools 目录的绝对路径添加到系统的PATH 中;
Ø 验证安装是否成功:
Android应用程序是在Java上进行开发的。Android自身不是一个语言,是一个运行应用程序的环境。在理论上,可以使用任何发布或者综合开发环境(IDE)来开始自己的开发,例如使用Borland的Jbuilder进行开发或者开源NetBeans。但是,Eclipse是开放手机联盟和谷歌认同的一个Java开发IDE,因此我们进行应用开发的一般选择还是安装Eclipse,以下是为什么选择Eclipse的补充几点原因:
Ø Eclipse具有开源、免费的特点,同时也是一个可用的Java开发IDE,这些特点符合开放手机联盟真正开发移动开发市场的宗旨和要求。此外,Eclipse也较容易使用。
Ø 开放手机联盟已经为Eclipse发布了一个Android的Plugin,允许开发者创建Android-定义项目,并可将编译后的程序在Android模拟器上进行运行和调试。Android的plugin所具有的工具和能力,能缩短开发的时间。
Ø Eclipse同样也可用于苹果和Linux系统,对应不同的系统有对应的版本,它们之间的操作差异并不会太多。
该软件不需安装,只需将下载的Eclipse安装包直接解压到安装路径,点击eclipse.exe后设置workspace就可以了。
1.2.5 Eclipse 中安装Android 组件
Android 为Eclipse 定制了一个插件,即Android Development Tools(ADT),目的是为用户提供一个强大的综合环境用于开发Android 应用程序。ADT 扩展了Eclipse 的功能,可以让用户快速地建立Android 项目,创建应用程序界面,在基于Android 框架API 的基础上添加组件,以及用SDK 工具集调试应用程序,甚至导出签名(或未签名)的APKs 以便发行应用程序。
Installing the ADT Plugin :ADT安装、配置、更新与卸载指南。
下面介绍安装ADT的步骤:
Ø 启动Eclipse,选择菜单 Help-> Install New Software后,跳出如下框图:
Ø 点击“Available Software Sites”,在下图中填入如下信息:
Name:Android Plugin
Location:https://dl-ssl.google.com/android/eclipse/
这边请注意:若获取后续插件有问题,请将上述网址中 https 改为 http。
Ø 选择 OK 后退回前图,在Work with 列表中选择“Android Plugin”。稍候片刻,下图列表中出现"Developer Tools"。选择其下两个插件”Android DDMS” 和 “Android Development”。并点击 Next;
Ø 后续将列出两个工具插件的特性,接受其Licence,最后,安装后重启Eclipse;
配置ADT组件其实就是给Eclipse指明SDK的目录所在。
Ø 选择菜单项 Window > Preferences,打开Preferences面板;
Ø 在面板左侧选择“Android”,在右侧中填入 SDK目录,选择 Apply;
Ø ok,稍后片刻右侧列表中将列出在上述SDK目录下找到的Target。
1.2.6 增加Android平台和其他组件
在前几节 Android SDK安装中,选择package的地方即为本节要着重讨论的。
安装SDK后,可通过启动SDK根目录下的 SDK Setup.exe来启动“Android SDK and AVD Manager”。下图列出了已经装好的package:
如需下载其他组件的话,请进入如下界面,其中列出了网上可获取且未安装的组件:
Android SDK 使用一种组件结构来将其内部的各个组成部分划分成一个个可独立安装的“components”。这些组件有:Android platform versions, add-ons, tools, samples, and the API documentation。
一般来说,我们刚下载的SDK只包含了一个组件:最新版本的SDK工具。为了开发Android程序,我们至少还需增加一种Android平台到我们的开发环境中。当然,Google非常推荐下载其他可选组件。
1.2.6.1 SDK组件有哪些类型?
SDK提供了如下几种类型的组件:
Ø SDK Tools
该组件预装在下载下来的“Android SDK starter package”中,包含了开发、调试、测试我们的程序和UI所需的全部SDK工具。关于SDK Tools的更多信息可参考Dev Guide :http://androidappdocs.appspot.com/guide/developing/tools/index.html;
Ø Android platforms
An SDK platform is available for every production Android platform deployable to Android-powered devices. Each platform component includes a fully compliant Android library and system image, sample code, emulator skins, and any version specific tools. For detailed information about each platform, see the overview documents available under the section "Downloadable SDK Components," at left.
Ø SDK Add-Ons
SDK add-ons provide a development environment for specific Android external library or a customized (but fully compliant) Android system image. The Android SDK repository offers the Google APIs Add-On, which gives your application access to powerful mapping capabilities through the com.google.android.maps library. You can also add additional repositories, so that you can download other SDK add-ons, where available.
Ø USB Driver for Windows
Contains driver files that you can install on your Windows computer, so that you can run and debug your applications on an actual device. You do not need the USB driver unless you plan to debug your application on an actual Android-powered device. If you develop on Mac OS X or Linux, you do not need a special driver to debug your application on an Android-powered device.
Ø Samples
Contains the sample code and apps available for each Android development platform. If you are just getting started with Android development, make sure to download the samples to your SDK.
Ø Documentation
Contains a local copy of the latest multiversion documentation for the Android framework API.
1.2.6.2 哪些组件是我们需要的?
根据我们开发的需要,可以分为几种开发环境,其相应所需的组件见下表:
Environment | SDK Component | Comments |
Basic | SDK Tools | If you've installed the SDK starter package, then you already have this component preinstalled. The SDK Tools component is required — you can't develop or build an application without it. |
SDK platform | You need to download at least one platform into your environment, so that you will be able to compile your application and set up an Android Virtual Device (AVD) to run it on (in the emulator). To start with, just download the latest version of the platform. Later, if you plan to publish your application, you will want to download other platforms as well, so that you can test your application on the full range of Android platform versions that your customers are using. | |
Recommended | Documentation | The Documentation component is useful because it lets you work offline and also look up API reference information from inside Eclipse. |
Samples | The Samples components give you source code that you can use to learn about Android, load as a project and run, or reuse in your own app. Note that multiple samples components are available — one for each Android platform version. When you are choosing a samples component to download, select the one whose API Level matches the API Level of the Android platform that you plan to use. | |
Usb Driver | The Usb Driver component is needed only if you are developing on Windows and have an Android-powered device on which you want to install your application for debugging and testing. For Mac OS X and Linux platforms, no special driver is needed. | |
Full | Google APIs | The Google APIs add-on gives your application access to the Maps external library, which makes it easy to display and manipulate Maps data in your application. |
Additional SDK Platforms | If you plan to publish your application, you will want to download additional platforms corresponding to the Android platform versions on which you want the application to run. The recommended approach is to compile your application against the lowest version you want to support, but test it against higher versions that you intend the application to run on. You can test your applications on different platforms by running in an Android Virtual Device (AVD) on the Android emulator. |
1.2.6.3 如何增加Android组件
关于这部分建议请参考:http://androidappdocs.appspot.com/sdk/adding-components.html 。
至此,Windows下的Android开发环境安装完成。后续我们将在“Hello World”一节中以一个项目来验证环境的可行性。