ant 多渠道打包

1.做好单渠道打包的准备工作

2.多渠道打包工具准备

  1. 编写多渠道打包的代码
<?xml version="1.0" encoding="UTF-8"?>
<project name="AppActivity" 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 属性为定义变量 -->
    <property
        name="manifest.file"
        value="AndroidManifest.xml" >
    </property>

    <property
        name="bin.dir"
        value="bin" >
    </property>
    <property
        name="gen.dir"
        value="gen" >
    </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>
   
<!-- 
   <target name="-pre-build" depends="-ndk-build">
  </target>

  <target name="-ndk-build">
      <exec executable="ndk-build" failonerror="true">
          <arg value="clean" />
      </exec>
      <exec executable="ndk-build" failonerror="true" />
  </target>
-->
    <!-- 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" />
    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME" />
    </condition>
 
    <!--
   <target name="-pre-build">
        <exec executable="${ndk.dir}/ndk-build.cmd" failonerror="true"/>
    </target>

    <target name="clean" depends="android_rules.clean">
        <exec executable="${ndk.dir}/ndk-build" failonerror="true">
            <arg value="clean"/>
         </exec>
    </target>  
-->
    <!-- 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
    -->
    
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" >
        <classpath>
            <pathelement location="E:/apache-ant-1.9.4/lib/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>

 <!--
    <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" />

      <!--多渠道打包开始 -->
    <property name="MAIN_CHANNEL" value="" />
    <property name="UMENG_CHANNEL" value="" />

    <property name="key" value="com.test.game:111,com.test.game.yos:222" />

    <target name="deploy">
        <foreach target="modify_manifest" list="${key}" param="nameandchannel" delimiter=","></foreach>
    </target>
    <target name="modify_manifest">
        <!-- 获取渠道名字 -->
        <propertyregex override="true" property="UMENG_CHANNEL" input="${nameandchannel}" regexp="(.*):" select="1" />
        <!-- 获取渠道号码 -->
        <propertyregex override="true" property="MAIN_CHANNEL" input="${nameandchannel}" regexp=":(.*)" select="1" />
        <!--
        <copy tofile="modify_manifest.xml" >
            <fileset
                dir=""
                includes="AndroidManifest.xml" />
        </copy>
-->
        <!-- 正则匹配替换渠道号 -->
        <replaceregexp flags="g" byline="false" encoding="UTF-8">
            <regexp pattern='meta-data android:name="MAIN_CHANNEL" android:value="(.*)"' />
            <substitution expression='meta-data android:name="MAIN_CHANNEL" android:value="${MAIN_CHANNEL}"' />
            <fileset dir="" includes="AndroidManifest.xml" />
        </replaceregexp>
        <!-- 修改包名 -->
        <replaceregexp flags="g" byline="false" encoding="UTF-8">
            <regexp pattern='package="${application.package}"'/>
            <substitution expression='package="${UMENG_CHANNEL}"'/>
            <fileset dir="" includes="AndroidManifest.xml" />
        </replaceregexp>
        <replaceregexp flags="g" encoding="UTF-8" byline="true">  
            <regexp pattern="import ${application.package}.R"/>  
            <substitution expression="import ${UMENG_CHANNEL}.R"/>  
                <fileset dir="${project.dir}/src" includes="**/*.java"/>  
        </replaceregexp>  

        <copy todir="${project.dir}/res">
            <fileset dir="${project.dir}/${UMENG_CHANNEL}/res"/>
        </copy>

        <antcall target="release" />
         <copy tofile="${gos.path}/test_${UMENG_CHANNEL}.apk" >

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

<!--
        <copy tofile="modify_manifest.xml" >
            <fileset
                dir=""
                includes="AndroidManifest.xml" />
        </copy>
          -->
          <!-- 以下是恢复之前正则匹配修改的数据 -->
        <replaceregexp flags="g" byline="false" encoding="UTF-8">
            <regexp pattern='package="${UMENG_CHANNEL}"'/>
            <substitution expression='package="${application.package}"'/>
            <fileset dir="" includes="AndroidManifest.xml" />
        </replaceregexp>
       
        <replaceregexp flags="g" encoding="UTF-8" byline="true">  
            <regexp pattern="import ${UMENG_CHANNEL}.R"/>  
            <substitution expression="import ${application.package}.R"/>  
                <fileset dir="${project.dir}/src" includes="**/*.java"/>  
        </replaceregexp>  

        <delete includeEmptyDirs="true" >

            <fileset
                dir="${out.absolute.dir}"
                includes="**/*" />
        </delete>  
        <delete includeEmptyDirs="true" >
            <fileset
                dir="${bin.dir}"
                includes="**/*" />
        </delete>  
         <delete includeEmptyDirs="true" >
            <fileset
                dir="${gen.dir}"
                includes="**/*" />
        </delete>  
    </target>
<!-- 多渠道打包结束 -->
</project>

  1. 用ant deploy进行打包
原文地址:https://www.cnblogs.com/zjzyh/p/6594301.html