Ant 自动打包配置过程

2024-06-17 10:18
文章标签 配置 ant 过程 自动 打包

本文主要是介绍Ant 自动打包配置过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • ant 安装网上很多这里就不多说,自己百度,这里只是提示下下,环境变量问题

         path里最好直接用绝对路径配置,ANT_HOME 有时会不识别,具体原因未知。

Ant 打包apk步骤如下:

  • 生成build.xml文件
  1. 在dos下,进入到我们项目的根目录,执行 android update project -n test1 -p . 命令(注意-n表示项目的名称,-p参数后面有个点 表示当前目录)。
  2. 执行命令后,会在我们的项目下,生成build.xml和local.properties文件。
  3. 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>(.*)&lt;/url" /><substitution expression="url>${url}&lt;/url" /><filesetdir=""includes="res/xml/appconfig.xml" /></replaceregexp><replaceregexpbyline="false"encoding="UTF-8"flags="g" ><!-- 这个是正则表达式匹配appconfig中url_update的值 --><regexp pattern="url_update>(.*)&lt;/url_update" /><substitution expression="url_update>${url_update}&lt;/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=&quot;(.*)&quot; android:name=&quot;UMENG_CHANNEL&quot;" /><substitution expression="android:value=&quot;${channel}&quot; android:name=&quot;UMENG_CHANNEL&quot;" /><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>
  • 打包
  1. 在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 自动打包配置过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1069171

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

Jenkins分布式集群配置方式

《Jenkins分布式集群配置方式》:本文主要介绍Jenkins分布式集群配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装jenkins2.配置集群总结Jenkins是一个开源项目,它提供了一个容易使用的持续集成系统,并且提供了大量的plugin满

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存