ant 实现批量打包android应用

  很多的应用中需要加上应用推广的统计,如果一个一个的去生成不同渠道包的应用,效率低不说,还有可能不小心弄错了分发渠道,使用ant可以批量生成应用。

一、添加渠道包信息

       为了统计渠道信息,就不得不在程序的某个地方加入渠道的信息,然后针对不同的渠道打不同的包。一般可以在Manifest文件中加入渠道编号,而不直接写在代码中。这样做的好处是,可以针对不同渠道,自动化去修改Manifest文件中的渠道编号,然后自动为该渠道打包。

Manifest文件支持Meta Data标签,建议使用这种自定义标签。例如下面的文件片段。

 

 

[html]  view plain copy print ?
  1. <meta-data android:value="000000" android:name="CHANNEL"/>  

 

 

二、渠道包读取

[java]  view plain copy print ?
  1. public static String getChanel(Context ctx){  
  2.         String CHANNELID="000000";  
  3.         try {  
  4.                ApplicationInfo  ai = ctx.getPackageManager().getApplicationInfo(  
  5.                        ctx.getPackageName(), PackageManager.GET_META_DATA);  
  6.                Object value = ai.metaData.get("");  
  7.                if (value != null) {  
  8.                    CHANNELID= value.toString();  
  9.                }  
  10.            } catch (Exception e) {  
  11.                //  
  12.            }  
  13.           
  14.         return CHANNELID;  
  15.     }  


 

三、自动打包实现

         Ant编译android程序        简单介绍了使用ant命令打包android程序,实现批量打包需要的加一个类似于for循环的功能即可,在Ant的核心包里没有相关的For循环的Task,要下载相应的扩展包。可以使用开源的Ant-contrib包。下载地址:http://ant-contrib.sourceforge.net/  。下载后的解压得到的jar文件放到ant的lib目录。

        在build.xml中增加如下代码就可以实现批量打包:

 

[html]  view plain copy print ?
  1. taskdef resource="net/sf/antcontrib/antcontrib.properties">  
  2.   <classpath>  
  3.     <pathelement location="lib/ant-contrib-1.0b3.jar"/>  
  4.   </classpath>  
  5. </taskdef>  
  6.  <target name="deploy">  
  7.    <foreach target="modify_manifest" list="${market_channels}" param="channel" delimiter=",">  
  8.    </foreach>  
  9.  </target>  
  10. <target name="modify_manifest">  
  11.     <replaceregexp flags="g" byline="false">  
  12.     <regexp pattern="android:value="(.*)" android:name="CHANNEL"/>  
  13.     <substitution expression="android:value="${channel}" android:name="CHANNEL"/>  
  14.     <fileset dir="" includes="AndroidManifest.xml" />  
  15.     </replaceregexp>  
  16.     <property name="out.release.file"  
  17.                   location="${out.absolute.dir}/${ant.project.name}_${channel}_${app_version}.apk" />  
  18.     <antcall target="release" />  
  19. </target>  

taskdef 声明需要放到较前位置,因为if condition也会用到此声明。

build.properties文件增加:

taskdef 声明需要放到较前位置,因为if condition也会用到此声明。

build.properties文件增加:

 

[html]  view plain copy print ?
  1. market_channels=000000,012345   
  2. app_version=1.2.1  

market名称用逗号分隔

执行ant deploy即可。

 


 http://www.cnitblog.com/zouzheng/archive/2011/01/12/72638.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值