Android官方开发指引中并不包含对maven的支持,但在google code上有个开源的 maven-android-plugin 插件项目,使用该插件可以很好地通过maven来管理Android项目,并对Android的多模块设计提供了良好的封装。
安装maven、android SDK等操作此处不再涉及,下面简单介绍如何配置pom文件,以及如何在eclipse上安装Maven Integration for Android Development Tools,做到命令行下和eclipse上都能够利用maven进行项目构建的目的。
一、Android项目的pom.xml示例
<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>example.XXXX.cupid</groupId> <artifactId>cupid</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>apk</packaging> <name>Cupid</name> <dependencies> <dependency> <groupId>com.google.android</groupId> <artifactId>android</artifactId> <version>2.2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.23</version> </dependency> <dependency> <groupId>apache-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <sourceDirectory>src</sourceDirectory> <pluginManagement> <plugins> <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>maven-android-plugin</artifactId> <version>2.8.4</version> <extensions>true</extensions> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>maven-android-plugin</artifactId> <configuration> <sdk> <!-- platform or api level (api level 4 = platform 1.6) --> <platform>8</platform> </sdk> <deleteConflictingFiles>true</deleteConflictingFiles> </configuration> </plugin> </plugins> </build> </project>
想要在命令行下构建Android工程,只需要在工程的根目录下新增pom.xml文件即可。
maven-android-plugin最新版本为3.5.0,如果读者机器上的maven版本较老,建议和我一样,采用较早的maven_android的版本,否则会由于和maven版本的兼容性问题无故报错。
*注:maven-android-plugin在3.0.0版本开始已经更名为android-maven-plugin.
二、如何在eclipse上安装Maven Integration for Android Development Tools
根据google code上的提示,需要安装软件及插件版本为:
这里要特别注意,安装的M2Eclipse插件版本最好是 0.12.0 或是0.12.1,否则在安装Maven Integration for Android Development Tools时会报:
Missing requirement: Maven Integration for Android Development Tools 0.2.5 (com.googlecode.eclipse.m2e.android.feature.feature.group 0.2.5) requires 'org.maven.ide.eclipse 0.12.0' but it could not be found 的错误
M2Eclipse 0.12.1 的安装方法为:启动Eclipse之后,在菜单栏中选择Help,然后选择Install New Software....,接着在Install对话框中店家Work with字段边上的Add按钮,得到一个新的Add Repository对话框,在Name字段中输入m2e,Location字段中输入http://m2eclipse.sonatype.org/sites/m2e,然后点击Ok。
Maven Integration for Android Development Tools安装方法为: Work with中输入https://svn.codespot.com/a/eclipselabs.org/m2eclipse-android-integration/updates/m2eclipse-android-integration/