现在的位置: 首页 > 综合 > 正文

Ant自动批量打包多渠道APK

2019年07月30日 ⁄ 综合 ⁄ 共 4884字 ⁄ 字号 评论关闭

一、准备工作


1. 下载安装Ant
    http://ant.apache.org/bindownload.cgi(官网下载)
    下载完成后解压到指定目录并将bin目录配置到环境变量Path中
2. 下载ant-contrib-1.0b3.jar
    由于Ant自身不支持循环命令,需要下载此扩展包来支持

二、在工程根目录下创建并编写build.xml、ant.properties、project.properties

    示例:
    build.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<project
    name="xuanshangbang"
    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" />
 
    <!--
         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 an env var"
        unless="sdk.dir" />
 
    <!--
     extension targets. Uncomment the ones where you want to do custom work
     in between standard targets
 
 
 
    -->
    <!--
    <target name="-pre-build">
    </target>
    <target name="-pre-compile">
    </target>
 
    
    <target name="-post-compile">
    </target>
 
 
 
 
    -->
 
 
    <!--
         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 -->
 
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" >
 
        <classpath>
 
            <pathelement location="D:/Ant/apache-ant-1.9.4/lib/ant-contrib-1.0b3.jar" /> <!--修改此处路径 -->
        </classpath>
    </taskdef>
 
    <import file="${sdk.dir}/tools/ant/build.xml" /> <!--导入ant的build.xml用于编译apk -->
 
    <target name="deploy" >
     <!--循环打包命令: market_channels(为ant.properties里配置的渠道号,用逗号隔开) -->
     <!--             param(为将上面取出来的一个个的渠道号赋值给channel参数          -->
     <!--             target(类似Makefile,执行目标modify_manifest                -->
        <foreach
            delimiter=","
            list="${market_channels}"
            param="channel"
            target="modify_manifest" >
        </foreach>
    </target>
 
    <target name="modify_manifest" >
 
        <!-- <replaceregexp file="AndroidManifest.xml" encoding="utf-8" match="android:value="(.*)"" replace=""/> -->
 
        <replaceregexp
            byline="false"
            flags="g" >
         <!-- 正则匹配并替换 -->
         <!-- 在代码中要用如下图片所示代码替换 -->
            <regexp pattern="android:name="UMENG_CHANNEL" android:value="(.*)"" />  
   <substitution expression="android:name="UMENG_CHANNEL" android:value="${channel}"" />  
 
            <fileset
                dir=""
                includes="AndroidManifest.xml" />
        </replaceregexp>
        <!-- <property  name="out.release.file" value="${out.absolute.dir}/${channel}.apk"/> -->
 
        <antcall target="release" /> <!-- 调用上面导入的ant里自带的build.xml里面的release目标 -->
 
        <copy tofile="${gos.path}/xuanshangbang_${app_version}_${channel}.apk" >
            <!-- 将每一次循环生成的apk包复制到指定目录并用(包名_版本号_渠道号)方式命名 -->
            <fileset
                dir="${out.absolute.dir}/"
                includes="xuanshangbang-release.apk" />
        </copy>
 
        <echo message="===========================" />
    </target>
 
</project>

注:拷贝build.xml到工程里去的时候, 用此代码替换上述注释需要替换的代码

 
ant.properties代码
<pre name="code" class="plain"><pre name="code" class="plain">application.package=com.xunlei.xuanshangbang  
ant.project.name=xuanshangbang 
java.encoding=utf-8
 
out.absolute.dir=d:/xuanshangbang_apk_channels
gos.path=d:/xuanshangbang_apk_channels/apks
 
#配置签名的路径、密码、别名、别名密码
#注:千万不要在值后面输入额外的空格字符,否则会因为密码不正确而报Build Failed!!!
key.store=D:/xuanshangbang_apk_channels/XuanShangBangKey
key.store.password=xljfqxsb
key.alias=xuanshangbang
key.alias.password=xljfqxsb
app_version=1.0.0.26
#配置渠道号,以逗号隔开输入,不要输入回车符等额外的字符
market_channels=0x11100001,0x11100002,0x11100003,0x11100004,0x11100005,0x11100006,0x11100007,0x11100008,0x11100009,0x1110000A,0x1110000B,0x1110000C,0x1110000D,0x1110000E,0x1110000F,0x11100010,0x11100011,0x11100012,0x11100013,0x11100014,0x11100015,0x11100016,0x11100017,0x11100018,0x11100019,0x1110001A,0x1110001B,0x1110001C,0x1110001D
project.properties代码
#在原来的project.properties最后面加上一句
#记得加上斜杠转义符
sdk.dir=D:\\ADT_Bundle\\adt-bundle-windows-x86_64-20140702\\sdk

最后, cmd进入工程根目录,执行ant deploy即可


抱歉!评论已关闭.