领先一步
VMware 提供培训和认证,助您加速进步。
了解更多在《Spring Android and Maven (Part 1)》中,我介绍了如何使用Maven从命令行构建Android应用程序。在这篇文章中,我将向您展示如何从Eclipse IDE使用Maven依赖管理来构建Android应用程序。该应用程序还将展示本周发布的Spring Android 1.0.0.M2中的最新功能。
Maven Android Plugin允许您使用Maven构建Android应用程序并从中受益于依赖管理。Google的Android Development Tools (ADT) 插件允许您在Eclipse IDE中开发和构建Android应用程序。要在Eclipse中实现Maven依赖管理,需要Maven Integration for Android Development Tools插件,该插件集成了m2eclipse、ADT插件和Maven Android Plugin。这篇文章将向您展示如何安装此插件,并使用它在Eclipse IDE中实现基于Maven的依赖管理。
本文使用的每个组件的具体版本如下所列
在构建或编译任何代码之前,我们需要安装和配置所需的Eclipse插件。在Part 1中,我讨论了安装Android SDK,所以假设您已经完成了。但是,如果您还没有安装,则需要继续安装。此外,您还需要已经安装了Eclipse 3.5或更高版本。在本例中,我使用的是基于Eclipse 3.6.1的SpringSource Tool Suite 2.5.2。
需要安装三个Eclipse插件:ADT Plugin for Eclipse、Maven Integration for Eclipse以及Maven Integration for Android Development Tools。您有两种选择来安装这些插件:要么使用Eclipse Marketplace Client,要么手动安装每个插件。
根据您的Eclipse版本,您可能已经安装了Eclipse Marketplace Client。Marketplace Client将简化插件的安装,因为它将传递性地包含任何必需的插件。


除了使用Marketplace Client之外,还可以手动安装每个插件。如果您从Marketplace安装了插件,则可以跳到“示例Android应用程序”部分。对于每个插件,在Eclipse的Help菜单中,选择Install New Software...,然后单击Add...按钮。
第一步是安装ADT(Android Developer Tools)插件。这是Google提供的官方插件,用于开发Android应用程序。如果您已经安装了ADT插件,请通过从Eclipse的Help菜单运行Check for Updates来验证您是否具有最新版本。


注意:如果您在ADT安装过程中遇到任何问题,Android网站可以提供更多信息。
下一步是安装m2eclipse插件。STS 2.5.2附带此插件。但是,如果您使用的是早期版本,或者已经安装了该插件,则需要验证您是否拥有最新版本。Maven Integration for Android Development Tools需要0.12.0或更高版本。

我们还需要安装一个插件,这个插件将所有这些功能整合在一起。设置好Android SDK并在Eclipse中配置好ADT插件后,安装Maven Integration for Android Development Tools插件。

现在我们已经安装并配置了所有必需的插件,可以开始使用示例Android应用程序来测试我们的设置了。我们将使用为Part 1博客文章创建的示例应用程序,但该示例应用程序已更新为运行在最新的Android平台SDK 2.3.1 API Level 9上。如果您没有安装此SDK平台,则需要在构建示例代码之前进行安装。
运行以下命令来克隆Spring Mobile samples存储库。
$ git clone git://git.springsource.org/spring-mobile/samples.git spring-mobile-samples
如果git://URL不可访问,您可能需要尝试samples存储库的备用URL。
$ git clone http://http.git.springsource.org/spring-mobile/samples.git spring-mobile-samples
在Eclipse中打开源代码之前,请导航到spring-android-showcase/client项目目录,并验证项目是否可以使用Android Maven Plugin成功构建。
$ mvn clean install
假设项目已成功从命令行构建,我们就可以在Eclipse中打开该项目了。


