android 下用ant编译脚本执行批量渠道打包


 在工程目录下新建一个文件夹 build,以下是文件夹中的文件 build.xml中的所有内容。


 

<?xml version="1.0" encoding="UTF-8"?>  
<project name="myprject" default="ids" basedir="../">
    <property environment="env"/>     
<!--     <property file="./build/build.properties" />   -->
    <tstamp>
<!--         打包用的时间戳名称 -->
    <format property="TODAY_CN"
          pattern="yyyyMMddHHmm"
          locale="zh,CN"/>
    </tstamp>
<!--     判断当前的系统环境 -->
	<condition property="tools_suffix" value=".exe" else="">  
        <os family="windows" />  
    </condition> 
    
    <condition property="build_name" value="local" else="jenkins">  
        <os family="windows" />  
    </condition> 
    <property file="./build/build_${build_name}.properties" /> 
    <property file="./build/build.properties" />
    <property name="out-app-name" value="${app.name}_android_${app.version}_${TODAY_CN}_${id}.apk"/>
    <property name="out-signed-package" value="${outdir}/${app.name}_android_${app.version}_${TODAY_CN}_${id}.apk" /> 
	<property name="out-signed-package-ospath" value="${basedir}/${out-signed-package}" /> 
    
    <!-- Tools -->  
    <property name="aapt" value="${android.sdk.tools}/aapt${tools_suffix}" />  
    <property name="zipalign" value="${android.tools}/zipalign${tools_suffix}" />  
    <property name="jarsigner" value="${jdk.home}/bin/jarsigner${tools_suffix}" />  
    <property name="aidl" value="${android.sdk.tools}/aidl${tools_suffix}" />  
    <property name="adb" value="${android.tools}/adb${tools_suffix}" />  
    <condition property="dx" value="${android.sdk.tools}/dx.bat" else="${android.sdk.tools}/dx">  
        <os family="windows" />  
    </condition>  
    <condition property="apk-builder" value="${android.tools}/apkbuilder.bat" else="${android.tools}/apkbuilder">  
        <os family="windows" />  
    </condition>  
  
<!--     <property name="android-jar" value="${android.sdk.folder}/android.jar" />   -->
<!--     <property name="maps-jar" value="${external-libs}/maps.jar" />   -->
  
    <!-- 第三方.jar -->
     <path id="libs_classpath">        
<!--          <pathelement location="${basedir}\jar\httpmime-4.1.1.jar"/>         -->
<!--          <pathelement location="${basedir}\jar\signpost-commonshttp4-1.2.1.1.jar"/> -->
<!--          <pathelement location="${basedir}\jar\signpost-core-1.2.1.1.jar"/> -->
<!--          <pathelement location="${basedir}\jar\Analytics-Android-SDK-2.1.jar2"/>         -->
         <fileset dir="${basedir}/libs/">            
             <include name="*.jar"/>        
             </fileset>
         </path>
          <path id="libs_pro">
              <fileset dir="${third_lib}/libs/">            
             <include name="*.jar"/>        
             </fileset>
         </path>
    <!-- Rules -->  

    <target name="init" >  
        <echo message="Init output directory.....">  
        </echo>
        <delete dir="${outdir}" />
        <delete file="${asset-dir}/config.properties" />
        <mkdir dir="${app.output.dir}" />  
    </target>  

    <!-- Create the output directories if they don't exist yet. -->  
    <target name="dirs" depends="init">  
        <echo>Creating output directories if needed...</echo>  
        <mkdir dir="${outdir}" />  
        <mkdir dir="${outdir-classes}" />  
    </target>  
  
    <!-- Generate the R.java file for this project's resources. -->  
    <target name="resource-src" depends="dirs">  
        <echo>Generating R.java / Manifest.java from the resources...</echo>  
        <echo>resources...resource-dir: ${resource-dir}</echo>  
        <echo>resources...android-jar: ${android-jar}</echo>
        <echo>resources...outdir-r: ${outdir-r}</echo>
        <echo>resources...aapt:${aapt}</echo>
        <exec executable="${aapt}" failonerror="true">  
            <arg value="package" />  
            <arg value="-m" /> 
            <arg value="-J" />  
            <arg value="${outdir-r}" />  
            <arg value="-M" />  
            <arg value="./AndroidManifest.xml" />  
            <arg value="-S" />  
            <arg value="${resource-dir}" />  
            <arg value="-I" />  
            <arg value="${android-jar}" /> 
        </exec>  
    </target>  
  
    <!-- Generate java classes from .aidl files. -->  
    <target name="aidl" depends="dirs">  
        <echo>Compiling aidl files into Java classes...</echo>  
        <apply executable="${aidl}" failonerror="true">  
            <arg value="-p${android-framework}" />  
            <arg value="-I${srcdir}" />  
            <fileset dir="${srcdir}">  
                <include name="**/*.aidl" />  
            </fileset>  
        </apply>  
    </target>  
  
    <!-- Compile this project's .java files into .class files. -->  
    <target name="compile" depends="dirs, resource-src, aidl">  
         	<echo>libs_classpath..${basedir}/libs/</echo> 
        <javac encoding="UTF-8" target="1.6"  extdirs="" srcdir="." 
            destdir="${outdir-classes}" bootclasspath="${android-jar}"
             includeantruntime="on" source="1.6">
         	<compilerarg value="-Xlint" />
