本文主要是介绍Ant 自动打包配置过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- ant 安装网上很多这里就不多说,自己百度,这里只是提示下下,环境变量问题
path里最好直接用绝对路径配置,ANT_HOME 有时会不识别,具体原因未知。
Ant 打包apk步骤如下:
- 生成build.xml文件
- 在dos下,进入到我们项目的根目录,执行 android update project -n test1 -p . 命令(注意-n表示项目的名称,-p参数后面有个点 表示当前目录)。
- 执行命令后,会在我们的项目下,生成build.xml和local.properties文件。
- build.xml 内容如下
<?xml version="1.0" encoding="UTF-8"?>
<project name="test1" default="help"><!-- The local.properties file is created and updated by the 'android' tool.It contains the path to the SDK. It should *NOT* be checked intoVersion 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.dirThe name of the source directory. Default is 'src'.out.dirThe name of the output directory. Default is 'bin'.For other overridable properties, look at the beginning of the rulesfiles in the SDK, at tools/ant/build.xmlProperties related to the SDK location or the project target shouldbe updated using the 'android' tool with the 'update' action.This file is an integral part of the build system for yourapplication and should be checked into Version Control Systems.--><!-- 我们在项目的根目录自己创建 --><property file="ant.properties" /><!-- 替换start --><target name="buildTime"><replaceregexp byline="false" file="./TestProxy.java" replace="public static int mProxy = 34"/></target><!-- 替换end --><!-- if sdk.dir was not set from one of the property file, thenget it from the ANDROID_HOME env var.This must be done before we load project.properties sincethe 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 librarydependencies. 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 yourapplication and should be checked into Version Control Systems. --><loadproperties srcFile="project.properties" /><!-- quick check on sdk.dir --><failmessage="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>
- custom_rules.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules" ><!-- 引用ant-contlib这个扩展包,声明一下 --><taskdef resource="net/sf/antcontrib/antcontrib.properties" ><classpath><pathelement location="${ant.ANT_HOME}/lib/ant-contrib-1.0b3.jar" /></classpath></taskdef><!-- 引用xmltask --><taskdef name="xmltask"classname="com.oopsconsultancy.xmltask.ant.XmlTask"classpath="./xmltask-1.16.jar" /><!-- 定义一个时间变量,打完包后跟渠道号一起命名apk --><tstamp><formatpattern="yyyyMMddhhmm"property="pktime"unit="hour" /></tstamp><!-- 创建apk存放目录 --><mkdir dir="${apk.dir}" ></mkdir><!-- 替换参数 然后打包APK --><target name="replace_parameter" ><!-- 替换服务器配置 --><replaceregexpbyline="false"encoding="UTF-8"flags="g" ><!-- 这个是正则表达式匹配appconfig中url的值 --><regexp pattern="url>(.*)</url" /><substitution expression="url>${url}</url" /><filesetdir=""includes="res/xml/appconfig.xml" /></replaceregexp><replaceregexpbyline="false"encoding="UTF-8"flags="g" ><!-- 这个是正则表达式匹配appconfig中url_update的值 --><regexp pattern="url_update>(.*)</url_update" /><substitution expression="url_update>${url_update}</url_update" /><filesetdir=""includes="res/xml/appconfig.xml" /></replaceregexp></target><!-- 打包测试环境命令就用这个 --><target name="deploytest" ><!-- 传服务器配置参数到 replace_parameter这个打包target --><antcall target="replace_parameter" ><paramname="url"value="${test.server.url}" /><paramname="url_update"value="${test.server.url.update}" /></antcall><echo message="------ init target run --------- ${test.server.url}" /> <!-- 执行循环打包target foreach_replacechannel --><antcall target="foreach_replacechannel" ><!-- apk命名时候用到的参数 --><paramname="deploy_environment"value="${test.tag.name}" /></antcall></target><!-- 打包生产环境命令就用这个 --><target name="deployrel" ><!-- 传服务器配置参数到 replace_parameter这个打包target --><antcall target="replace_parameter" ><paramname="url"value="${rel.server.url}" /><paramname="url_update"value="${rel.server.url.update}" /></antcall><!-- 执行循环打包target foreach_replacechannel --><antcall target="foreach_replacechannel" ><!-- apk命名时候用到的参数 --><paramname="deploy_environment"value="${release.tag.name}" /></antcall></target><!-- 循环打包的target --><target name="foreach_replacechannel" ><!-- 开始循环打包,从market_channels参数中取出一个渠道号用channel标识,然后通过正则修改manifest文件 --><foreachdelimiter=","list="${market_channels}"param="channel"target="modify_manifest" ></foreach></target><target name="modify_manifest" ><replaceregexpbyline="false"encoding="UTF-8"flags="g" ><!-- 这个是正则表达式匹配manifest中meta,我用的友盟的统计,我 AndroidManifest中的配置为: -->
<!-- <meta-data android:value="360shichang" android:name="UMENG_CHANNEL" --><regexp pattern="android:value="(.*)" android:name="UMENG_CHANNEL"" /><substitution expression="android:value="${channel}" android:name="UMENG_CHANNEL"" /><filesetdir=""includes="AndroidManifest.xml" /></replaceregexp><xmltask source="AndroidManifest.xml" dest="AndroidManifest.xml" encoding="UTF-8" ><attr path="//manifest" attr="android:versionName" value="${app.version}" /><attr path="//manifest" attr="android:versionCode" value="${app.version.code}"/></xmltask><!-- 这里设置最终生成包的存放目录以及apk的名称,注意这里是文件名称,所以变量中不允许出现违规字符,否则将无法生成最终的apk(会出现output is not valid 的错误) --><propertyname="out.final.file"location="${apk.dir}/${app.name}_${channel}_${deploy_environment}_${pktime}.apk" /><antcall target="clean" /><antcall target="release" /></target></project>
- 打包
- 在dos下执行ant deployrel 成功,会在我们项目的目录下,apk下生成对应的apk如图
- 结束
附上 ant中需要我们手动下载的jar包
https://download.csdn.net/download/u011794223/10892827 xmltask时用
https://mvnrepository.com/artifact/ant-contrib/ant-contrib/1.0b3 通过maven,下载ant-contrib-1.0b3.jar
这篇关于Ant 自动打包配置过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!