更好的阅读效果,请查看原文地址:
###为什么引入maven构建方式### 做过java后台开发的人员应该都知道,maven使用解决依赖包管理问题的,同时优化测试,打包,部署等流程的.
在android里,
- maven可以管理你的依赖包
- 打包成apklib,管理自己的组件库
- 动态配置你的发布渠道(此点非常方便)
- 签名,打包,混淆一条龙服务.
###开始使用maven###
引入pom.xml
<code>
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
4.0.0 com.test apkDeplorer 1.0.0 apk apkDeplorer com.google.android android 4.1.1.4 provided com.google.android support-v4 r7 apkDeplorer.keystore kison.chen@zaozao kison.chen@zaozao kison-android-app UTF-8 ${project.artifactId}-${project.version}-${manifest.metadata.id} src/main/java . true ../filtered-resources AndroidManifest.xml src/main/resources true **/* **/env-*.properties com.jayway.maven.plugins.android.generation2 android-maven-plugin 3.6.0 true run deploy run install proguard-project.txt ${project.build.proguardSkip} ${manifest.debuggable} target/filtered-resources/AndroidManifest.xml ${project.build.release} ${project.build.debug} ${project.build.runDebug} ${project.build.sign.debug} true com.jayway.maven.plugins.android.generation2 android-maven-plugin 3.8.0 15 org.codehaus.mojo keytool-maven-plugin 1.2 ${keystore.filename} ${keystore.storepass} ${keystore.keypass} ${keystore.alias} CN=iKoding, OU=iKoding, O=iKoding, C=CN SHA1withDSA 10000 DSA 1024 org.apache.maven.plugins maven-compiler-plugin org.apache.maven.plugins maven-resources-plugin 2.6 initialize resources debug true src/main/resources/env-debug.properties true false true false true true release false false false true false false src/main/resources/env-release.properties org.apache.maven.plugins maven-jarsigner-plugin 1.2 sign sign package true ${project.build.outputDirectory}/*.apk ${keystore.filename} ${keystore.storepass} ${keystore.keypass} ${keystore.alias} channel-test true test test channel-91 91-market 91 market channel-yingyonghui yingyonghui-market yingyonghui market channel-tongbutui tongbutui-market tongbutui market channel-tengxun tengxun-market tengxun market channel-anzhi anzhi-market anzhi market channel-gfan gfan gfan
</project> </code>
###安装本地依赖包###
由于国内的第三方库多未以maven形式打包,故我们要手动将jar包安装到本地maven库.(高德地图为例) {% highlight xml %} mvn install:install-file -DgroupId=com.autonavi -DartifactId=libamapv3 -Dversion=v3 -Dfile=/Users/keepcleargas/Downloads/AMapSDKV2Demo/libs/armeabi/libamapv3.so -Dpackaging=so -DgeneratePom=true -Dclassifier=armeabi
mvn install:install-file -DgroupId=com.autonavi -DartifactId=location -Dversion=2.0.0 -Dfile=/Users/keepcleargas/Downloads/AMapSDKV2Demo/libs/MapApiLocation.jar -Dpackaging=jar -DgeneratePom=true
{% endhighlight %} 添加依赖包到pom.xml {% highlight xml %} <dependency> <groupId>com.autonavi</groupId> <artifactId>libamapv3</artifactId> <version>v3</version> <classifier>armeabi</classifier> <scope>runtime</scope> <type>so</type> </dependency> <dependency> <groupId>com.autonavi</groupId> <artifactId>map</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>com.autonavi</groupId> <artifactId>ApiLocation</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>com.autonavi</groupId> <artifactId>ApiSearch</artifactId> <version>2.0.0</version> </dependency> {% endhighlight %}
###android的maven版本构建###
由于在maven central中 android版本只有4.1.1.4
我们需要一个工具来安装新版的android sdk. .
根据Maven Android SDK Deployer的wiki 文案,mvn install -P 4.4
在pom.xml导入 4.4.2的安卓包.
{% highlight xml %}
android android 4.4.2_r3 provided
{% endhighlight %}
###构建中可能出现的问题###
- maven版本问题 使用过程中可能会出现版本问题,笔者这里用的是maven 3.1.1,
com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.8.2
- .9图片问题 {% highlight xml %} ERROR: 9-patch image Project/res/drawable- hdpi/input_03_mid.9.png malformed. [INFO] Must have one-pixel frame that is either transparent or white. [INFO] ERROR: Failure processing PNG image Project/res/drawable-hdpi/input_03_mid.9.png {% endhighlight %}
将.9图片标准化 即可
###命令执行 ###
{% highlight xml %} mvn clean package
打包,但不部署。 mvn clean install
打包,部署并运行。 mvn clean package android:redeploy android:run
这个命令通常用于手机上已经安装了要部署的应用,但签名不同,所以我们打包的同时使用redeploy命令将现有应用删除并重新部署,最后使用run命令运行应用。 mvn android:redeploy android:run
不打包,将已生成的包重新部署并运行。 mvn android:deploy android:run
部署并运行已生成的包,与redeploy不同的是,deploy不会删除已有部署和应用数据。
mvn clean install -Prelease,channel-91
打包签名,的渠道为channel-91的apk {% endhighlight %}
###参考文献###