Android ANT多渠道打包

在没用AndroidStudio的Gradle构建项目之前,多渠道打包一般都是基于ANT构建,所以在此记录一下,以供查阅!

一、文件准备

1、首先要去官网下载ANT代码。

ANT官网下载地址

2、下载ANT循环打包JAR包。

ANT循环JAR包下载地址

网盘下载地址

3、解压得到其中的ant-contrib-1.0b3.jar文件待用。

做完以上3步,ANT多渠道打包所需要的软件就准备完毕了,接下来就是环境配置了。

二、环境配置

1、先将之前下载的ANT文件解压缩到任意文件路径,如D:\Ant目录

2、配置以下环境变量

* 我的电脑->属性->高级->环境变量
* 系统变量新建ANT_HOME,变量值为D:\Ant
* 在path变量下追加以下值,%ANT_HOME%\bin; 3、打开CMD窗口,输入ant build,如果能够看到下面的两句话,说明你得ANT环境已经配置好了。
C:\Users\ZhuWenWu>ant build
Buildfile: build.xml does not exist!
Build failed

4、ANT要支持循环打包需要安装扩展包ant-contrib,将之前解压得到的ant-contrib-1.0b3.jar文件复制到你ANT目录下得lib文件夹下。如:D:\Ant\lib

做完以上4步,ANT多渠道打包环境就基本配置好了,接下来就是真正的要来处理循环打包了。

###三、多渠道打包

总共需要编辑4个文件,分别为build.xml、local.properties、custom_rules.xml、ant.properties. 同时准备好自己的签名文件:android_key.keystore。

1、编辑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的路径地址
sdk.dir=E:\\Android\\android-studio\\sdk  
#项目路径地址
project.dir=F:\\Android\\sourceCode\\Test

2、编辑ant.properties,主要是配置ANT打包的数据

#项目包名
application.package=com.ecloud.test  
#项目名称
ant.project.name=test  
#项目编码,这里需要注意自己工程的编码格式,需要保证编码格式一致
java.encoding=utf-8   

#编译中间文件生成目录
out.absolute.dir=F:/Test/compile  
#最终APK生成文件目录
gos.path=F:/Test/test-code1-20140523  

#签名文件全路径
key.store=F:/Test/Test/android_key.keystore 
#签名文件密码
key.store.password=111111  
#别名,这里要注意如果你签名文件的别名为中文,需要和我这个一样转成16进制,不然签名的时候会报错,转码可以用【UltraEdit工具】(自行百度下载)来做。
key.alias=\u5BB9\u6613\u901B  
#别名密码
key.alias.password=111111  

#软件版本号
app_version=1.0.0
#需要打包的渠道名,注意‘,’分格
market_channels=Gfan,3G,360,AndMarket,AnZi  

3、编辑custom_rules.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules" >
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" >
        <classpath>
        <!--注意这里要和你拷贝的那个JAR文件相同-->
            <pathelement location="lib/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>

    <target name="deploy" >
        <foreach
            delimiter=","
            list="${market_channels}"
            param="channel"
            target="modify_manifest" >
        </foreach>
    </target>

    <target name="modify_manifest" >
       <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}/${ant.project.name}_${channel}.apk" />
        <antcall target="clean" />
        <antcall target="debug" />
    </target>
</project>

4、编辑最终的build.xml文件,这个是才是循环打包的重点

	<?xml version="1.0" encoding="UTF-8"?> <!-- 项目名称test,可用全局替换为当前项目名称 -->
	<project
    	name="test"
    	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" />

    <property
        name="manifest.file"
        value="AndroidManifest.xml" >
    </property>

    <property
        name="bin.dir"
        value="bin" >
    </property>

    <property
        name="absolute-out"
        value="${project.dir}/${bin.dir}" >
    </property>

    <property
        name="absolute-file-manifest-out"
        value="${absolute-out}/${manifest.file}" >
    </property>

    <property
        name="absolute-file-manifest-src"
        value="${project.dir}/${manifest.file}" >
    </property> <!-- 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" />

    <fail
        message="sdk.dir is missing. Make sure to generate local.properties using &apos;android update project&apos; 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" >
<!---->
######注意这里的JAR包路径,要修改成自己的

        <classpath>
            <pathelement location="D:/Ant/lib/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>

    <import file="${sdk.dir}/tools/ant/build.xml" />

    <target name="deploy" >

        <foreach
            delimiter=","
            list="${market_channels}"
            param="channel"
            target="modify_manifest" >
        </foreach>
    </target>   
<!-- -->
######这里就是修改渠道号的代码,我的渠道值设置的是SETTING_UMENG_CHANNEL_CHANNELID_VALUES你可以替换成你自己的

    	<target name="modify_manifest" > <!-- <copy file="${absolute-file-manifest-src}" tofile="${absolute-file-manifest-out}" overwrite="true"/>
        <replace file="${absolute-file-manifest-out}" token="SETTING_UMENG_CHANNEL_CHANNELID_VALUES" value="${channel}" encoding="UTF8"/> -->
        
        <replaceregexp
            encoding="utf-8"
            file="AndroidManifest.xml"
            match="SETTING_UMENG_CHANNEL_CHANNELID_VALUES"
            replace="${channel}" /> <!-- <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" />

        <copy tofile="${gos.path}/test_${channel}.apk" >

            <fileset
                dir="${out.absolute.dir}/"
                includes="test-release.apk" />
        </copy>

        <delete includeEmptyDirs="true" >

            <fileset
                dir="${out.absolute.dir}"
                includes="**/*" />
        </delete>  
<!---->
######这里要将替换之后的渠道号值改成默认的值SETTING_UMENG_CHANNEL_CHANNELID_VALUES,不然下一个打包时将不会替换渠道号的值
        <replaceregexp
            encoding="utf-8"
            file="AndroidManifest.xml"
            match="${channel}"
            replace="SETTING_UMENG_CHANNEL_CHANNELID_VALUES" />
        <echo message="===========================" />
    </target>
	</project>

5、把以上4个文件拷贝到项目根目录下,目录结构如下

test--
	--src
	--res
	--libs
	--local.properties
	--custom_rules.xml
	--ant.properties
	--build.xml

6、打开CMD,然后CD到项目根路径下,运行ant deploy即可。注意在运行之前要注意先clean一下项目,不然可能会报错误,切记!!!!

BUILD FAILED
F:\Test\Test\build.xml:113: The following error occurred while executing this line:
F:\Test\Test\build.xml:139: The following error occurred while executing this line:

7、如果你看到BUILD SUCCESS,那么恭喜你,多渠道打包编译成功了,去输出目录查看一下APK文件吧!

8、多渠道打包文件下载地址

文件下载地址