示例项目现在已加载到Eclipse中。您首先会注意到Package Explorer中项目上的一个大红色“X”,这表示它目前无法构建。由于我们尚未为该项目配置Maven,因此这是预期的行为。
要启用Maven依赖管理,请右键单击Package Explorer中的spring-android-showcase-client,然后从上下文菜单中选择Maven -> Enable Dependency Management。
示例项目已包含以下Maven POM文件。如果您的项目中没有现有的POM,Eclipse会提示您创建一个。请注意在build部分中使用了maven-android-plugin和maven-compiler plugin。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.android</groupId>
<artifactId>spring-android-showcase-client</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<packaging>apk</packaging>
<name>spring-android-showcase-client</name>
<url>http://www.springsource.org</url>
<organization>
<name>SpringSource</name>
<url>http://www.springsource.org</url>
</organization>
<properties>
<android-platform>9</android-platform>
<android-emulator>9</android-emulator>
<maven-android-plugin-version>2.8.4</maven-android-plugin-version>
<maven-compiler-plugin-version>2.3.2</maven-compiler-plugin-version>
<android-version>2.3.1</android-version>
<spring-android-version>1.0.0.M2</spring-android-version>
<jackson-version>1.7.2</jackson-version>
<simple-version>2.4.1</simple-version>
<android-rome-version>1.0.0-r2</android-rome-version>
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<version>${maven-android-plugin-version}</version>
<configuration>
<sdk>
<platform>${android-platform}</platform>
</sdk>
<emulator>
<avd>${android-emulator}</avd>
</emulator>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin-version}</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>${android-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.android</groupId>
<artifactId>spring-android-rest-template</artifactId>
<version>${spring-android-version}</version>
</dependency>
<dependency>
<!-- Using Jackson for JSON marshaling -->
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<!-- Using Simple for XML marshaling -->
<groupId>org.simpleframework</groupId>
<artifactId>simple-xml</artifactId>
<version>${simple-version}</version>
<exclusions>
<exclusion>
<artifactId>stax</artifactId>
<groupId>stax</groupId>
</exclusion>
<exclusion>
<artifactId>stax-api</artifactId>
<groupId>stax</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<!-- Using ROME for RSS and ATOM feeds -->
<groupId>com.google.code.android-rome-feed-reader</groupId>
<artifactId>android-rome-feed-reader</artifactId>
<version>${android-rome-version}</version>
</dependency>
</dependencies>
<repositories>
<!-- For developing with Android ROME Feed Reader -->
<repository>
<id>android-rome-feed-reader-repository</id>
<name>Android ROME Feed Reader Repository</name>
<url>https://android-rome-feed-reader.googlecode.com/svn/maven2/releases</url>
</repository>
<!-- For testing against latest Spring snapshots -->
<repository>
<id>org.springframework.maven.snapshot</id>
<name>Spring Maven Snapshot Repository</name>
<url>http://maven.springframework.org/snapshot</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<!-- For developing against latest Spring milestones -->
<repository>
<id>org.springframework.maven.milestone</id>
<name>Spring Maven Milestone Repository</name>
<url>http://maven.springframework.org/milestone</url>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
</project>
Maven现在将更新所需的依赖项,Eclipse应该可以成功构建项目。一旦Eclipse完成项目构建,您现在应该会在Package Explorer窗口中看到Maven Dependencies类路径容器。
有几点需要注意。首先,您可能会看到项目中有一个bin文件夹。您可以从Java Build Path属性(如下所示)中看到默认的输出文件夹是Target文件夹。因此,可以安全地删除bin文件夹。
其次,您可能还会注意到项目中添加了一个JRE System Library类路径容器。由于我们正在构建一个利用Android JVM的Android应用程序,因此您应该不需要引用JRE。如果您在Eclipse中使用ADT创建了新的Android应用程序,您会知道它不会为JRE添加类路径容器。我已经与Maven Integration for Android Development Tools的开发人员Ricardo Gladwell讨论过此事,他创建了一个ticket来研究这个问题。我已经从示例项目中删除了JRE,没有明显的负面影响。但您可能需要关注该问题以获取更多信息。
要运行示例应用程序,只需在Package Explorer中选择spring-android-showcase-client,然后单击Run按钮。示例客户端中的Maven POM文件配置为查找名为“9”的Android Virtual Device (AVD)。如前所述,samples项目已更新为运行在Android Platform SDK 2.3.1上。您需要为此平台配置一个AVD才能运行samples。
第一次运行应用程序时,您应该会在Eclipse控制台中看到类似以下的内容
[2011-02-08 14:00:49 - spring-android-showcase-client] ------------------------------
[2011-02-08 14:00:49 - spring-android-showcase-client] Android Launch!
[2011-02-08 14:00:49 - spring-android-showcase-client] adb is running normally.
[2011-02-08 14:00:49 - spring-android-showcase-client] Performing org.springframework.android.showcase.MainActivity activity launch
[2011-02-08 14:00:49 - spring-android-showcase-client] Automatic Target Mode: launching new emulator with compatible AVD '9'
[2011-02-08 14:00:49 - spring-android-showcase-client] Launching a new emulator with Virtual Device '9'
[2011-02-08 14:00:50 - Emulator] 2011-02-08 14:00:50.936 emulator[5951:903] Warning once: This application, or a library it uses, is using NSQuickDrawView, which has been deprecated. Apps should cease use of QuickDraw and move to Quartz.
[2011-02-08 14:00:50 - spring-android-showcase-client] New emulator found: emulator-5554
[2011-02-08 14:00:50 - spring-android-showcase-client] Waiting for HOME ('android.process.acore') to be launched...
[2011-02-08 14:01:21 - spring-android-showcase-client] HOME is up on device 'emulator-5554'
[2011-02-08 14:01:21 - spring-android-showcase-client] Uploading spring-android-showcase-client.apk onto device 'emulator-5554'
[2011-02-08 14:01:23 - spring-android-showcase-client] Installing spring-android-showcase-client.apk...
[2011-02-08 14:01:50 - spring-android-showcase-client] Success!
[2011-02-08 14:01:50 - spring-android-showcase-client] Starting activity org.springframework.android.showcase.MainActivity on device emulator-5554
[2011-02-08 14:01:52 - spring-android-showcase-client] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=org.springframework.android.showcase/.MainActivity }
AVD将启动并显示锁定屏幕。将绿色锁从左向右滑动以“打开”Android设备。打开后,应用程序现在应该显示
在这篇文章中,我们回顾了如何在Eclipse中构建一个使用Maven依赖管理的示例Android应用程序。为了实现这一点,我们使用了Eclipse、Android Development Tools (ADT) Plugin for Eclipse、Maven Android Plugin、Maven Integration for Android Development Tools插件以及Maven Integration for Eclipse (m2eclipse)插件。其中涉及许多组件,但一旦所有设置都配置好,就可以轻松地构建和部署到Android模拟器。如果您在Android应用程序中使用第三方库,则应考虑使用这些工具来帮助管理这些依赖项。