之前一直在做WEB前端项目,前段时间接手第一个Android项目,拿到代码之后,先试着run起来再说,导入eclipse,一堆错误,设置classpath依赖,折腾半天,还是编译错误,于是联系项目接口人,得知他有一个Android库项目没有提交到SVN,晕。。。
对于习惯使用Maven管理Java项目的我来说,自然想到能否用Maven构建Android项目呢?于是开始Google、百度,发现已经有前人做过这样的实践了,不过在使用过程中还是遇到不少问题,后面经过各种努力终于能比较顺地使用了,这篇文章对如何使用Maven构建Android项目作了简要总结。如果你和我一样饱受项目依赖管理的折磨,和我一样讨厌项目打包发布时的繁琐,希望能通过Maven让这一切自动化完成。那么,这篇文章或许对你有用。
1. 环境搭建
- **JDK与Android SDK安装
做Android开发,这里无需多说,但安装完成后需要正确设置JAVA_HOME、CLASSPATH、ANDROID_HOME等环境变量。其中ANDROID_HOME为Android SDK安装的根目录。并将%ANDROID_HOME%\tools和%ANDROID_HOME%\platform-tools值添加到Path变量中。
- **Maven安装
这里无需多说,下载安装Maven并正确设置环境变量即可。
- **IDE支持
- **Eclipse
大多数人都在使用Eclipse开发Android应用,如果你用Eclipse做Android开发,推荐下载eclipse-with-m2e-and-adt,该版本已经安装了ADT、m2e、m2e-android等重要插件,支持在Eclipse中使用Maven进行Android应用开发。当时为了安装这些插件费了好大劲,所以,如果你看到这里,可以直接下载它,不用像我一样去做那些没有意义又浪费时间的事情。
- **IntelliJ IDEA
做Java开发,你不能不知道的神器,完美支持使用Maven构建Android应用,强烈推荐。即使是装了插件的Eclipse对使用Maven构建Android应用仍然支持不好。如果你是Eclipse的信徒,你也可以试试它,如果你能习惯它,你一定会被它的强大所吸引。
- **NetBeans IDE
NetBeans也支持Android开发,但没怎么了解,用的人应该也比较少。
2. 项目构建
以下是我的项目中使用的pom文件,因为涉及保密,部分地方做过修改,但整体结构没有改变,可以清楚地说明问题。
<span class="sc3"><span class="re1" style="color: #ffcc00;"><modelVersion<span class="re2">></span></span></span>4.0.0<span class="sc3"><span class="re1" style="color: #ffcc00;"></modelVersion<span class="re2">></span></span></span>
<groupId>com.ikoding.android</groupId> <artifactId>android-app-quickstart</artifactId> <version>1.0.0</version>
apk</packaging> <name>Android Application Quick Satrt</name>
<span class="sc3"><span class="re1" style="color: #ffcc00;"><dependencies<span class="re2">></span></span></span>
<dependency> <groupId>com.google.android</groupId> <artifactId>android</artifactId> <version>4.1.1.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.google.android</groupId> <artifactId>support-v4</artifactId> <version>r7</version> </dependency> <dependency> <groupId>org.apache.james</groupId> <artifactId>apache-mime4j</artifactId> <version>0.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.2.2</version> </dependency> <dependency> <groupId>com.ikoding.android</groupId> <artifactId>android-base</artifactId> <version>2.0.0</version> <type>apklib</type> </dependency> </dependencies>
<span class="sc3"><span class="re1" style="color: #ffcc00;">
<keystore.filename>app-quickstart.keystore</keystore.filename> <keystore.storepass>2013@ikoding</keystore.storepass> <keystore.keypass>2013@ikoding</keystore.keypass> <keystore.alias>ikoding-android-app</keystore.alias> UTF-8</project.build.sourceEncoding> </properties>
<span class="sc3"><span class="re1" style="color: #ffcc00;">**></span></span></span>
<finalName>${project.artifactId}-${project.version}-${manifest.metadata.id}</finalName> <sourceDirectory>src</sourceDirectory> <resources> <resource> <directory>.</directory> <filtering>true</filtering> <targetPath>../filtered-resources</targetPath> > <include>AndroidManifest.xml</include> </includes> </resource> <resource> <directory>resources</directory> <filtering>true</filtering> <includes> <include>**/</include> </includes> <excludes> <exclude>**/env-*.properties</exclude> </excludes> </resource> </resources>
<groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>android-maven-plugin</artifactId> <version>3.5.0</version> <extensions>true</extensions> <executions> <execution> <id>run</id> <goals> <goal>deploy</goal> <goal>run</goal> </goals> install</phase> </execution> </executions> <configuration> proguard.cfg</proguardConfig> ${project.build.proguardSkip}</proguardSkip> <manifestDebuggable>${manifest.debuggable}</manifestDebuggable> <androidManifestFile>target/filtered-resources/AndroidManifest.xml</androidManifestFile> <release>${project.build.release}</release> <run> <debug>${project.build.debug}</debug> </run> <runDebug>${project.build.runDebug}</runDebug> <sign> <debug>${project.build.sign.debug}</debug> </sign> </configuration> </plugin> </plugins> </pluginManagement> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>android-maven-plugin</artifactId> <configuration> <sdk> 15</platform> </sdk> </configuration> </plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>keytool-maven-plugin</artifactId> <version>1.2</version> <!–
–> <!––> <!––> <!–clean –> <!–generateKeyPair –> <!––> <!– generate-resources–> <!––> <!––> <configuration> <keystore>${keystore.filename}</keystore> <storepass>${keystore.storepass}</storepass> <keypass>${keystore.keypass}</keypass> <alias>${keystore.alias}</alias> <dname>CN=iKoding, OU=iKoding, O=iKoding, C=CN</dname> <sigalg>SHA1withDSA</sigalg> <validity>10000</validity> <keyalg>DSA</keyalg> <keysize>1024</keysize> </configuration> </plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF8</encoding> </configuration> </plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> org.eclipse.m2e.core.maven2Nature</projectnature> com.android.ide.eclipse.adt.AndroidNature</projectnature> org.eclipse.jdt.core.javanature</projectnature> </projectnatures> </configuration> </plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> initialize</phase> <goals> <goal>resources</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
<span class="sc3"><span class="re1" style="color: #ffcc00;">
<id>debug</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <filters> <filter>resources/env-debug.properties</filter> </filters> </build> true</project.build.debug> false</project.build.runDebug> true</project.build.proguardSkip> false</project.build.release> true</project.build.sign.debug> <manifest.debuggable>true</manifest.debuggable> </properties> </profile> <id>release</id> false</project.build.debug> false</project.build.runDebug> false</project.build.proguardSkip> true</project.build.release> false</project.build.sign.debug> <manifest.debuggable>false</manifest.debuggable> </properties> <build> <filters> <filter>resources/env-release.properties</filter> </filters> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jarsigner-plugin</artifactId> <version>1.2</version> <executions> <execution> <id>sign</id> <goals> <goal>sign</goal> </goals> package</phase> <inherited>true</inherited> <configuration> <includes> <include>${project.build.outputDirectory}/*.apk</include> </includes> <keystore>${keystore.filename}</keystore> <storepass>${keystore.storepass}</storepass> <keypass>${keystore.keypass}</keypass> <alias>${keystore.alias}</alias> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <!– 渠道profiles –> <id>channel-test</id> <activation> <activeByDefault>true</activeByDefault> </activation> <manifest.metadata.id>test</manifest.metadata.id> <manifest.metadata.channel>test</manifest.metadata.channel> </properties> </profile> <id>channel-91</id> <manifest.metadata.id>91-market</manifest.metadata.id> <manifest.metadata.channel>91 market</manifest.metadata.channel> </properties> </profile> <id>channel-yingyonghui</id> <manifest.metadata.id>yingyonghui-market</manifest.metadata.id> <manifest.metadata.channel>yingyonghui market</manifest.metadata.channel> </properties> </profile> <id>channel-tongbutui</id> <manifest.metadata.id>tongbutui-market</manifest.metadata.id> <manifest.metadata.channel>tongbutui market</manifest.metadata.channel> </properties> </profile> <id>channel-tengxun</id> <manifest.metadata.id>tengxun-market</manifest.metadata.id> <manifest.metadata.channel>tengxun market</manifest.metadata.channel> </properties> </profile> <id>channel-anzhi</id> <manifest.metadata.id>anzhi-market</manifest.metadata.id> <manifest.metadata.channel>anzhi market</manifest.metadata.channel> </properties> </profile> <id>channel-gfan</id> <manifest.metadata.id>gfan</manifest.metadata.id> <manifest.metadata.channel>gfan</manifest.metadata.channel> </properties> </profile> </profiles> </project>
以上的pom文件基本上涉及到了一个Android应用构建过程中的各个方面,以下针对其中比较重要的一些点作简要说明。
- <i>*依赖管理
这也是我们使用Maven构建Android项目的重要原因之一,值得注意的是以下部分:
<div class="codecolorer-container xml vibrant .codecolorer-container table td.line-numbers div {width:auto;}" style="color: #ffffff;">
<div class="xml codecolorer">
<span class="sc3"><span class="re1" style="color: #ffcc00;"><dependency<span class="re2">></span></span></span>
<groupId>com.ikoding.android</groupId> <artifactId>android-base</artifactId> <version>2.0.0</version> <type>apklib</type> </dependency>
💬 评论