android ant 多渠道打包

正在做的事情,步骤如下:

1、下载一些必须的工具:ant,ant-contrib扩展包(用于ant循环打包)

ant下载地址:http://download.csdn.net/detail/u013062469/8838789 

ant-contrib扩展包下载地址:http://download.csdn.net/detail/u013062469/8838795

2、配置ant的环境变量:

新建系统变量:ANT_HOME 变量值:C:\apache-ant-1.9.4(你的ant所在)

Path 后添加:%ANT_HOME%\bin

之后检查是否配置成功:打开cmd,输入ant,显示如下:


表示配置成功。

3、通过cmd进入将要打包的项目路径,我这边设个新的demo,为AndroidAnt

进入之后运行下面命令:

android update project -p ./

显示的结果如下:


表示成功,进入Eclipse,刷新项目,会出现两个新的文件:

一个:build.xml,一个:local.properties,我们还要在项目中重新新建一个ant.properties文件

在AndroidManifest.xml文件中添加渠道号数据:

 <meta-data
            android:name="channelname"
            android:value="channelkey" />
之后新建custom_rules.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="libs/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>


    <target name="deploy" >


        <antcall target="clean" />


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


    <target name="modify_manifest" >


        <propertyregex
            input="${channel}"
            override="true"
            property="channelname"
            regexp="(.*):"
            select="\1" />


        <propertyregex
            input="${channel}"
            override="true"
            property="channelkey"
            regexp=":(.*)"
            select="\1" />


        <replaceregexp
            byline="true"
            encoding="utf-8"
            file="AndroidManifest.xml"
            match="android:name=&quot;channelname&quot;"
            replace="android:name=&quot;${channelname}&quot;" />


        <replaceregexp
            byline="true"
            encoding="utf-8"
            file="AndroidManifest.xml"
            match="android:value=&quot;channelkey&quot;"
            replace="android:value=&quot;${channelkey}&quot;" />


        <antcall target="release" />


        <copy
            file="${out.absolute.dir}/${ant.project.name}-release.apk"
            tofile="${out.absolute.dir}/out/${ant.project.name}-v${version}-${channelkey}.apk" />


        <replaceregexp
            byline="true"
            encoding="utf-8"
            file="AndroidManifest.xml"
            match="android:name=&quot;${channelname}&quot;"
            replace="android:name=&quot;channelname&quot;" />


        <replaceregexp
            byline="true"
            encoding="utf-8"
            file="AndroidManifest.xml"
            match="android:value=&quot;${channelkey}&quot;"
            replace="android:value=&quot;channelkey&quot;" />
    </target>


</project>
之后我们将我们自己的.keystore文件添加到我们的项目中,然后在ant.properties文件中设置一些数据:

sdk.dir=SDK所在路径
key.store=AAA.keystore
key.store.password=123456
key.alias=AAA.keystore
key.alias.password=123456
market_channels=UMENG_CHANNEL:10001
version=1.0
我们做上面内容之后右键build.xml文件,运行 第二个既是:Ant 构建 


然后选择:


点击运行,稍等下、运行结果:


其中打开我们所在的apk文件


可以使用反编译工具反编译下,你会看到这个包里面的AndroidMnifest.xml文件中的

 <meta-data
            android:name="channelname"
            android:value="channelkey" />

已经变成了

 <meta-data
            android:name="channelname"
            android:value="10001" />
至此,结束:

下面是一个apk反编译工具:

下载地址:http://download.csdn.net/detail/u013062469/8838961

有朋友说,出现了问题我也不截图了,都是以前出现的,请你把你的配置文件做好,还有有的时候会出现javac类型的错误,这个时候肯能是你安装Java的过程中出现错误,到底是不是呢,现在你打开dos命令,输入javac如果出现用法: javac <options> <source files>
其中, 可能的选项包括:
  -g                         生成所有调试信息
  -g:non.....等,说明你的java安装是不错的,如果出现javac不是系统命令什么的,说明你安装的不对,这个问题不再者说,下节我会说仔细跟大家说的。


你可能感兴趣的:(android ant 多渠道打包)