Android ant 批量打包研究

来源:互联网 发布:数据库结构设计 编辑:程序博客网 时间:2024/06/05 03:50

通常开发Android程序需要发布到很多不同的电子市场提供下载,而统计不同电子市场的下载使用量就需要设置不同的渠道值。

很苦恼的问题是,市面上很少可用的批量打包(即替换AndroidManifest.xml中的渠道值,生成对应渠道名的包)小工具。


还是自己研究ant打包原理,自己动手写一个小工具吧。

现在可以实现的用dos批量打包,但还需要加一个友好的UI界面,做的更通用一些。现在先PO上目前的进度吧。

参考了http://www.cnblogs.com/stay/archive/2013/05/27/3102027.html 的很大一部分,在这里表示感谢!


首先,用ant打包需要配置ant,这里呢,就不说了,网上很多,这里给个链接吧http://www.cnblogs.com/yuzhongwusan/archive/2013/03/26/2982411.html

安装成功之后,在dos下输入ant,可能会提示build.xml 不存在的错误,那,就需要我们自己手动生成了

生成build.xml需要运行dos进入SDK/tools文件夹下,

运行 android update project  -p 项目路径 这个命令

比如:

android update project -n test -p E:\android\workspace\test
-n 对应的是项目名称
-p 就是生成的路径

生成build.xml内容,如下

<?xml version="1.0" encoding="UTF-8"?><project name="test" default="help">    <!-- The local.properties file is created and updated by the 'android' tool.         It contains the path to the SDK. It should *NOT* be checked into         Version Control Systems. -->    <property file="local.properties" />    <!-- The ant.properties file can be created by you. It is only edited by the         'android' tool to add properties to it.         This is the place to change some Ant specific build properties.         Here are some properties you may want to change/update:         source.dir             The name of the source directory. Default is 'src'.         out.dir             The name of the output directory. Default is 'bin'.         For other overridable properties, look at the beginning of the rules         files in the SDK, at tools/ant/build.xml         Properties related to the SDK location or the project target should         be updated using the 'android' tool with the 'update' action.         This file is an integral part of the build system for your         application and should be checked into Version Control Systems.         -->    <property file="ant.properties" />    <!-- if sdk.dir was not set from one of the property file, then         get it from the ANDROID_HOME env var.         This must be done before we load project.properties since         the proguard config can use sdk.dir -->    <property environment="env" />    <condition property="sdk.dir" value="${env.ANDROID_HOME}">        <isset property="env.ANDROID_HOME" />    </condition>    <!-- The project.properties file is created and updated by the 'android'         tool, as well as ADT.         This contains project specific properties such as project target, and library         dependencies. Lower level build properties are stored in ant.properties         (or in .classpath for Eclipse projects).         This file is an integral part of the build system for your         application and should be checked into Version Control Systems. -->    <loadproperties srcFile="project.properties" />    <!-- quick check on sdk.dir -->    <fail            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."            unless="sdk.dir"    />    <!--        Import per project custom build rules if present at the root of the project.        This is the place to put custom intermediary targets such as:            -pre-build            -pre-compile            -post-compile (This is typically used for code obfuscation.                           Compiled code location: ${out.classes.absolute.dir}                           If this is not done in place, override ${out.dex.input.absolute.dir})            -post-package            -post-build            -pre-clean    -->    <import file="custom_rules.xml" optional="true" />    <!-- Import the actual build file.         To customize existing targets, there are two options:         - Customize only one target:             - copy/paste the target into this file, *before* the               <import> task.             - customize it to your needs.         - Customize the whole content of build.xml             - copy/paste the content of the rules files (minus the top node)               into this file, replacing the <import> task.             - customize to your needs.         ***********************         ****** IMPORTANT ******         ***********************         In all cases you must update the value of version-tag below to read 'custom' instead of an integer,         in order to avoid having your file be overridden by tools such as "android update project"    -->    <!-- version-tag: 1 -->    <import file="${sdk.dir}/tools/ant/build.xml" /></project>

同时也生成了 local.properties 这个文件,里面可以设置

sdk.dir=E:\\android\\sdk                                                           //SDK路径(注意转义字符)
key.store=E:\\android\\workspace\\test\\test.keystore              //keystore路径
key.store.password=111111                                                    //keystore密码
key.alias=snkso                                                                         //keystore设置的别名
key.alias.password=111111                                                      //别名密码
channels=cs_001,cs_002,cs_003                                              //设置的不同渠道
apks=E:\\android\\workspace\\test\\apks                                 //生成包的路径


build.xml中调用的 local.properties 和 custom_rules.xml 文件

生成了local.properties 文件,需要咱们自己新建custom_rules.xml这个文件,里面主要起到循环替换渠道号并生成不同渠道号包的作用。

custom_rules.xml代码如下:

<?xml version="1.0" encoding="utf-8"?><project name="custom_rules">        <!-- 声明loop 直接调用ant的循环功能 -->    <taskdef resource="net/sf/antcontrib/antcontrib.properties">        <classpath>            <pathelement location="lib/ant-contrib-1.0b2.jar"/>        </classpath>    </taskdef>        <target name="deploy" >        <foreach            delimiter=","            list="${channels}"            param="channel"            target="modify_manifest" >        </foreach>    </target>        <target name="modify_manifest" >       <replaceregexp flags="g" byline="false">        <regexp pattern="android:name="CHANNEL" android:value="(.*)"" />              <substitution expression="android:name="CHANNEL" android:value="${channel}"" />             <fileset                dir=""                includes="AndroidManifest.xml" />        </replaceregexp>        <property            name="out.final.file"            location="${apks}/test_${channel}.apk" />        <antcall target="clean" />        <antcall target="release" />    </target>    </project>

这里需要调用ant-contrib-1.0b2.jar这个包,可以在附件中下载http://download.csdn.net/detail/u010538991/8235521

记住不要放到项目的 libs 文件夹下,项目中调用不到会报错的。


自己可以修改custom_rules.xml 文件的为自己想要的代码。

运行ant使用下面代码:

dos中进入项目目录下运行 ant deploy 就可以批量运行了。

P.S.如果批量运行中,只跑了一次之后就报错退出,请检查AndroidManifest.xml这个文件中注释中是不是出现了乱码,把出现乱码的地方删掉就好了。


删掉<!-- 渠道? -->之后





参考文章

ant官网http://ant.apache.org/

1 0
原创粉丝点击