android修改渠道批量打包



一,准备步骤:

1,ant无法循环,需提前下载ant-contrib-1.0b3.jar文件支持批量打包。放在工程LIBS目录下。

 

2.JDK版本控制为1.6,太高的版本有些API已淘汰不可用。

 

3.androidSDK许多工具可能路径不同,根据具体情况配置。

 

 

二,编写build.xml(该文件已在文件里有提供)

<?xml version="1.0" encoding="UTF-8"?>

<project

    name="${app.name}"

    basedir="."

    default="release" >

 

    

    

    <!-- 属性列表 -->

 

    <property

        description="引用的配置文件"

        file="build.properties" >

    </property>

 

    <property

        name="outdir"

        description="输出路径"

        value="${app.source.path}/bin" />

 

 

    <property

        name="resource-dir"

        value="${app.source.path}/res" />

 

    <property

        name="resource-path"

        value="${app.source.path}/resource" />

 

    <property

        name="asset-dir"

        value="${app.source.path}/assets" />

 

    <property

        name="srcdir"

        value="${app.source.path}/src" />

 

    <property

        name="external-libs"

        value="${app.source.path}/libs" />

 

    <property

        name="outdir-classes"

        value="${outdir}/classes" />

 

    <property

        name="zipalign-package-ospath"

        value="${app.source.path}/bin/${app.name}.apk" />

 

    <property

        name="dex-file"

        value="classes.dex" />

 

    <property

        name="intermediate-dex-ospath"

        value="${outdir}/${dex-file}" />

 

    <property

        name="resources-package-ospath"

        value="${outdir}/${ant.project.name}.ap_" />

 

    <property

        name="out-debug-package"

        value="${outdir}/${ant.project.name}.apk" />

 

    <property

        name="out-debug-package-ospath"

        value="${outdir}/${ant.project.name}.apk" />

 

    <property

        name="proguard-dir"

        value="${app.source.path}/proguard.jar" />

 

 

    <property

        name="out-unsigned-package-ospath"

        value="${outdir}/${ant.project.name}-unsigned.apk" />

 

    <property

        name="out-signed-package-ospath"

        value="${outdir}/${ant.project.name}-signed.apk" />

 

    <property

        name="aapt"

        value="${apk.sdk.home}/platform-tools/aapt.exe" />

 

    <property

        name="jarsigner"

        value="${jdk.home}/bin/jarsigner.exe" />

 

    <property

        name="aidl"

        value="${android.tools}/aidl.exe" />

 

    <property

        name="adb"

        value="${apk.tools}adb.exe" />

 

    <property

        name="dx"

        value="${apk.platform-tools}/dx.bat" />

 

    <property

        name="apk-builder"

        value="${apk.tools}apkbuilder.bat" />

 

    <property

        name="apktool"

        value="${apk.tools}apktool.bat" />

 

    <property

        name="android-framework"

        value="${sdk.folder}framework.aidl" />

 

    <property

        name="android-jar"

        value="${sdk.folder}android.jar" />

 

    <property

        name="outdir-gen"

        description="生成R文件的相对目录"

        value="gen" />

 

    <property

        name="outdir-bin"

        description="编译后的文件相对目录"

        value="bin" />

 

    <property

        name="manifest-xml"

        value="AndroidManifest.xml" />

 

    <property

        name="android-maps-jar"

        value="${apk.sdk.home}/add-ons/addon-google_apis-google-17/libs/maps.jar" />

 

    <property

        name="external-lib-ospath"

        description="外部类库所在目录"

        value="${app.source.path}/${external-libs}" />

 

    <property

        name="keystore-file"

        description="证书文件"

        value="${app.source.path}/${keystore.name}.keystore" />

 

 

  <property name="key" value="1个包渠道:111,第2个包渠道:222/>   

   

    <property name="zipalign" value="${apk.tools}/zipalign.exe" /> 

 

  <property environment="env" />  

    <!-- 使用第三方的ant包,使ant支持for循环-->  

    <taskdef resource="net/sf/antcontrib/antcontrib.properties">  

        <classpath>  

            <pathelement location="${external-libs}/ant-contrib-1.0b3.jar" />  

        </classpath>  

    </taskdef> 

   <!--  

  <target name="init" >

 

        <echo>

