<?xml version="1.0" encoding="UTF-8"?> <project name="PushFastDemo" basedir="." default="package" > <property environment="env" /> <property name="demo.name" value="PushDemo" /> <property name="android.dir" value="${env.ANDROID_HOME}/platforms/android-19" /> <property name="jarsigner.dir" value="${env.JAVA_HOME}/bin/jarsigner" /> <property name="aapt.dir" value="${env.ANDROID_HOME}/build-tools/android-4.4W/aapt" /> <property name="dx.dir" value="${env.ANDROID_HOME}/build-tools/android-4.4W/dx" /> <property name="sdklib.dir" value="${env.ANDROID_HOME}/tools/lib/sdklib.jar" /> <property name="version" value="4.4" /> <property name="sdk.dir" value="${env.ANDROID_HOME}" /> <property name="bin.dir" value="${basedir}/bin" /> <property name="libs.dir" value="${basedir}/libs" /> <property name="gen.dir" value="${basedir}/gen" /> <property name="src.dir" value="${basedir}/src" /> <property name="classes.dir" value="${bin.dir}/classes" /> <property name="keystore.dir" value="${basedir}/demo.keystore" /> <property name="keystore-password" value="demodebug" /> <property name="alias" value="lulei" /> <property name="alias-password" value="demodebug" /> <target name="clean" description="clean the directory" > <echo message="正在删除之前的生成的包/文件..." /> <delete dir="${bin.dir}" /> <delete dir="${libs.dir}" /> <delete dir="${gen.dir}" /> <echo message="删除之前的生成的包/文件完成" /> </target> <target name="init" depends="clean" description="init for compile" > <echo message="正在准备编译依赖..." /> <mkdir dir="${bin.dir}" /> <mkdir dir="${classes.dir}" /> <mkdir dir="${gen.dir}" /> <mkdir dir="${libs.dir}" /> <mkdir dir="${libs.dir}/armeabi" /> <mkdir dir="${libs.dir}/mips" /> <copy file="../../output/pushservice-${version}.jar" todir="${libs.dir}" /> <copy file="../../output/armeabi/libbdpush_V2_2.so" todir="${libs.dir}/armeabi" /> <copy file="../../output/mips/libbdpush_V2_2.so" todir="${libs.dir}/mips" /> <echo message="编译依赖准备完成" /> </target> <!-- 因为demo中的java代码依赖R文件生成的R.java,所以先生成R.java --> <target name="compile-resouces" depends="init" description="compile the res/ assets/ to R.java" > <echo message="正在编译资源文件..." /> <exec executable="${aapt.dir}" failonerror="true" > <arg value="package" /> <arg value="-m" /> <arg value="--auto-add-overlay" /> <arg value="-J" /> <arg value="${gen.dir}" /> <arg value="-S" /> <arg value="${basedir}/res" /> <arg value="-A" /> <arg value="${basedir}/assets" /> <arg value="-I" /> <arg value="${android.dir}/android.jar" /> <arg value="-M" /> <arg value="${basedir}/AndroidManifest.xml" /> </exec> <echo message="编译资源文件完成" /> </target> <target name="compile" depends="compile-resouces" description="compile *.java to *.class" > <echo message="正在编译源文件..." /> <!-- 编译时所依赖的jar 文件集合 --> <path id="classpath" > <fileset dir="${libs.dir}" > <include name="**/*.jar" /> </fileset> <fileset dir="${android.dir}" > <include name="**/*.jar" /> </fileset> <pathelement path="${classes.dir}" /> </path> <javac destdir="${classes.dir}" encoding="UTF-8" > <src path="${src.dir}" /> <src path="${gen.dir}" /> <include name="**/*.java" /> <classpath refid="classpath" /> </javac> <echo message="编译源文件完成" /> </target> <target name="generate-dex" depends="compile" description="transform the *.class files to .dex file" > <echo message="正在生成dex文件..." /> <exec executable="${dx.dir}" failonerror="true" > <arg value="--dex" /> <!-- 输出文件 --> <arg value="--output=${bin.dir}/classes.dex" /> <!-- 要生成.dex文件的源classes和libraries --> <arg path="${classes.dir}" /> <arg path="${libs.dir}" /> </exec> <echo message="dex文件已经生成" /> </target> <target name="package-resource" depends="generate-dex" description="package the res/ assets/ into a package" > <echo message="正在打包资源文件..." /> <exec executable="${aapt.dir}" failonerror="true" > <arg value="package" /> <arg value="-f" /> <arg value="-M" /> <arg value="${basedir}/AndroidManifest.xml" /> <arg value="-A" /> <arg value="${basedir}/assets" /> <arg value="-S" /> <arg value="${basedir}/res" /> <arg value="-I" /> <arg value="${android.dir}/android.jar" /> <arg value="-F" /> <arg value="${bin.dir}/resouces.ap_" /> <arg value="--auto-add-overlay" /> </exec> <echo message="资源文件已打包完成" /> </target> <target name="package" depends="package-resource" description="package the res/ assets/ into a package" > <echo message="正在生成apk文件..." /> <java classname="com.android.sdklib.build.ApkBuilderMain" > <classpath> <path location="${sdklib.dir}" /> </classpath> <arg value="${bin.dir}/${demo.name}-${version}-unsign.apk" /> <arg value="-u" /> <arg value="-z" /> <arg value="${bin.dir}/resouces.ap_" /> <arg value="-f" /> <arg value="${bin.dir}/classes.dex" /> <arg value="-rf" /> <arg value="${basedir}/src" /> <arg value="-nf" /> <arg value="${libs.dir}" /> </java> <echo message="apk文件已打生成" /> </target> <!-- 对apk进行签名,签名前先将自己的密钥拷贝到该工程目录下 --> <target name="sign" depends="package" > <echo message="正在对生成的包进行签名..." /> <exec executable="${jarsigner.dir}" failonerror="true" > <arg value="-keystore" /> <arg value="${keystore.dir}" /> <arg value="-storepass" /> <arg value="${keystore-password}" /> <arg value="-keypass" /> <arg value="${alias-password}" /> <arg value="-signedjar" /> <arg value="${bin.dir}/${demo.name}-${version}.apk" /> <arg value="${bin.dir}/${demo.name}-${version}-unsign.apk" /> <arg value="${alias}" /> </exec> <delete dir="${bin.dir}/${demo.name}-${version}-unsign.apk" /> <echo message="包签名完成" /> </target> <target name="install" depends="sign" description="install the apk file to your phone" > <echo message="正在安装到你的手机..." /> <exec executable="adb" > <arg value="install" /> <arg value="-r " /> <arg path="${bin.dir}/${demo.name}-${version}.apk" /> </exec> <echo message="安装完成" /> </target> </project>
注意事项:
1. 使用jarsigner时,如果是java 1.7(包括1.7)以上的话,要指定签名算法,否则有的机型安装会失败,即在jarsign 中添加如下的参:
<arg value="-digestalg"/> <arg value="SHA1"/> <arg value="-sigalg"/> <arg value="MD5withRSA"/>