最近项目中须要添加应用渠道,我使用的是友盟统计,对于不一样渠道须要编译不一样版本,对于开发者说编译一次,手动操做仍是能够接受的,可是项目发布版本频率较高,并且渠道不少,这就是一个体力活,并且手动打包还比较容易出错,因此就想到了用脚本打包。 java
利用脚本打包的原理就是把项目中的配置文件给覆盖,而后再次编译的时候,就是你要的apk了。 python
对于eclipse中项目,可使用ant来编译,android的sdk中自带一个ant的build.xml文件,因此直接使用就能够了,在使用以前须要配置一下。 android
首先定义一个ant的配置文件,放在eclipse的项目目录中。 windows
ant.properites 数组
out.path=./build/out out.absolute.dir=./build/compile out.config.path=./build_config # sdk的目录,注意是“/”不是“\” sdk.dir=E:/tool/android-sdk_r22.2.1-windows/android-sdk-windows # 项目的package application.package=com.example.test # 项目名称 ant.project.name=test java.encoding=utf-8 # 签名的信息 key.store=*** key.store.password=*** key.alias=*** key.alias.password=*** # 应用的版本,和渠道 app_version=1.0 channel=wandoujia
build.xml app
<?xml version="1.0" encoding="UTF-8"?> <project name="test" default="deploy"> <property file="ant.properties" /> <loadproperties srcFile="project.properties" /> <import file="${sdk.dir}/tools/ant/build.xml" /> <tstamp prefix="time"> <format property="time" pattern="yyyyMMddHHmmss" /> </tstamp> <target name="deploy"> <antcall target="release" /> <copy tofile="${out.path}/${ant.project.name}_${channel}_${time.time}.apk"> <fileset dir="${out.absolute.dir}/" includes="${ant.project.name}-release.apk" /> </copy> <delete includeEmptyDirs="true"> <fileset dir="${out.absolute.dir}" includes="**/*" /> </delete> </target> </project>
只要运行deploy的任务就能够完成编译。
上面的配置文件中的渠道只有豌豆荚一个,那么怎么使用多个渠道呢?开发人员很容易就想到了,咱们能够顶一个渠道数组,而后遍历数组,分别编译打包。虽然耗时比较长可是不容易出错,相对于手动编译仍是比较好的。 eclipse
在实际项目中我问了下年长的同事,说ant中可使用for循环不,可是须要添加额外的jar包(ant-contrib-1.0b3.jar)来支持,我就觉的比较麻烦,而后本身就写了一个python的脚步来自动替换ant的配置文件,这样我又多了几个文件。若是想了解ant的for标签使用能够百度一下,这里很少作介绍。其实我也不懂, python2.7
#market_channels default hiapk baidu 360 tencent nduo 91 wandoujia
下面是python的脚步build.py。 ide
configname = "python_build.cfg"; antconfigname = "ant.properties"; channels = []; def loadChannels(): global channels; configfile = file(configname); while True: line = configfile.readline(); if len(line) == 0: break; line = line.strip(); if line.startswith('#'): pass; else: channels.append(line); configfile.close(); def replaceChannel(channel): input = open(antconfigname); lines = input.readlines(); input.close(); output = open(antconfigname,"w"); for line in lines: if not line: break; if line.startswith('channel='): temp = 'channel='+channel; output.write(temp); else: output.write(line); output.close(); #==============main begin ========================= import os; loadChannels(); for channel in channels: replaceChannel(channel); os.system("ant"); print ('channel '+channel+' is build success......');
python是一个比较流行的脚步,入手仍是比较简单的,我也只是周末的时候,看了几个小时,敲了几回代码就写了上面的脚本,固然可能还有不足的地方,但愿有大神看到不要喷我,我这边使用的是python2.7,使用前须要安装python才行。 函数
上面分别定义了2个函数,=======下面的就是开始运行脚本,python里面是没有main的概念,同时还不支持{},百度来的,好像是发明这个脚本的人,但愿强制使用缩进来控制代码块,对一个习惯了用ide来写代码的的人仍是比较痛苦滴,。不过我再网上找到了一个比较好的python的ide,叫UliPad。有兴趣的童鞋能够去使用下。
首先是替换渠道名称,当首先须要读取配置文件,使用的是python中文件模块,每次读取一行,若是开始时“#”则认为是注释,不使用,不然加入到一个数组里面,最后遍历数组,每次执行的时候,都替换ant里面的channel一行,而后调用cmd命了,来执行ant。最后完成了渠道打包的功能。
使用cmd,进入到eclipse的项目目录下面,执行python build.py就能够了。
小弟也是开发android不久,写的很啰嗦,望各位看官见谅。
下面是一些截图。