多渠道批量打包

来源:互联网 发布:三挫仑在淘宝上叫什么 编辑:程序博客网 时间:2024/05/24 15:40

经过一天的奋战,终于搞定了批量打包,不多说,上代码

多渠道就是按照不同的市场进行发布不同的包,由于要改版本名,所以有的时候要打10多个包,在测试部的人员打一次包久会感觉不好,

先说前提,是运用android sdk进行打包,需要ant,

同时ant核心库本身不支持遍历功能,所以需要引入新库ant-contrib-1.0b3.jar,需要放到ant\lib目录下,我直接放到当前项目目录下。

ant-contrib 下载地址: http://sourceforge.net/projects/ant-contrib/

如果出现如下错误,说明没有放置jar文件
Buildfile: D:\ProjectDemo\build.xml
  [taskdef] Could not load definitions from resource net/sf/antcontrib/antcontri
b.properties. It could not be found.


对现有项目进行编译

android update project --name ProjectDemo --target 8 --path ./  

生成一下一个文件

  1. Updated project.properties  
  2. Updated local.properties  
  3. Added file D:\ProjectDemo\build.xml  
  4. Updated file D:\ProjectDemo\proguard.cfg  

首先要在AndroidManifest.xml文件中加入

  <meta-data android:value="baiduyingyongzhongxin" android:name="CHANNEL"/>
用来表示版本的值

然后再把要替换的值放在配置文件,可以放在ant.properties, project.properties, local.properties等文件,当然如果为了打包与这些文件分离,可以自己创建一个文件,并导入,笔者作为测试,放在了ant.properoties文件中,这里不推荐

market_channels=anzhuoshichang,jifengshichang,baiduyingyongzhongxinapp_version=1_0_build_0 
上面就是需要打包的版本,是三个市场的,也就是说要打3个包

一下是build.xml文件中添加的源码

          <taskdef resource="net/sf/antcontrib/antcontrib.properties" >        <classpath>            <!-- <pathelement location="lib/ant-contrib-1.0b3.jar" /> --><pathelement location="./ant-contrib-1.0b3.jar" />        </classpath>    </taskdef><echo>Run ant-contrib OK</echo>       <target name="deploy">        <foreach target="edit_and_build" list="${market_channels}" param="channel" delimiter=",">        </foreach>     </target>        <target name="edit_and_build">  <echo>Run '${channel}' apk</echo><!--  flags="g" 指定全局替换,替换所有符合规则的字段byline="false" 确认被替换的时候是一次替换一行还是多行 pattern 属性用来指明正则表达式 --><replaceregexp flags="g" byline="false">      <regexp pattern="android:value="(.*)" android:name="CHANNEL""/>     <!--  substitution expression 中是替换的值,替换的值都定义在相对应的配置文件中 -->    <substitution expression="android:value="${channel}" android:name="CHANNEL"" />   <!--  fileset 属性中的 dir 用来指定被替换文件所在的目录includes 用来指定要替换哪个文件。   -->    <fileset dir="" includes="AndroidManifest.xml" />      </replaceregexp>      <property name="out.final.file" location="./${channel}.apk" />               <antcall target="clean" />        <antcall target="release" />
是不是感觉头很大,看不懂,没关系,学知识就是要循序渐进,

推荐两篇文章,学习一些怎么循环读数据,看懂了那两个,再看笔者这个久容易很多了

http://blog.csdn.net/androiddevelop/article/details/11619635

这里面有一个地方

<property name="out.final.file" location="./${channel}.apk" />  
笔者亲测用out.final.file才能生成不同版本的apk,不知道是不是变量名称的改变

http://www.cnblogs.com/stay/archive/2013/05/27/3102027.html这篇讲了比较多的原由



0 0
原创粉丝点击