Android 使用 Ant 批量打包

1.先下载安装 Ant,下载地址:http://ant.apache.org/ 点击打开链接

下载完成后,安装:

1)解压Ant,比如解压到D:\download\Ant

2) 我的电脑 -> 属性-> 高级-> 环境变量

3)系统变量新建 ANT_HOME, 变量值为 D:\Android\Ant

4) 系统变量新建或修改 path, 变量值为 %ANT_HOME%\bin

操作结束后,在Dos窗口输入ant 回车,如果出现:

Buildfile: build.xml does not exist!

Build failed

表明 ant 已经安装完毕了。

2. 安装扩展包 Ant_contrib,支持循环打包。

1)到http://sourceforge.net/projects/ant-contrib/files/ant-contrib/ 点击打开链接下载1.0b3

2)从下载的压缩包复制ant-contrib-1.0b3.jar到Ant的lib文件夹中。

3.1 在工程的根目录下创建文件 build.xml.(除此之外还需要新建3个文件ant.properties、local.properties、custom_rules.xml,在build文件中会引用这三个文件,也可以将三个文件内容都放在build.xml中)

内容:

<?xml version="1.0" encoding="UTF-8"?>
<project name="应用名称" default="deploy">


    <!-- 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" />
    <!-- LL改用自己指定的SDK
    <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>

3.2 ant.properties文件内容


application.package= 包名
java.encoding=utf-8


out.absolute.dir= E:/PD/Ant/compiletemp //编译临时文件存放位置
gos.path=E:/PD/Ant/apk/APP2.2 //打包的应用存放位置


key.store=E:/PersonalData/Ant/app.keystore // keystore 存放位置
key.store.password=123456 //keystore密码
key.alias=app
key.alias.password=123456


app_version=2.2 //应用版本号
market_channels=1001,1002(渠道号)

3.3 local.properties 内容

sdk.dir=D:\\soft\\adt-bundle-windows-x86-20130917\\sdk  //SKD安装位置


3.4 custom_rules.xml 内容

 <?xml version="1.0" encoding="utf-8"?>
<project >
    
    
<!-- 声明ant loop ,直接用ant的循环功能,批处理什么的又要多写代码 -->
   <taskdef resource="net/sf/antcontrib/antcontrib.properties" >
        <classpath>
            <pathelement location="${sdk.dir}/tools/lib/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>


<path id="lib-classpath">  
    <fileset dir="${from.dir}/lib"><!--4-->
<include name="**/*.jar"/> 
</fileset> 
<fileset dir="${from.dir}/libs"><!--4-->
       <include name="**/*.jar"/> 
</fileset> 
<fileset dir="${from.dir}/libs/armeabi"><!--4-->
   <include name="**/*.so"/> 
</fileset> 
    </path>  


<!-- 这里相当于一个方法把,(表示ant不会,只能看懂= =) ,以后可以用命令行 ant deploy 来表示批量打包 -->
<!-- ${market_channels} 要在local.properties里声明,并用,来分隔你要打包的channel名 -->
<!-- 比如我的local.properties里是这样写的   market_channels=Google,Gfan,AnZhi,MuMayi -->
    <target name="deploy" >
        <foreach
            delimiter=","
            list="${market_channels}"
            param="channel"
            target="modify_manifest" >
        </foreach>
    </target>


<!-- 修改manifest.xml里的渠道名,如果你要改其他文件,举一反三把 -->
<!-- regexp pattern是正则匹配,这里双引号要用&quot;而不是\ -->
<!-- substitution expression 是你要替换的的channel名-->
<!-- 打包完毕后要把apk移动到一个指定的目录把,你可以在sdk/tools/ant/build.xml搜下out.final.file这个property在哪用到的-->
<!-- <property name="out.final.file" location="${apk.dir}/XXX_{channel}.apk" />  ${apk.dir}表示你要指定的apk目录 XXX表示你要定义apk名和${channel}渠道号-->
<!-- <antcall target="clean" /> <antcall target="release" /> release之前要调下clean,不然以后改的channel名不生效,你懂得-->
    <target name="modify_manifest" >
       <!--  <property
         <replaceregexp flags="g" byline="false">  
    <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;" />  
           <fileset
                dir=""
                includes="AndroidManifest.xml" />
        </replaceregexp>-->
       <!--  <property
            name="out.final.file"
            location="${apk.dir}/XXX_${channel}.apk" />-->
        <replaceregexp flags="g" byline="false" encoding="utf-8">  
<regexp pattern="&lt;string name=&quot;channel&quot;&gt;(.*)&lt;/string&gt;" />  
<substitution expression="&lt;string name=&quot;channel&quot;&gt;${channel}&lt;/string&gt;" />  
  <fileset dir="${resource.absolute.dir}" includes="values/strings.xml" />
        </replaceregexp>
<echo level="info"> ${channel} haahhhahahahahahahahahhahahahahahaahhahahhah</echo>
<!--<replace file="res/values/strings.xml" token="_channel_" value="${channel}" />-->
        <antcall target="clean" />
        <antcall target="release" />


<!--<copy todir="${to.dir}\TestAnt_release_${channel}" >
<fileset dir="${from.dir}" >
  <include name="TestAnt-release.apk" />
</fileset>  
        </copy>--> 
   <copy 
       file="${out.absolute.dir}\应用名称.apk" 
       tofile="${gos.path}\LJHSuperMarket_${app_version}_${channel}.apk" />
    </target>
</project>


4. 完成以上几个步骤以后在 ADE中右键build.xml然后Run as -> ant build以后就可以打包了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值