1初始化删除对应文件夹创建文件夹

 

        </echo>

 

        <delete>

 

            <fileset dir="${app.source.path}/bin" >

 

                <include name="*" />

            </fileset>

        </delete>

 

        <mkdir dir="${outdir}" />

 

        <mkdir dir="${outdir}" />

 

        <mkdir dir="${outdir-classes}" />

 

        <mkdir dir="${asset-dir}" />

    </target>

   -->

      <target name="deploy">

           <echo>  1按渠道批量打包  </echo>

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

            <!-- 获取渠道号码 -->  

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

            <!-- 正则匹配替换渠道号 -->  

            <replaceregexp flags="g" byline="false" encoding="UTF-8">  

                <regexp pattern='meta-data android:value="(.*)" android:name="UMENG_CHANNEL"' />  

                <substitution expression='meta-data android:value="${channelkey}" android:name="UMENG_CHANNEL"' />  

                <fileset dir="" includes="AndroidManifest.xml" />  

            </replaceregexp>  

            <antcall target="zipalign" />  

        </target> 

   

    <target

        name="gen-R"

        ><!-- depends="init"  -->

 

        <echo>

2 生成  R.java

 

        </echo>

 

        <exec

            executable="${aapt}"

            failonerror="true" >

 

            <arg value="package" />

 

            <arg value="-f" />

 

            <arg value="-m" />

 

            <arg value="-J" />

 

            <arg value="${outdir-gen}" />

 

            <arg value="-S" />

 

            <arg value="${resource-dir}" />

 

            <arg value="-M" />

 

            <arg value="${manifest-xml}" />

 

            <arg value="-I" />

 

            <arg value="${android-jar}" />

        </exec>

    </target>

 

 

 

    <target

        name="aidl"

        depends="gen-R" >

 

        <echo>

3编译 .aidl 文件

 

        </echo>

 

        <apply

            executable="${aidl}"

            failonerror="true" >

 

            <!-- 指定预处理文件 -->

 

            <arg value="-p${android-framework}" />

            <!-- aidl声明的目录 -->

 

            <arg value="-I${srcdir}" />

            <!-- 目标文件目录 -->

 

            <arg value="-o${outdir-gen}" />

            <!-- 指定哪些文件需要编译 -->

 

            <fileset dir="${srcdir}" >

 

                <include name="**/*.aidl" />

            </fileset>

        </apply>

    </target>

 

    <target

        name="compile"

        depends="aidl" >

 

        <echo>

4 编译 java 源文件编译成class文件

 

        </echo>

 

        <javac

            bootclasspath="${android-jar}"

            destdir="${outdir-classes}"

            encoding="utf-8"

            srcdir="."

            target="1.6"

            includeantruntime="on"

             >

 

            <classpath>

 

                <fileset

                    dir="${external-libs}"

                    includes="*.jar" />

 

                <filelist>

 

                    <file name="${android-maps-jar}" />

                </filelist>

            </classpath>

        </javac>

    </target>

 

    <target

        name="dex"

        depends="compile" >

 

        <echo>

5 将.class文件转化成.dex文件

 

        </echo>

 

        <apply

            executable="${dx}"

            failonerror="true"

            parallel="true" >

 

            <arg value="--dex" />

 

            <arg value="--output=${intermediate-dex-ospath}" />

 

            <arg path="${outdir-classes}" />

 

            <fileset

                dir="${external-libs}"

                includes="*.jar" />

        </apply>

    </target>

 

    <target name="package-res-and-assets" >

 

        <echo>

6 将资源文件放进输出目录

 

        </echo>

 

        <exec

            executable="${aapt}"

            failonerror="true" >

 

            <arg value="package" />

 

            <arg value="-f" />

 

            <arg value="-M" />

 

            <arg value="${manifest-xml}" />

 

            <arg value="-S" />

 

            <arg value="${resource-dir}" />

 

            <arg value="-A" />

 

            <arg value="${asset-dir}" />

 

            <arg value="-I" />

 

            <arg value="${android-jar}" />

 

            <arg value="-F" />

 

            <arg value="${resources-package-ospath}" />

        </exec>

    </target>

 

    <target

        name="package"

        depends="dex, package-res-and-assets" >

 

        <echo>

