1.build.xml
<?xml version="1.0" encoding="UTF-8"?> <project name="apkTargets" default="zipalign" basedir="."> <record name="C:/build.log" loglevel="info" append="no" action="start" /> <property name="app.source.path" value="C:ProjectForAntBuild" /> <property name="jdk.home" value="C:Program FilesJavajdk1.6.0_10" /> <property name="sdk.home" value="C:android_sdk" /> <property name="ant.path" value="C:apache-ant-1.9.2lib" /> <property name="appname" value="ProjectForAntBuild" /> <property name="target" value="android-20" /> <property name="android_version" value="4.4" /> <property name="bin.dir" value="${jdk.home}in" /> <property name="sdk.folder" value="${sdk.home}platforms${target}" /> <property name="android.tools" value="${sdk.home}platform-tools" /> <property name="apk.tools" value="${sdk.home}/tools" /> <property name="key.store" value="${app.source.path}keystore" /> <property name="key.alias" value="keystore" /> <property name="key.store.password" value="123456" /> <property name="key.alias.password" value="123456" /> <property name="output.dir" value="out" /> <property name="temp.dir" value="out" /> <!-- 导入ant指令(If)需要用到第三方jar包ant-contrib.jar --> <path id="ant.contrib.lib"> <fileset dir="${ant.path}"> <include name="ant-contrib.jar" /> </fileset> </path> <taskdef resource="net/sf/antcontrib/antcontrib.properties"> <classpath refid="ant.contrib.lib" /> </taskdef> <!-- Tools --> <property name="aapt" value="${android.tools}/aapt.exe" /> <property name="zipalign" value="${apk.tools}/zipalign.exe" /> <property name="jarsigner" value="${jdk.home}/bin/jarsigner.exe" /> <property name="aidl" value="${android.tools}/aidl.exe" /> <property name="dx" value="${android.tools}/dx.bat" /> <property name="apk-builder" value="${apk.tools}/apkbuilder.bat" /> <property name="android-jar" value="${sdk.folder}android.jar" /> <property name="annotations-jar" value="${sdk.home} oolssupportannotations.jar" /> <property name="android-framework" value="${sdk.folder}/framework.aidl" /> <property name="proguard-home" value="${sdk.home} oolsproguardlib" /> <!-- some file directory exclude "bin" file, because "bin" file will be changed each loop --> <property name="outdir-gen" value="gen" /> <property name="resource-dir" value="res" /> <property name="asset-dir" value="assets" /> <property name="manifest-xml" value="AndroidManifest.xml" /> <property name="srcdir" value="src" /> <property name="srcdir-ospath" value="${basedir}${srcdir}" /> <property name="external-libs" value="libs" /> <property name="external-libs-ospath" value="${basedir}${external-libs}" /> <property name="outdir" value="bin" /> <property name="outdir-classes" value="${outdir}/classes" /> <property name="outdir-classes-ospath" value="${basedir}${outdir-classes}" /> <property name="dex-file" value="classes.dex" /> <property name="intermediate-dex" value="${outdir}/${appname}/${dex-file}" /> <property name="intermediate-dex-ospath" value="${basedir}${intermediate-dex}" /> <!-- The final package file to generate --> <property name="resources-package" value="${outdir}/${appname}.ap_" /> <property name="resources-package-ospath" value="${basedir}${resources-package}" /> <!-- The final apk file to generate when use the debug key --> <property name="out-debug-package" value="${outdir}/${appname}-debug.apk" /> <property name="out-debug-package-ospath" value="${basedir}${out-debug-package}" /> <property name="out-unsigned-package" value="${outdir}/${appname}/${appname}-unsigned.apk" /> <property name="out-unsigned-package-ospath" value="${basedir}${out-unsigned-package}" /> <property name="out-signed-package" value="${outdir}/${appname}/${appname}-signed.apk" /> <property name="out-signed-package-ospath" value="${basedir}${out-signed-package}" /> <property name="zipalign-package-ospath" value="${basedir}${output.dir}${appname}_for_android_${android_version}_${temp.dir}.apk" /> <property name="libs-path" value="libs" /> <!--1 初始化--> <target name="init"> <delete dir="${outdir-gen}" /> <delete dir="${outdir}" /> <delete file="${basedir}/proguardMapping.txt" /> <mkdir dir="${outdir-gen}" /> <mkdir dir="${outdir-classes}" /> <mkdir dir="${outdir}/${appname}" /> <mkdir dir="${basedir}/${output.dir}" /> </target> <!--2 生成R文件和Manifest.java--> <target name="aapt_gererateR" depends="init"> <exec executable="${aapt}" failonerror="true"> <arg value="package" /> <arg value="-m" /> <arg value="-J" /> <arg value="${outdir-gen}" /> <arg value="-M" /> <arg value="${manifest-xml}" /> <arg value="-S" /> <arg value="${resource-dir}" /> <arg value="-I" /> <arg value="${android-jar}" /> </exec> </target> <!--3 生成aidl对应的java文件--> <target name="aidl" depends="aapt_gererateR"> <apply executable="${aidl}" failonerror="true"> <arg value="-p${android-framework}" /> <arg value="-I${srcdir}" /> <arg value="-o${outdir-gen}" /> <fileset dir="${srcdir}"> <include name="**/*.aidl" /> </fileset> </apply> </target> <!--4 编译java文件为class文件--> <target name="compile" depends="aidl"> <javac debug="true" extdirs="" srcdir="." includeantruntime="on" destdir="${outdir-classes}" bootclasspath="${android-jar}" encoding="UTF-8"> <compilerarg line="-encoding UTF-8 " /> <classpath> <fileset dir="${external-libs}" includes="*.so" /> <fileset dir="${external-libs}" includes="**/*.so" /> <fileset dir="${external-libs}" includes="*/*.so" /> <fileset dir="${external-libs}" includes="**/*.jar" /> </classpath> </javac> </target> <!--5 混淆--> <target name="obfuscate" depends="compile"> <jar basedir="${outdir-classes}" destfile="temp.jar" /> <java jar="${proguard-home}/proguard.jar" fork="true" failonerror="true"> <jvmarg value="-Dmaximum.inlined.code.length=32" /> <arg value="-injars temp.jar" /> <arg value="-outjars optimized.jar" /> <arg value="-libraryjars '${annotations-jar}'" /> <arg value="-libraryjars '${android-jar}'" /> <arg value="@proguard-project.txt" /> </java> <delete file="temp.jar" /> <delete dir="${outdir-classes}" /> <mkdir dir="${outdir-classes}" /> <unzip src="optimized.jar" dest="${outdir-classes}" /> <delete file="optimized.jar" /> </target> <!--6 把class文件转换为dex文件--> <target name="dex" depends="obfuscate"> <apply executable="${dx}" failonerror="true" parallel="true"> <arg value="--dex" /> <arg value="--output=${intermediate-dex-ospath}" /> <arg path="${outdir-classes-ospath}" /> <arg path="${external-libs-ospath}" /> <fileset dir="${external-libs}" includes="*.so" /> <fileset dir="${external-libs}" includes="**/*.so" /> </apply> </target> <!--7 aapt打包资源--> <target name="aapt-package-res" depends="dex"> <echo>Packaging resources and assets...</echo> <echo>${resource-dir}</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}" /> </exec> </target> <!--8 apkbuilder打包成apk文件--> <target name="apkbuilder" depends="aapt-package-res"> <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="-nf" /> <arg value="${external-libs-ospath}" /> <arg value="-rj" /> <arg value="${basedir}${external-libs}" /> </exec> </target> <!--9 签名apk文件--> <target name="jarsigner" depends="apkbuilder"> <exec executable="${jarsigner}" failonerror="true"> <arg value="-verbose" /> <arg value="-keystore" /> <arg value="${key.store}" /> <arg value="-storepass" /> <arg value="${key.store.password}" /> <arg value="-keypass" /> <arg value="${key.alias.password}" /> <arg value="-signedjar" /> <arg value="${out-signed-package-ospath}" /> <arg value="${out-unsigned-package-ospath}" /> <arg value="${key.alias}" /> <arg value="-digestalg" /> <arg value="SHA1" /> <arg value="-sigalg" /> <arg value="MD5withRSA" /> </exec> </target> <!--10 对apk文件进行zipalign对齐--> <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="${zipalign-package-ospath}" /> </exec> </target> </project>