ANT批量打包笔记


   由于目前渠道很多,需要批量打包,一般是window下打包,但是mac下如何批量打包呢?其实挺麻烦的,具体步骤如下:

    

一.打包前准备工作

1.首先确定你的JDK版本为1.6!

2.在AndroidManifest.xml中application标签下添加一个用来识别渠道的标签:

<meta-data  android:name="qudao"  android:value="channel" />

3.为了让ant支持循环功能,我们要在Android SDK/tools/lib下放一个ant-contrib-1.0b3.jar包

4.项目中放置第三方jar包的文件夹必须叫libs而不是lib

 

二.build.xml等文件的生成和配置

1.通过终端(cmd)命令自动生成build.xml和local.properties两个文件,方法如下:

<sdk>/tools/android update project -p <project> -t <target>

例如:

/Users/moushou/Downloads/AndroidSDK/tools/android update project -p /Users/moushou/Documents/workspace/HelloWorld -t 14

其中<sdk>为SDK全路径,<project>为项目全路径,<target>为API版本。

执行完成截图如下:

执行完成后,Refresh你的项目就会发现项目的根目录下多了两个文件:build.xml和local.properties

其中local.properties的内容是:


# This file is automatically generated by Android Tools.

# Do not modify this file -- YOUR CHANGES WILL BE ERASED!

#

# This file must *NOT* be checked into Version Control Systems,

# as it contains information specific to your local configuration.


# location of the SDK. This is only used by Ant

# For customization when using a Version Control System, please read the

# header note.

sdk.dir=/Users/moushou/Downloads/AndroidSDK


project.properties的内容如下:


# This file is automatically generated by Android Tools.

# Do not modify this file -- YOUR CHANGES WILL BE ERASED!

#

# This file must be checked in Version Control Systems.

#

# To customize properties used by the Ant build system use,

# "ant.properties", and override values to adapt the script to your

# project structure.

#proguard.config=proguard.cfg

# Project target.

target=Google Inc.:Google APIs:14

proguard.config=proguard.cfg


项目的目录结构如下图所示:

:project.properties中target=GoogleInc.:GoogleAPIs:14代表所使用的SDK的版本,可进行手动修改。

2.手动为项目新建一个File,该文件名为:ant.properties,创建完成项目的目录结构如下图:

创建完成后在ant.properties中添加如下内容:


key.store=<keystore>

key.alias=<key>

key.store.password=<keystore pwd>

key.alias.password=<key pwd>

market_channels=xx,yy,zz

app_version=1_0_build_0


例如:


key.store=/Users/moushou/Desktop/qianming

key.alias=meilihuaduo

key.store.password=123456xx

key.alias.password=123456xx 

market_channels=anzhuoshichang,jifengshichang,baiduyingyongzhongxin

app_version=1_0_build_0


其中:

keystore为签名文件的全路径。

key.alias为签名需要使用的私钥。

key.store.password为私钥库的密码。

key.alias.password为私钥的密码。

market_channels为渠道集合。

app_version为apk的版本(此字段可根据自己喜好编写)。

 

--------------------------------------------------------------------------------------------------

 

至此,除build.xml外,其余文件配置完成。

三.build.xml的编写方法:

1.修改build.xml的第二行,修改方法如下:

<project name="HelloWorld" default="release">

其中name为你项目的名称,default设置为release。

2.循环替换AndroidManifest.xml中qudao的value值并进行自动签名打包,方法如下:


 1 <import file="${sdk.dir}/tools/ant/build.xml" />

 2     <property name="out.unaligned.dir" value="/Users/moushou/Desktop/HelloWorld_${app_version}/" />

 3     <mkdir dir="${out.unaligned.dir}" />

 4     <target name="modify_update_file">

 5         <echo>*********************** make channel ${channel}</echo>

 6         

 7         <replaceregexp file="AndroidManifest.xml"

 8             match='channel'

 9             replace='${channel}'

10             byline="false"

11             encoding="utf-8"

12         />

13         <property name="out.unaligned.file" location="${out.unaligned.dir}/HelloWorld_${app_version}_${channel}_android.apk"/>

14         

15     </target>

16 

17     <target name="make_one_channels" depends="savemanifest,modify_update_file,release,replacemanifest,deletebin" description="description">

18     </target>

19 

20     <target name="replacemanifest">

21         <echo>*********************** replacemanifest</echo>

22         <delete file="${basedir}\AndroidManifest.xml"/>

23         <copy file="..\temp\build\META-INF\AndroidManifest.xml" todir="${basedir}" encoding="utf-8"/>

24     </target>

25 

26     <target name="savemanifest">

27         <echo>*********************** savemanifest</echo>

28         <copy file="${basedir}\AndroidManifest.xml" todir="..\temp\build\META-INF" encoding="utf-8" />

29     </target>

30 

31     <target name="deletebin">

32         <delete dir="${basedir}\bin" />

33     </target>

34 

35     <taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach" classpath="/Users/moushou/Downloads/AndroidSDK/tools/lib/ant-contrib-1.0b3.jar" />

36     <target name="make_channels">

37         <foreach target="make_one_channels" list="${market_channels}" delimiter="," param="channel">

38         </foreach>

39     </target>


其中:

1.out.unaligned.dir的value值为apk输出文件夹的绝对路径,文件夹采用HelloWorld结合app_version命名,app_version为ant.properties中的app_version

2.out.unaligned.file的location为apk最终的输出路径,apk命名采用HelloWorld加app_version加当前的channel加android方式

3.说一下打包的过程:

(1)第36行make_channels的target是ant的入口,该target中使用foreach循环调用名为make_one_channels的target(第17行)并把market_channels集合中的每个值替换给channel

(2)make_one_channels的target指定了每次打包的过程:

   savemanifest:打包前先将原始的AndroidManifest.xml复制到与项目同一层级目录下的temp下build下META-INF中

   modify_update_file:匹配到AndroidManifest.xml中的channel并将其替换

   release:自动编译加签名

   replacemanifest:删除AndroidManifest.xml,将temp/build/META-INF中的原始AndroidManifest.xml复制回项目根目录下

   deletebin:删除bin文件(:这步很重要,否则只能打出一个渠道的APK,当时做这块的时候碰到的问题)

4.第35行taskdef标签下的classpath是ant-contrib-1.0b3.jar的绝对路径

 

四.打包方法的使用

打开终端(cmd),执行:

cd /Users/moushou/Documents/workspace/HelloWorld

然后执行:

ant make_channels

此时,打包就开始进行啦!当出现BUILD SUCCESSFUL代表打包成功!如下图所示:


此时你会发现你输出的文件夹中多了三个APK,如下图:


 1.每次打包前一定要删除掉temp/build/META-INF中的AndroidManifest.xml,特别是在给不同项目做打包时

   2.打包前请检查AndroidManifest.xml中qudao的value值是否为channel,特别是打包失败后再次重新打包的时候一定要将value值改为channel

   3.如果打包时出现Cannot recover key错误导致BUILD FAILD的话,请检查ant.properties中key.alias.password的值后面是否有多余的空格!有的话请把空格删除掉!

 

五.在代码中获取渠道值,方法如下:


       try {

                 ApplicationInfo appInfo = getPackageManager().getApplicationInfo

                         (getPackageName(),PackageManager.GET_META_DATA);

                 qudao = appInfo.metaData.getString("qudao");

            } catch (NameNotFoundException e) {

                e.printStackTrace();

            }




错误记录:

输入 ant ,后如果显示上述信息,说明全局变量配置正确。最后一点,大家要注意的是:
1. 每次打包前一定要删除掉 temp/build/META-INF 中的 AndroidManifest.xml ,特别是在给不同项目做打包时
2. 打包前请检查 AndroidManifest.xml qudao value 值是否为 channel ,特别是打包失败后再次重新打包的时候一定要将 value 值改为 channel
3. 如果打包时出现 Cannot recover key 错误导致 BUILD FAILD 的话,请检查 ant.properties key.alias.password 的值后面是否有多余的空格!有的话请把空格删除掉!
好吧,分析到这里,我真的很累了,大家在看的时候,如果有什么疑惑,记得给我留言,我会一一为大家解答。



参考:

http://doc.okbase.net/1244156/archive/118231.html


http://www.eoeandroid.com/thread-325923-1-1.html




************************************************************************

/******

* 获取渠道名称: 

* #ant批量打包时,需要注意: 

* #渠道名称,不能为整型,因为整型andorid会认为它是一个数值,而不是一个字符串

* 例如:00001 android会认证是 1,而不是 00001

* 这是血一般的经验,请注意不要再跳坑!!!

* ********/

    private void printMarketChannels(Context context){

try {

ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(),

PackageManager.GET_META_DATA);

Object obj =  appInfo.metaData.get("MARKET_CHANNEL");

String str = obj.toString();

EMLog.d("TAG", str);


} catch (Exception e) {

EMLog.e("TAG", ""+e.getMessage());

}

    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值