7 打包成未签证的apk

 

        </echo>

 

        <exec

            executable="${apk-builder}"

            failonerror="true" >

 

            <arg value="${out-unsigned-package-ospath}" />

 

            <arg value="-u" />

 

            <arg value="-z" />

 

            <arg value="${resources-package-ospath}" />

 

            <arg value="-f" />

 

            <arg value="${intermediate-dex-ospath}" />

 

            <arg value="-rf" />

 

            <arg value="${srcdir}" />

        </exec>

    </target>

 

    <target

        name="jarsigner"

        depends="package" >

 

        <echo>

8 对apk进行签证

 

        </echo>

 

        <exec

            executable="${jarsigner}"

            failonerror="true" >

 

            <arg value="-keystore" />

 

            <arg value="${keystore-file}" />

 

            <arg value="-storepass" />

 

            <arg value="${keystore.pass}" />

 

            <arg value="-keypass" />

 

            <arg value="${keystore.pass}" />

 

            <arg value="-signedjar" />

 

            <arg value="${out-signed-package-ospath}" />

 

            <arg value="${out-unsigned-package-ospath}" />

            <!-- 不要忘了证书的别名 -->

 

            <arg value="${keystore.alias.name}" />

        </exec>

    </target>

 

     <target

        name="release"

        depends="jarsigner" >

 

        <delete file="${out-unsigned-package-ospath}" />

 

        <echo>

 删除未签证apk, 签名APK路径:${out-signed-package-ospath}

 

        </echo>

    </target>

 

      

           

      <target name="zipalign" depends="release">  

           <echo>    渠道 :${channelname} 的apk正在生成</echo>

        <exec executable="${zipalign}" failonerror="true">  

            <arg value="-v" />  

            <arg value="4" />  

            <arg value="${out-signed-package-ospath}" />  

            <arg value="${outdir}/${app.name}_${channelname}.apk" />  

        </exec>  

    </target> 

</project>

 

第三步:编写build.properties(该文件已在文件里有提供,该文件用来配置,请根据自己本地工程及环境配置)

android_version=4.2.2(工程android版本)

apk.sdk.home=F\:/sdk/(SDK的目录)

 

apk.tools=${apk.sdk.home}tools/(SDK下的TOOLS目录)

apk.platform-tools=${apk.sdk.home}platform-tools/(SDK下的platform_tools目录)

 

jdk.home=C:/Program Files/Java/jdk1.6.0_10

(JDK路径,注意是1.6)

app.source.path=D\:/android_workspacetwo/zz_ddz

(你的android工程目录,zz_ddz是你的工程

sdk.folder=${apk.sdk.home}platforms/android-17/

SDKandroid-17目录)

 

app.name=appname

你的应用名

 

keystore.name=zz_ddz

keystore的名字

keystore.alias.name=zhangling

keystore的别名,注意是别名不是文件名

 

keystore.pass=123456

keystore密码

四:将以上两个文件放入工程根目录。并修改相应配置。

在AndroidManifest.xml文件内加入

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

UMENG_CHANNEL是渠道名,对应的VALUE=***”将是我们批量打包要修改的值,注意valuename的先后不要颠倒。对应build.xml文件里的<property name="key" value="1个包渠道:111,第2个包渠道:222/>   。若有多个渠道,以此类推:1个包渠道:渠道号1,第2个包渠道:渠道号2,第3个包渠道:渠道号3.。。。。。。渠道包之间要用“,”隔开。

 

五:运行脚本

1很多时候咱们直接运行build.xml是第一个默认运行的target不是咱们需要的,你可以仔细看看这个 build.xml的依赖关系,你就能知道了,咱们这个build.xml需要第一个运行的是 deploy的这个target,所以 需要这么做: build.xml右键,run as , 这时候不是选择运行 而是 最后那个选项: external tools configurations ,然后进去一个界面

 

以上运行完控件台会有报提示,若有错误,请根据提示改正对应的工具路径就行。最后附上对应工程目录以供参考

 

 

以上做法主要参考以下几个贴子:

http://blog.csdn.net/tenggangren/article/details/8051937

http://blog.csdn.net/liuhe688/article/details/6679879

需要的JAR文件及文档下载地址:

http://pan.baidu.com/disk/home?sr=fc#dir/path=%2Fme%2Fant%E6%89%B9%E9%87%8F%E6%89%93%E5%8C%85

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值