android ant批量打包

1.配置ant打包所需的环境变量(事先配置好Java,Android的环境变量)---这个不会请自行百度.or google
2.解压ant,比如解压到D:\ant
3.我的电脑->属性->高级->环境变量
4.系统变量新建ANT_ HMOE,变量值为d:\ant
5.系统变量新建或修改Path,将%ANT_HOME%\bin;添加到环境变量的path中去,(注意以上路径均为反斜杠 )
6.验证ant环境配置是否正确,在控制台输入cmd回车,如果出现
Buildfile:build.xml dose   not exist!
Build  failed
恭喜你已经配置成功!!
 
配置完成后:
1.在你的工程下面和依赖工程下面执行命令android update project -p E:\workspace\text(这是你的项目路径)
2.将custom_rules.xml拷贝到你的项目工程下

 

<?xml version="1.0" encoding="UTF-8"?>
 <project name="android_rules" default="deploy">


    <taskdef resource="net/sf/antcontrib/antcontrib.properties">  
        <classpath>  
            <pathelement location="${env.ANT_HOME}/lib/ant-contrib.jar" />  
        </classpath>  
    </taskdef> 
    
    <property name="channelname" 
            value="" /> 
    <property name="release.out.out.dir" value="20131219194545" /> 
    <property name="key" value="
        :xxx_adwx1,
        :xxx_adwx2,"/> 
    <target name="deploy">  
        <foreach target="modify_manifest" list="${key}" param="nameandchannel" delimiter=",">  
        </foreach>  
    </target>  
    
    <target name="modify_manifest">  
        <!-- 获取渠道名字 -->  
        <propertyregex override="true" property="channelname" input="${nameandchannel}" regexp=":(.*)" select="\1" />
        <replaceregexp flags="g" byline="false" encoding="UTF-8">  
            <regexp pattern='meta-data android:name="UMENG_CHANNEL" android:value="(.*)"' />  
            <substitution expression='meta-data android:name="UMENG_CHANNEL" android:value="${channelname}"' />  
            <fileset dir="" includes="AndroidManifest.xml" />  
        </replaceregexp>  
        <antcall target="release-name" />  
    </target>  
    <!-- version-tag: 1 -->
    
    <target name="release-name"
                depends="-set-release-mode-name, -release-obfuscation-check, -package, -post-package, -release-prompt-for-password, -release-nosign, -release-sign, -post-build"
                description="Builds the application in release mode.">
    </target>
    
    
    <target name="-set-release-mode-name" depends="-set-mode-check">
        <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-release-unsigned.apk" />
        <property name="out.final.file" location="${out.absolute.dir}/${channelname}.apk" />
        <property name="build.is.mode.set" value="true" />

        <!-- record the current build target -->
        <property name="build.target" value="release" />

        <property name="build.is.instrumented" value="false" />

        <!-- release mode is only valid if the manifest does not explicitly
             set debuggable to true. default is false. -->
        <xpath input="${manifest.abs.file}" expression="/manifest/application/@android:debuggable"
                output="build.is.packaging.debug" default="false"/>

        <!-- signing mode: release -->
        <property name="build.is.signing.debug" value="false" />

        <!-- Renderscript optimization level: aggressive -->
        <property name="renderscript.opt.level" value="${renderscript.release.opt.level}" />

        <if condition="${build.is.packaging.debug}">
            <then>
                <echo>*************************************************</echo>
                <echo>****  Android Manifest has debuggable=true   ****</echo>
                <echo>**** Doing DEBUG packaging with RELEASE keys ****</echo>
                <echo>*************************************************</echo>
            </then>
            <else>
                <!-- property only set in release mode.
                     Useful for if/unless attributes in target node
                     when using Ant before 1.8 -->
                <property name="build.is.mode.release" value="true"/>
            </else>
        </if>
    </target>
</project>

3.将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.dir=E:\\android\\sdk 

4.新建一个文件ant.properties---这里是你自己的keystore的密码
内容为:
key.store=android.keystore
key.store.password=123456
key.alias=android
key.alias.password=123456
5.将android.keystore拷贝到主项目根路径下.
5. cd到你的主工程下
6. 执行ant clean  
7.再执行 ant deploy
 

 最后需要说明一点,ant打包跟Eclipse的工程是不能同时存在的。就是说如果你的工程现在在Eclipse里面运行就会出现问题.好的方法就是用svn吧代码down下来放到一个特定的位置。然后对代码进行打包操作。还有一点就是我们这个打包脚本只是对友盟的渠道进行修改.里面有个容易出现的问题.就是友盟的AndroidManifest.xml里面的渠道需要写成一行。如下--是必须写成一行,原因应该是脚本的问题..我这个不熟..

 <meta-data android:name="UMENG_CHANNEL"android:value="qupai_adwx1" />

 

ok就这么愉快的完成了!!!-----有任何问题可以加我QQ:2519687025或者直接留言.我邮件会收到的.

 

posted @ 2014-05-07 20:36  Mulberry_yan  阅读(1331)  评论(0编辑  收藏  举报