`
xyz_lmn
  • 浏览: 62802 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

ant 实现批量打包android应用

 
阅读更多

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

一、添加渠道包信息

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

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

 <meta-data android:value="000000" android:name="CHANNEL"/>

二、渠道包读取

public static String getChanel(Context ctx){
		String CHANNELID="000000";
		try {
	           ApplicationInfo  ai = ctx.getPackageManager().getApplicationInfo(
	        		   ctx.getPackageName(), PackageManager.GET_META_DATA);
	           Object value = ai.metaData.get("");
	           if (value != null) {
	        	   CHANNELID= value.toString();
	           }
	       } catch (Exception e) {
	           //
	       }
		
		return CHANNELID;
	}


三、自动打包实现

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

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

taskdef resource="net/sf/antcontrib/antcontrib.properties">
  <classpath>
	<pathelement location="lib/ant-contrib-1.0b3.jar"/>
  </classpath>
</taskdef>
 <target name="deploy">
   <foreach target="modify_manifest" list="${market_channels}" param="channel" delimiter=",">
   </foreach>
 </target>
<target name="modify_manifest">
	<replaceregexp flags="g" byline="false">
	<regexp pattern="android:value="(.*)" android:name="CHANNEL"" />
	<substitution expression="android:value="${channel}" android:name="CHANNEL"" />
	<fileset dir="" includes="AndroidManifest.xml" />
	</replaceregexp>
	<property name="out.release.file"
				  location="${out.absolute.dir}/${ant.project.name}_${channel}_${app_version}.apk" />
	<antcall target="release" />
</target>

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

build.properties文件增加:

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

build.properties文件增加:

  market_channels=000000,012345 
  app_version=1.2.1

market名称用逗号分隔

执行ant deploy即可。

/**
* @author 张兴业
* 邮箱:xy-zhang#163.com
* android开发进阶群:278401545
*
*/

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics