打包流程:(转载文章 描述了一下我碰到的问题)
一.打包前准备工作
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为渠道集合。
备注:
1.我用的命令是:<sdk>/tools/android update project --name -t <target> -p <project>
例如: /Users/ningwang/Android/adt-bundle-mac-x86_64-20130729/sdk/tools/android update project --name sdmtv_android4 -t 5 -p /Users/ningwang/Android/sdmtv_android4
写 -t 18会遇到 Error: Target id '18' is not valid. Use 'android list targets' to get the target ids. 于是此处的API版本我先写上了5就此跳过。
2.写keystore文件签名的时候 注意不要有中文路径。
3.app_version 可写可不写,我暂时没写这一项。
4. 如果有使用lib项目, 把build.xml复制到lib项目去,修改下对应的project name即可。
执行 ant release的时候可能会遇到第三方包错误 关闭eclipse 执行.rm -fR $(find . -type d -name crunch|xargs) 删除crunch文件夹
三.说下build.xml的编写方法:
在ant.properties 文件末尾加上 market_channels=MMMarket,GFan,AppChina,91Assistant,NDuo,3G,MuMaYi,MyApp
在build.xml 文件的末尾加上
[html] view plaincopy
- <property name="out.unaligned.dir" value="bin/${ant.project.name}_release"/>
- <property name="xml.temp.folder" value="bin/temp"]]]]></property]]]]>
- <mkdir dir="${out.unaligned.dir}" />
- <target name="modify_update_file" ]]]]>
- <echo]]]]>*********************** make channel ${channel}</echo]]]]>
- <replaceregexp
- byline="false"
- encoding="utf-8"
- file="AndroidManifest.xml"
- match="channel_value"
- replace="${channel}" />
- <property name="out.unaligned.file" location="${out.unaligned.dir}/${ant.project.name}_release_${channel}.apk" />
- </target]]]]>
- <target name="make_one_channels" depends="savemanifest, modify_update_file, release, replacemanifest, delete_temp_folder"
- description="description" ]]]]>
- </target]]]]>
- <target name="replacemanifest" ]]]]>
- <echo]]]]>*********************** replacemanifest</echo]]]]>
- <delete file="${basedir}\AndroidManifest.xml" />
- <copy encoding="utf-8" file="${xml.temp.folder}\AndroidManifest.xml"
- todir="${basedir}" />
- </target]]]]>
- <target name="savemanifest" ]]]]>
- <echo]]]]>*********************** savemanifest</echo]]]]>
- <copy encoding="utf-8" file="${basedir}\AndroidManifest.xml" todir="${xml.temp.folder}" />
- </target]]]]>
- <target name="deletebin" ]]]]>
- <delete dir="${basedir}\bin" />
- </target]]]]>
- <target name="delete_temp_folder"]]]]>
- <delete dir="${xml.temp.folder}"]]]]></delete]]]]>
- </target]]]]>
- <taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach" classpath="libs\ant-contrib-1.0b3.jar" />
- <target name="make_channels" ]]]]>
- <foreach delimiter="," list="${market_channels}" param="channel" target="make_one_channels" ]]]]></foreach]]]]>
- </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)make_channels的target是ant的入口,该target中使用foreach循环调用名为make_one_channels的target 并把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,当时做这块的时候碰到的问题)
四.打包方法的使用
打开终端(cmd),执行:
cd /Users/moushou/Documents/workspace/HelloWorld
然后执行:
(1) rm -fR $(find . -type d -name crunch|xargs)
(2) ant clean
(3) ant make_channels
备注:
在执行ant make_channels 的时候可能会碰到 javac编译时出现“非法字符:\65279” 的问题 解决方案是对这个java文件重新字符集设置。也可以重新创建这个class文件 然后以UTF-8的形式对这个文件保存。
最终可以打出带签名的各个渠道的APK包。