<!--          	第三方的工程 -->
<!--          	<src path="${third_lib}src" /> -->
<!--     		<src path="src" /> -->
<!--     		<src path="gen" /> -->
<!--     		第三方工程里的导入包 -->
<!--     		<classpath refid="libs_pro"/>  -->
         	<classpath refid="libs_classpath"/>  
        </javac> 
        <echo>libs_classpath..${libs_classpath}</echo> 
    </target>  
  
    <!-- Convert this project's .class files into .dex files. -->  
    <target name="dex" depends="compile">  
        <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>
          <echo>--output=${intermediate-dex-ospath}</echo>
          <echo>--outdir-classes-ospath=${outdir-classes-ospath}</echo>
          <echo>--outdir-external-libs=${external-libs}</echo>
          
        <exec executable="${dx}"  failonerror="true">  
            <arg value="--dex" />
            <!-- 输出文件 -->
			<arg value="--output=${intermediate-dex-ospath}" />
			<!-- 要生成.dex文件的源classes和libraries -->
            <arg value="${outdir-classes-ospath}" />
             <arg value="${external-libs}"/>  
        </exec>
    </target>  
  
    <!-- Put the project's resources into the output package file. -->  
    <target name="package-res-and-assets">  
        <echo>Packaging resources and assets...</echo>  
        <exec executable="${aapt}" failonerror="true">  
            <arg value="package" />  
            <arg value="-f" />  
            <arg value="-M" />  
            <arg value="AndroidManifest.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}" />  
        </exec>  
    </target>  
  
    <!-- Same as package-res-and-assets, but without "-A ${asset-dir}" -->  
    <target name="package-res-no-assets">  
        <echo>Packaging resources...</echo>  
        <exec executable="${aapt}" failonerror="true">  
            <arg value="package" />  
            <arg value="-f" />  
            <arg value="-M" />  
            <arg value="AndroidManifest.xml" />  
            <arg value="-S" />  
            <arg value="${resource-dir}" />  
            <!-- No assets directory -->  
            <arg value="-I" />  
            <arg value="${android-jar}" />  
            <arg value="-F" />  
            <arg value="${resources-package}" />  
        </exec>  
    </target>  
  
    <!-- Invoke the proper target depending on whether or not   
             an assets directory is present. -->  
    <!-- TODO: find a nicer way to include the "-A ${asset-dir}" argument   
             only when the assets dir exists. -->  
    <target name="package-res">  
        <available file="${asset-dir}" type="dir" property="res-target" value="and-assets" />  
        <property name="res-target" value="no-assets" />  
        <antcall target="package-res-${res-target}" />  
    </target>  
  
    <!-- Package the application and sign it with a debug key.   
             This is the default target when building. It is used for debug. -->  
    <target name="release" depends="dex, package-res">  
        <echo>Packaging ${out-unsigned-package}, and signing it with a release ...</echo>  
        <echo>... ${external-libs-ospath}, .so 文件的路径</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-ospath}" />  
	    	<arg value="-rj" />  
            <arg value="${external-libs-ospath}" />
            <arg value="-nf"/>
			<arg value="${external-libs-ospath}"/>
        </exec>  
    </target>  
    
    <target name="idsrelease">  
        <echo>Packaging ${out-unsigned-package}, and signing it with a release ...</echo>  
        <echo>out-unsigned-package-ospath: ${out-unsigned-package-ospath}</echo>
        <echo>resources-package-ospath: ${resources-package-ospath}</echo>
        <echo>intermediate-dex-ospath: ${intermediate-dex-ospath}</echo>
        <echo>srcdir-ospath: ${srcdir-ospath}</echo>
        <echo>external-libs-ospath: ${external-libs-ospath}</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-ospath}" />  
	    	<arg value="-rj" />  
            <arg value="${external-libs-ospath}" />  
        </exec>  
    </target> 
  
    <!--进行签名--> 
        <target name="jarsigner" depends="release">  
        <echo>cmcc_jiaotong.key== ${basedir}/${cmcc_jiaotong.key}</echo>
        <exec executable="${jarsigner}" failonerror="true"> 
