Ant 打包全攻略
2015-11-12 10:10
501 查看
摘要: Ant 构建 Android项目,批量构建多渠道版本
http://ant.apache.org/manualdownload.cgi
注意:/ant-contrib-1.0b3.jar 需要单独下载(http://sourceforge.net/projects/ant-contrib/)
2.配置环境变量
添加 ANT_HOME 的环境变量,添加方法,请自行搜索
说明:android update project -p . -t "你编译的SDK版本"
在项目根目录创建 local.properties (如果不存在的话),配置你的SDK、签名信息,包括路径,别名,密码
2. 准备构建渠道版本所需要的渠道
在项目根目录创建 channels.properties
3. 接下来就是关键的 build.xml 了
打包多渠道,需要在 AndroidManifest.xml 里配置 meta-data ,可以参考
这里需要注意,build.xml 里循环打包的部分,是用正则替换渠道号的,所以 meta-data 不能格式化
这里需要 ant-contrib-1.0b3.jar 用来执行循环操作
打包的最终结果位于项目根目录下 release 文件夹下,下面是全部内容
![](http://static.oschina.net/uploads/space/2015/1112/122806_nInN_778954.png)
![](http://static.oschina.net/uploads/space/2015/1112/122806_CY2A_778954.png)
注:针对 引用了 Library Project 的一样, 命令行定位库工程目录,为库工程添加 Ant构建支持
build.xml 如下
C:\Android\android-sdk\tools\ant\build.xml:653: The following error occurred while executing this line:> C:\Android\android-sdk\tools\ant\build.xml:698: null returned: 1
Also, if eclipse isset to 'build automatically' then this could keep popping up because eclipse will keep regenerating into the bin folder.
just run 'ant clean release' instead of 'ant release' when you are building this.
rm -fR $(find . -type d -name crunch|xargs)
如果遇到上述error 就执行 ant clean releas
2.invalid resource directory name: E:\gemeite_project\znsq_android\dev\SmartLibrary\bin\res crunch
如果遇到上述,请执行 ant clean
总结:Ant构建的方式效率低,谷歌已经推 Gradle了,我们也要跟上谷歌的脚步了
一、Ant 前期准备
下载Anthttp://ant.apache.org/manualdownload.cgi
注意:/ant-contrib-1.0b3.jar 需要单独下载(http://sourceforge.net/projects/ant-contrib/)
2.配置环境变量
添加 ANT_HOME 的环境变量,添加方法,请自行搜索
二、为Project 添加 ANT Build支持
进入命令行模式,并切换到项目目录,执行如下命令:android update project -p . -t "android-19"
说明:android update project -p . -t "你编译的SDK版本"
三、编写构建脚本
首先需要准备构建所需要的签名信息在项目根目录创建 local.properties (如果不存在的话),配置你的SDK、签名信息,包括路径,别名,密码
# 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=E:\\adt-bundle-windows-x86\\sdk #keystore key.store=XXX\\gemeite.keystore key.alias=XXX key.store.password=XXX key.alias.password=XXX
2. 准备构建渠道版本所需要的渠道
在项目根目录创建 channels.properties
#market_channels=Baidu,Tencent,AppChina,HiApk,AnZhi,91Android,Gfan,Wandou,360,163,Xiaomi,Sohu,Angeeks,Nduoa,Mumayi,3G,Hicloud,Xunlei market_channels=Tencent app_version=v1.0.0
3. 接下来就是关键的 build.xml 了
打包多渠道,需要在 AndroidManifest.xml 里配置 meta-data ,可以参考
这里需要注意,build.xml 里循环打包的部分,是用正则替换渠道号的,所以 meta-data 不能格式化
<!-- 渠道商编号,其中 name 请不要改,将 value 修改为渠道商编号。渠道名称请到 mtj.baidu.com的渠道管理中修改 --> <meta-data android:name="BaiduMobAd_CHANNEL" android:value="Tencent" />
这里需要 ant-contrib-1.0b3.jar 用来执行循环操作
打包的最终结果位于项目根目录下 release 文件夹下,下面是全部内容
<?xml version="1.0" encoding="UTF-8"?> <project name="SmartCommunity" default="deploy" basedir="."> <!-- 渠道配置 --> <property file="channels.properties" /> <!-- 使用第三方的ant包,使ant支持for循环 --> <taskdef resource="net/sf/antcontrib/antcontrib.properties"> <classpath> <pathelement location="${ant.dir}/lib/ant-contrib-1.0b3.jar"/> </classpath> </taskdef> <!-- 循环打包 --> <target name="deploy"> <foreach target="modify_manifest" list="${market_channels}" param="channel" delimiter=","> </foreach> </target> <!-- 循环修改AndroidManifest.xml --> <target name="modify_manifest"> <!-- 正则匹配替换渠道号 --> <!-- <replaceregexp flags="g" byline="false" encoding="UTF-8"> --> <!-- <regexp pattern='meta-data android:name="JPUSH_CHANNEL" android:value="(.*)"' /> --> <!-- <substitution expression='meta-data android:name="JPUSH_CHANNEL" android:value="${channel}"' /> --> <!-- <fileset dir="" includes="AndroidManifest.xml" /> --> <!-- </replaceregexp> --> <replaceregexp flags="g" byline="false" encoding="UTF-8"> <regexp pattern='meta-data android:name="BaiduMobAd_CHANNEL" android:value="(.*)"' /> <substitution expression='meta-data android:name="BaiduMobAd_CHANNEL" android:value="${channel}"' /> <fileset dir="" includes="AndroidManifest.xml" /> </replaceregexp> <!-- 执行打包 --> <antcall target="release" /> <!-- 拷贝新打包的app文件 --> <echo>Copy released app file.</echo> <copy file="${basedir}/bin/${ant.project.name}-release.apk" tofile="${basedir}/app-release/${ant.project.name}_${channel}_${app_version}.apk" overwrite="true"></copy> </target> <!-- The local.properties file is created and updated by the 'android' tool. It contains the path to the SDK. It should *NOT* be checked into Version Control Systems. --> <property file="local.properties" /> <!-- The ant.properties file can be created by you. It is only edited by the 'android' tool to add properties to it. This is the place to change some Ant specific build properties. Here are some properties you may want to change/update: source.dir The name of the source directory. Default is 'src'. out.dir The name of the output directory. Default is 'bin'. For other overridable properties, look at the beginning of the rules files in the SDK, at tools/ant/build.xml Properties related to the SDK location or the project target should be updated using the 'android' tool with the 'update' action. This file is an integral part of the build system for your application and should be checked into Version Control Systems. --> <property file="ant.properties" /> <!-- if sdk.dir was not set from one of the property file, then get it from the ANDROID_HOME env var. This must be done before we load project.properties since the proguard config can use sdk.dir --> <property environment="env" /> <condition property="sdk.dir" value="${env.ANDROID_HOME}"> <isset property="env.ANDROID_HOME" /> </condition> <condition property="ant.dir" value="${env.ANT_HOME}"> <isset property="env.ANT_HOME" /> </condition> <!-- The project.properties file is created and updated by the 'android' tool, as well as ADT. This contains project specific properties such as project target, and library dependencies. Lower level build properties are stored in ant.properties (or in .classpath for Eclipse projects). This file is an integral part of the build system for your application and should be checked into Version Control Systems. --> <loadproperties srcFile="project.properties" /> <!-- quick check on sdk.dir --> <fail message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable." unless="sdk.dir" /> <!-- Import per project custom build rules if present at the root of the project. This is the place to put custom intermediary targets such as: -pre-build -pre-compile -post-compile (This is typically used for code obfuscation. Compiled code location: ${out.classes.absolute.dir} If this is not done in place, override ${out.dex.input.absolute.dir}) -post-package -post-build -pre-clean --> <import file="custom_rules.xml" optional="true" /> <!-- Import the actual build file. To customize existing targets, there are two options: - Customize only one target: - copy/paste the target into this file, *before* the <import> task. - customize it to your needs. - Customize the whole content of build.xml - copy/paste the content of the rules files (minus the top node) into this file, replacing the <import> task. - customize to your needs. *********************** ****** IMPORTANT ****** *********************** In all cases you must update the value of version-tag below to read 'custom' instead of an integer, in order to avoid having your file be overridden by tools such as "android update project" --> <!-- version-tag: 1 --> <import file="${sdk.dir}/tools/ant/build.xml" /> </project>
四、执行构建
命令行定位到项目根目录,执行如下命令ant
![](http://static.oschina.net/uploads/space/2015/1112/122806_nInN_778954.png)
![](http://static.oschina.net/uploads/space/2015/1112/122806_CY2A_778954.png)
注:针对 引用了 Library Project 的一样, 命令行定位库工程目录,为库工程添加 Ant构建支持
android update project -p . -t "android-19"
build.xml 如下
<?xml version="1.0" encoding="UTF-8"?> <project name="SmartLibrary" default="release"> <!-- The local.properties file is created and updated by the 'android' tool. It contains the path to the SDK. It should *NOT* be checked into Version Control Systems. --> <property file="local.properties" /> <!-- The ant.properties file can be created by you. It is only edited by the 'android' tool to add properties to it. This is the place to change some Ant specific build properties. Here are some properties you may want to change/update: source.dir The name of the source directory. Default is 'src'. out.dir The name of the output directory. Default is 'bin'. For other overridable properties, look at the beginning of the rules files in the SDK, at tools/ant/build.xml Properties related to the SDK location or the project target should be updated using the 'android' tool with the 'update' action. This file is an integral part of the build system for your application and should be checked into Version Control Systems. --> <property file="ant.properties" /> <!-- if sdk.dir was not set from one of the property file, then get it from the ANDROID_HOME env var. This must be done before we load project.properties since the proguard config can use sdk.dir --> <property environment="env" /> <condition property="sdk.dir" value="${env.ANDROID_HOME}"> <isset property="env.ANDROID_HOME" /> </condition> <!-- The project.properties file is created and updated by the 'android' tool, as well as ADT. This contains project specific properties such as project target, and library dependencies. Lower level build properties are stored in ant.properties (or in .classpath for Eclipse projects). This file is an integral part of the build system for your application and should be checked into Version Control Systems. --> <loadproperties srcFile="project.properties" /> <!-- quick check on sdk.dir --> <fail message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable." unless="sdk.dir" /> <!-- Import per project custom build rules if present at the root of the project. This is the place to put custom intermediary targets such as: -pre-build -pre-compile -post-compile (This is typically used for code obfuscation. Compiled code location: ${out.classes.absolute.dir} If this is not done in place, override ${out.dex.input.absolute.dir}) -post-package -post-build -pre-clean --> <import file="custom_rules.xml" optional="true" /> <!-- Import the actual build file. To customize existing targets, there are two options: - Customize only one target: - copy/paste the target into this file, *before* the <import> task. - customize it to your needs. - Customize the whole content of build.xml - copy/paste the content of the rules files (minus the top node) into this file, replacing the <import> task. - customize to your needs. *********************** ****** IMPORTANT ****** *********************** In all cases you must update the value of version-tag below to read 'custom' instead of an integer, in order to avoid having your file be overridden by tools such as "android update project" --> <!-- version-tag: 1 --> <import file="${sdk.dir}/tools/ant/build.xml" /> </project>
五、错误处理
1.> BUILD FAILED> C:\Android\android-sdk\tools\ant\build.xml:601: The following error occurred while executing this line:>C:\Android\android-sdk\tools\ant\build.xml:653: The following error occurred while executing this line:> C:\Android\android-sdk\tools\ant\build.xml:698: null returned: 1
Also, if eclipse isset to 'build automatically' then this could keep popping up because eclipse will keep regenerating into the bin folder.
just run 'ant clean release' instead of 'ant release' when you are building this.
rm -fR $(find . -type d -name crunch|xargs)
如果遇到上述error 就执行 ant clean releas
2.invalid resource directory name: E:\gemeite_project\znsq_android\dev\SmartLibrary\bin\res crunch
如果遇到上述,请执行 ant clean
总结:Ant构建的方式效率低,谷歌已经推 Gradle了,我们也要跟上谷歌的脚步了
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories