现在的位置: 首页 > 综合 > 正文

Ant自动编译打包android项目(二)—-签名与渠道包

2017年12月08日 ⁄ 综合 ⁄ 共 2272字 ⁄ 字号 评论关闭

上篇介绍了怎么使用ant自动编译打包现有的android项目,这篇将继续介绍如果如何在ant打包应用的时候加入签名信息以及自动打包渠道包。

1. 加入签名信息:

在项目的根目录下建一个ant.properties文件,输入如下内容,其中keystore密码和alias密码可以不指定(防泄漏),那么在命令执行的过程中会要求你输入。

[html] view
plain
copy在CODE上查看代码片派生到我的代码片

  1. #keystore的路径,必须使用正斜杠  
  2. key.store=E:/wp_android_sample/me.key  
  3. #keystore的密码  
  4. #key.store.password=*****  
  5. #alias名  
  6. key.alias=me  
  7. #alias密码  
  8. #key.alias.password=******       

在项目根目录下运行 ant release 命令就会帮你生成一个经过签名和aligned的apk,生成的apk(your_project_name-release.apk)在bin目录下

2. 自动打包渠道包:

 实现批量循环打包需要一个类似于for循环的功能,在Ant的核心包里没有相关的For循环的Task,即不支持for循环,但是ant支持第三方扩展包,以支持更多的其他功能。

于是我们要下载相应的支持for循环的扩展包。可以使用开源的Ant-contrib包。下载地址:http://ant-contrib.sourceforge.net/  。

下载后的解压得到的jar文件放到ant的lib目录。接下来我们就可以打包渠道包了,具体做法是:

(1)首先在ant.properties文件中增加属性 market_channels (渠道列表,以逗号分割),version(应用程序版本名)

[html] view
plain
copy在CODE上查看代码片派生到我的代码片

  1. #渠道市场列表  
  2. market_channels=91,360,wandoujia,baidu  
  3. #版本号  
  4. version=1.2.1  

(2)在我们项目的build.xml中加入如下代码:

[html] view
plain
copy在CODE上查看代码片派生到我的代码片

  1. <!-- 渠道包打包脚本  ant deploy-->  
  2.   
  3. <taskdef resource="net/sf/antcontrib/antcontrib.properties">  
  4.     <classpath>  
  5.         <pathelement location="lib/ant-contrib-1.0b3.jar"/>  
  6.     </classpath>  
  7. </taskdef>  
  8.   
  9. <target name="deploy">  
  10.    <foreach target="modify_manifest" list="${market_channels}" param="channel" delimiter=",">  
  11.    </foreach>  
  12. </target>  
  13.   
  14. <target name="modify_manifest">  
  15.     <replaceregexp flags="g" byline="false">  
  16.         <!-- 匹配的内容是 android:value="*****" android:name="UMENG_CHANNEL" -->  
  17.         <regexp pattern='android:value="(.*)" android:name="UMENG_CHANNEL"' />  
  18.         <!-- 匹配之后将其替换为 android:value="渠道名" android:name="UMENG_CHANNEL" -->  
  19.         <substitution expression='android:value="${channel}" android:name="UMENG_CHANNEL"' />    
  20.         <!-- 正则表达式需要匹配的文件为AndroidManifest.xml -->  
  21.         <fileset dir="" includes="AndroidManifest.xml" />  
  22.     </replaceregexp>  
  23.     <property name="out.release.file" location="${out.absolute.dir}/${ant.project.name}_${channel}.apk" />  
  24.     <!--包 -->  
  25.     <antcall target="release" />  
  26.     <!--输出渠道包到bin/out目录下 -->  
  27.     <copy tofile="${out.absolute.dir}/out/${ant.project.name}v${version}-${channel}.apk" file="bin/${ant.project.name}-release.apk"/>  
  28. </target>  


在项目根目录下运行 ant deploy 命令就会帮你各个渠道的签名包了(为了全程可以自动执行,ant.properties文件中的keystore的密码可以指定,这样在执行过程中就不需要手动输入密码了),在bin目录的out目录下。


转自:http://blog.csdn.net/likebamboo/article/details/17953259





抱歉!评论已关闭.