<!--              <arg value="-verbose" />  -->
			<arg value="-digestalg"/>
			<arg value="SHA1"/>
			<arg value="-sigalg"/>
			<arg value="MD5withRSA"/>
<!-- 			jdk 1.7 需要 以上四行算法的代码 -->
            <arg value="-keystore" />  
            <arg value="${basedir}/${cmcc_jiaotong.key}" />  
            <arg value="-storepass" />  
            <arg value="${keystore.password}" />  <!-- 验证密钥完整性的口令,创建时建立的 -->
            <arg value="-keypass" />
    		<arg value="${keystore.password}" /> <!-- 专用密钥的口令,就是key密码 -->
            <arg value="-signedjar" />  
            <arg value="${out-signed-package-ospath}" />  
            <arg value="${out-unsigned-package-ospath}" />
            <arg value="${keystore.alias}" />   
            <arg value="-sigfile"/>
            <arg value="CERT" />
        </exec>  
    </target> 

    <target name="idsjarsigner" depends="idsrelease">  
         <echo>key== ${keystore}</echo>
          <echo>key.password == ${keystore.password}</echo>
           <echo>output path == ${out-signed-package-ospath}</echo>
            <echo>keystore.alias == ${keystore.alias}</echo>
        <exec executable="${jarsigner}" failonerror="true">  
<!--             <arg value="-verbose" />   -->
			<arg value="-digestalg"/>
			<arg value="SHA1"/>
			<arg value="-sigalg"/>
			<arg value="MD5withRSA"/>
<!-- 			jdk 1.7 需要 以上四行算法的代码 -->
            <arg value="-storepass" />  
            <arg value="${keystore.password}" />  
            <arg value="-keystore" />  
            <arg value="${keystore}" />  
            <arg value="-signedjar" />  
            <arg value="${out-signed-package-ospath}" />  
            <arg value="${out-unsigned-package-ospath}" />
            <arg value="${keystore.alias}"/>
            <arg value="-sigfile"/>
            <arg value="CERT" />  
        </exec>  
        <exec executable="${zipalign}" failonerror="true">  
            <arg value="-v" />  
            <arg value="-f" />  
            <arg value="4" />  
            <arg value="${out-signed-package-ospath}" />  
            <arg value="${out-signed-zipalign-package}" />  
        </exec>  
    </target>    
    
    <!--进行优化-->  
    <target name="zipalign" depends="jarsigner">  
        <exec executable="${zipalign}" failonerror="true">  
            <arg value="-v" />  
            <arg value="-f" />  
            <arg value="4" />  
            <arg value="${out-signed-package-ospath}" />  
            <arg value="${out-signed-zipalign-package}" />  
        </exec>  
    </target>  
    
    <!--直接上传到手机中去-->  
    <target name="adb" depends="zipalign">  
            <exec executable="${adb}" failonerror="true">  
                <arg value="install" />  
                <arg value="-r" />  
                <arg value="${out-signed-zipalign-package}" />  
            </exec>  
    </target>  
    
   <target name="ids" depends="dex">
       <echo>ant 的位置:${ant.home}/lib/ant-contrib-1.0b3.jar</echo>
		<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${ant.home}/lib/ant-contrib-1.0b3.jar" />
			<loadfile property="file-content" srcFile="build/ids.cfg" />
			<echo>打印渠道文件存放地址:${file-content}</echo>
			<foreach  target="modify_manifest" param="id" list="${file-content}" delimiter="
"/>
	</target>
	
<!-- 	修改 配置文件 manifest 中的channel id -->
   <target name="modify_manifest">  
      <replaceregexp flags="g" byline="false" encoding="utf-8">  
      <regexp pattern="<meta-data android:name="SZICITY_CHANNEL" android:value="(.*)"/>" /> 
	  <substitution expression="<meta-data android:name="SZICITY_CHANNEL" android:value="${id}"/>" /> 
      <fileset dir="" includes="AndroidManifest.xml" />  
      </replaceregexp>  
      <antcall target="package-res" />
		<antcall target="idsjarsigner" /> 
		<copy file="${out-signed-zipalign-package}" tofile="${app.output.dir}/${id}/${out-app-name}"/>
		<delete file="${out-signed-zipalign-package}" />
   </target> 
   

