以前因为maven大行其道时,大家都说着ant过时了,所以我想就算要学习构建工具也应该是学习maven才对,每次在osc上看到ant的更新信息时,一方面我在想ant还有人在维护着,一方面想着,这货还活着。
于是我从图书馆借来一本maven实战,然后看了,试了,觉得有一些麻烦,然后就没有用了。说实话折腾时间加起来都超过2-3个星期了。但是最后我还是没有怎么学会,平常还是一直使用eclipse。于是这些构建工具也就显得不那么重要了。
1. 简单的代码修改无需Eclipse,使用vim即可。
直到现在我做了android,直到我想改一点代码打包,而不想打开eclipse(带android插件的eclipse,开起来可不是一般的慢啊。)。于是有了我之前写的 在终端编译运行android项目 。
这个问题,轻松的就解决了。从此,我觉得ant还是可用的。
2.打包签名时不用再每次手动输入密码
后来又用ant解决了android项目打包签名输入密码的问题。
把密码写在ant属性配置文件中,就不用每次都输再次密码了。
具体就是在ant.properties文件中,将签名密码写上,写好后ant.properties文件如下:
# This file is used to override default values used by the Ant build system.
#
# This file must be checked into Version Control Systems, as it is
# integral to the build system of your project.
# This file is only used by the Ant script.
# You can use this to override default values such as
# 'source.dir' for the location of your java source folder and
# 'out.dir' for the location of your output folder.
# You can also use it define how the release builds are signed by declaring
# the following properties:
# 'key.store' for the location of your keystore and
# 'key.alias' for the name of the key to use.
# The password will be asked during the build when you use the 'release' target.
key.store=/home/banxi1988/android/keystore/you_app_keystore
key.alias=key_alias
key.store.password=i_want_tell_you
key.alias.password=you_nerver_know
如果你还没有生成过签名,可以先在eclipse中的项目上右键,android | export signed application...
在打开的导航对话框中,可以方便的生成签名。
值得注意的是,使用ant在终端打包时,如果此时eclipse也开着的话,如果再回去eclipse中的项目的话,
容易出一些编译的错误,这个时间只要使用eclipse重新编译下就好了。
在使用ant打包时,也要注意先clean。
3.可以实现一键打完多个渠道包
使用ant,结合简单的shell脚本,就可以一键将多渠道包打好---我已经记不清,有多少时间浪费在为多个渠道打包了,没有办法在中国你必须这样。
参考了: Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包
我修改得到了我想要的效果shell脚本。
banxi1988@banxi:~/BANXIworkspace/youjia_android_app$ cat mcpkg.sh
#!/bin/bash -x
## channel list
channels="WEB APPCHINA HIAPK GOOGLE_PLAY XIAOMI MEIZU 360 ANZHI BAIDU DUAPP"
### the final apk file the ant will create
releaseApk="youjia_android_app-release.apk"
apkVersionName="v1.4.3"
### the dir to save the final apk
destDir=~/android/release/${apkVersionName}
## if the destDir not exists create it
if [ ! -d ${destDir} ];then
mkdir -p ${destDir}
fi
## build for every channels
for channel in $channels
do
echo build release pkg for channel $channel ...
## replace the channel value in AndroidManifest.xml file using each real channel value
sed -i "s/\(android:value=\)\"\(.*\)\"\( android:name=\"UMENG_CHANNEL\"\)/\1\"${channel}\"\3/g" AndroidManifest.xml
rApk=${releaseApk}-${channel}
destApkFile=${destDir}/${rApk}
echo $destApkFile
## clean release and copy the final apk to dest dir
ant clean && ant release && cp bin/${releaseApk} ${destApkFile}
done
echo SUCCESS BUILD ALL CHANNEL PACKAGE
banxi1988@banxi:~/BANXIworkspace/youjia_android_app$
值得注意的是:上面脚本中,是使用sed这一个 流式行编辑器及正则来替换不同的渠道名。
所以如果你也用的是友盟的话,下面的元素节点要在一行,并且根据上面正则的特点,android:value属性在前,我的如下:
<meta-data android:value="WEB" android:name="UMENG_CHANNEL" />
这个每次打包的时候,修改一下版本名,就可以一键打多个包,这个节约出来的时候,可以去喝一杯。
PS:在写脚本的时候,参考了:我的这篇:http://my.oschina.net/banxi/blog/93336
和这里http://www.cnblogs.com/haivey/archive/2012/09/04/2669870.html
bash中的if和[确实比较坑啊。