android ant批量打包

当我们对安卓项目需要分很多渠道打包的时候,批量打包工具无疑是个不二选择,批量打包的方式大概可以分为两种,第一是通过第三方的打包平台,比如360加固宝,这确实是个很好的工具,既提高了源代码的安全性能,又能简单的达到分渠道批量打包的目的,唯一的缺点就是你的源代码会被360一览无余。当然我今天要说的就是一个传统的打包工具----ant的用法。

其实在我们安卓的开发平台里,已经有了ant的功能,在sdk和eclipse中都会自带有。如果你自认品味高端,也可以去直接下载一个独立的ant工具。下载地址:http://ant.apache.org/

把ant设为系统的全局变量。

如果要完成批量处理,我们需要添加一个为ant添加一个jar包,下载地址:http://download.csdn.net/detail/tenggangren/5958967;

下载完成后放到ant的安装目录下的lib/目录下。

接下来就是批量打包的流程了,打开cmd窗口,进入安卓项目所在目录下,输入android update project,回车,

在我们的项目目录下就会生成相应的build.xml,custom_rules.xml,ant.properties,local.properties四个文件,接下来我们一个一个文件的讲

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="BuildTest" 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 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" />
    <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>

这个文件是我们完成编辑工作后需要执行的总文件,也就是说我们把4个文件编辑好了之后,直接ant运行build.xml就可以了。

里头的代码不多,第一行<project name="MangoTravel" default="help">是声明项目名称,

接下来是引用一些文件,<property file="local.properties" />引用local.properties,

<property file="ant.properties" />引用ant.properties,这两个文件都是存放这一些声明好了的变量,稍后我们细看。

接着看这几行:

<property environment="env" />
    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME" />
    </condition>

这里主要是引用到安卓sdk的环境变量这个env变量在后面会用到。

<import file="custom_rules.xml" optional="true" />,import具体需要做事情的文件

<import file="${sdk.dir}/tools/ant/build.xml" />,import安卓sdk中的ant下的buld.xml,也可以说是系统的build文件。

这就是build.xml的全部。

接下来看custom_rules.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules" >
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" >
        <classpath>
            <pathelement location="E:/work/ant/apache-ant-1.9.4/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="(.*)" android:name="UMENG_CHANNEL"" />  
            <substitution expression="android:value="${channel}" android:name="UMENG_CHANNEL"" />  
           <fileset
                dir=""
                includes="AndroidManifest.xml" />
        </replaceregexp>
        <property
            name="out.final.file"
            location="${gos.path}/MangoTravel_${channel}.apk" />
        <antcall target="clean" />
        <antcall target="release" />
    </target>
</project>

实现循环打包的代码就在这里,原理就是循环去用正则表达式匹配到对应的文本然后进行替换的过程。

<taskdef resource="net/sf/antcontrib/antcontrib.properties" >
        <classpath>
            <pathelement location="E:/work/ant/apache-ant-1.9.4/lib/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>

这里是引用我们下载的第三方包。

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

这里是把我们的渠道值放入list中,market_channels是在ant.properties文件中声明的,用,分割成数组。

<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="${gos.path}/MangoTravel_${channel}.apk" />
        <antcall target="clean" />
        <antcall target="release" />
    </target>

这一段就是打包的核心代码了,通过正则表达式替换掉manifest.xml文件中的渠道值,这里我是替换友盟的值。

需要注意的是:<substitution expression="android:value=&quot;${channel}&quot; android:name=&quot;UMENG_CHANNEL&quot;" />  

这一行与你项目中的Manifest.xml中的要替换的<meta标签包含的代码要保持一致,比如这里是android:value=...;android:name=...,我们的Manifest.xml中也得这样,否则会匹配不成功而导致失败。

下面季航就是输出包名的代码了,然后是clean,release,这个可以忽略。

接下来看下local.properties中的变量:

sdk.dir=E:\\work\\Android_Develop_Tools\\SDK,就这一行,这是自动生成好的,不用管

ant.properties:


application.package:包名,manifest.xml中的包名.

ant.project.name:项目名称.

...

最后偷个懒,直接在eclipse中运行build.xml


选择ant build等待数分钟后即可大功告成。

其实批量打包工具都是一个原理,包括360加固包宝的批量加密(我猜),不管怎样,达到我们的目的就是一件美差。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值