</project>  


配置文件中的一些配置信息:build.properties 文件

#===============App set========================
app.name=jiaotongbobao
app.version=2.0.1
#keystore=build/cmcc_jiaotong.key
#keystore.password=cmcc_jiaotong
#keystore.alias=\u4EA4\u901A\u64AD\u62A5
keystore=build/itotem.keystore
keystore.password=itotem
keystore.alias=itotemApp
third_lib=../ItotemBase/
app.output.dir=outputDir
#=============Android set======================
android.api.version=16
android.home=${env.ANDROID_HOME}
#android.home=/Users/Shared/Jenkins/android-dev/android-sdk-macosx
android.tools=${android.home}/tools
android.sdk.folder=${android.home}/platforms/android-${android.api.version}
android.sdk.tools=${android.home}/platform-tools
#===============Java set======================
jdk.home=${env.JAVA_HOME}
#jdk.home=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/
jdk.bin.dir=${jdk.home}/bin
ant.home=${env.ANT_HOME}
#=================google map set=================
google.map.path=${android.home}/add-ons/addon_google_apis_google_inc_${android.api.version}/libs/
#=================================================
<!-- The intermediates directory -->  
    <!-- Eclipse uses "bin" for its own output, so we do the same. --> 
basedir=../
id=${app.name}
outdir=bin
     
android-framework="${android.sdk.folder}/framework.aidl
resource-dir=./res
asset-dir=./assets
srcdir=./src
srcdir-ospath=${basedir}/${srcdir}
external-libs=${basedir}/libs
external-libs-ospath=${basedir}/libs

    <!-- Output directories -->
outdir-classes=${outdir}/classes
outdir-classes-ospath=${basedir}/${outdir-classes}
<!-- Create R.java in the source directory -->
outdir-r=./gen

  <!-- Intermediate files -->
dex-file=classes.dex
intermediate-dex=${outdir}\${dex-file}
intermediate-dex-ospath=${basedir}\${intermediate-dex}
	
    <!-- The final package file to generate -->  
resources-package=${outdir}/${app.name}.ap_
resources-package-ospath=${basedir}/${resources-package}
out-debug-package=${outdir}/${app.name}-debug.apk
out-debug-package-ospath=${basedir}/${out-debug-package}

#out-app-name=${app.name}_android_${app.version}_${TODAY_CN}_${id}.apk"/>
out-unsigned-package=${outdir}/${app.name}-Unsigned.apk
#out-signed-package=${outdir}/${app.name}_android_${app.version}_${TODAY_CN}_${id}.apk
out-unsigned-package-ospath=${basedir}/${out-unsigned-package}
#out-signed-package-ospath=${basedir}/${out-signed-package}

android-jar=${android.sdk.folder}/android.jar
maps-jar=${external-libs}/maps.jar
    


渠道号的配置文件:ids.cfg

CHANNEL012
CHANNEL013

还需要注意在manifest.xml中的渠道配置信息为:

		     <meta-data android:name="SZICITY_APPKEY" android:value="3clhitctfu6k"/>
		     <meta-data android:name="SZICITY_CHANNEL" android:value="CHANNEL012"/>

在代码中检测是否正确打出的渠道包信息:

	/**
	 * 获得渠道号
	 * @param ctx
	 * @return
	 */
	public static String getChanel(Context ctx) {
		String CHANNELID = "000000";
		try {
			ApplicationInfo ai = ctx.getPackageManager().getApplicationInfo(
					ctx.getPackageName(), PackageManager.GET_META_DATA);
			CHANNELID = ai.metaData.getString("SZICITY_CHANNEL");
		} catch (Exception e) {
			//
		}
		Log.e(TAG,"CHANNELID:"+CHANNELID);
//		Toast.makeText(ctx, "CHANNELID:"+CHANNELID, Toast.LENGTH_SHORT).show();
		return CHANNELID;
	}  



然后还需要一个生成安装包的签名文件,最好签名的信息都为英文,和路径信息也都为英文,

因为经测试目前在mac系统环境下中文会编译不通过失败掉。








你可能感兴趣的:(android,ant,脚本,include,tools,output)