android使用ANT打包,签名,混淆

<?xml version="1.0" encoding="GBK"?> <!-- 步骤 1.用aapt生成R.java 2.JAVAC编译JAVA文件 3.批处理生成classes.dex 4.aapt打包资源 5.apkbuilder包资源和类文件为APK文件 6.jarsinger 对APK进行签名--> <project name="MyJbox2D" default="zipalign" basedir="."> <property file="local.properties" /> <property name="jbox2dlib" value="D:/workspace/MyJbox2D/lib" /> <!-- 定义自己的输出目录 --> <property name="outdir" value="outdir" /> <property name="out.absolute.dir" location="${outdir}" /> <!-- 定义 R.java 所有目录 --> <property name="outdir-gen" value="gen" /> <!-- 读取目录 --> <property name="resource-dir" value="res" /> <property name="asset-dir" value="assets" /> <property name="src-dir" value="src" /> <property name="src-dir-ospath" value="${basedir}/${src-dir}" /> <!-- 输出目录 --> <property name="outdir-classes" value="${outdir}/classes" /> <property name="out.classes.absolute.dir" location="${outdir-classes}" /> <!-- 临时类资源文件 --> <property name="dex-file" value="classes.dex" /> <property name="intermediate-dex" value="${out.absolute.dir}/${dex-file}" /> <!-- 最终生成文件 --> <property name="out-package" value="${outdir}/${ant.project.name}.apk" /> <!-- 要使用的工具 --> <property name="aapt" value="${sdk-platform-tools}/aapt.exe" /> <property name="aidl" value="${sdk-platform-tools}/aidl.exe" /> <property name="dx" value="${sdk-platform-tools}/dx.bat" /> <property name="adb" value="${sdk-tools}/adb" /> <property name="apk-builder" value="${sdk-tools}/apkbuilder.bat" /> <property name="android-jar" value="${sdk-platform}/android.jar" /> <!-- 最终生成各种包文件 --> <property name="resources-package" value="${outdir}/${ant.project.name}" /> <property name="resources-package-ospath" value="${basedir}/${resources-package}" /> <property name="out-unsigned-package" value="${outdir}/${ant.project.name}-unsigned.apk" /> <property name="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" /> <property name="out-signed-package" value="${outdir}/${ant.project.name}-signed.apk" /> <property name="out-signed-package-ospath" value="${basedir}/${out-signed-package}" /> <property name="zipalign-package-ospath" value="${basedir}/${outdir}/${ant.project.name}_release.apk" /> <!--签名工具--> <property name="jarsigner" value="${jdk-home}/bin/jarsigner.exe" /> <!--优化APK工具--> <property name="zipalign" value="${sdk-tools}/zipalign.exe" /> <!-- 初始化 --> <target name="init"> <echo>Creating all output directories </echo> <delete dir="${outdir}" /> <delete dir="${outdir-classes}" /> <delete dir="${outdir-gen}" /> <mkdir dir="${outdir}" /> <mkdir dir="${outdir-classes}" /> <mkdir dir="${outdir-gen}" /> </target> <!--步骤 1: aapt生成R.java --> <target name="resource-src" depends="init"> <echo>Generating R.java...</echo> <exec executable="${aapt}" failonerror="true"> <arg value="package" /> <arg value="-m" /> <arg value="-J" /> <arg value="${outdir-gen}" /> <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="init"> <echo>Compiling aidl files into Java classes...</echo> <apply executable="${aidl}" failonerror="true"> <arg value="-p${android-framework}" /> <arg value="-I${src-dir}" /> <fileset dir="${src-dir}"> <include name="**/*.aidl" /> </fileset> </apply> </target> <!-- 步骤 2. 编译所有class文件. --> <target name="compile" depends="init, resource-src, aidl"> <unjar src="${jbox2dlib}/jbox2d.jar" mce_src="${jbox2dlib}/jbox2d.jar" dest="${outdir-classes}" /> <javac encoding="GBK" target="1.6" debug="true" extdirs="" srcdir="." destdir="${outdir-classes}" bootclasspath="${android-jar}" /> </target> <!-- 混淆代码--> <target name="proguard" 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 ${android-jar}" /> <arg value="-dontpreverify" /> <arg value="-dontoptimize" /> <arg value="-dontusemixedcaseclassnames" /> <arg value="-repackageclasses ''" /> <arg value="-allowaccessmodification" /> <!-- 要保留自己的主ACTIVITY类 --> <arg value="-keep public class com.jbox2d.demo.MyActivity" /> <arg value="-optimizationpasses 7" /> <arg value="-verbose" /> <arg value="-dontskipnonpubliclibraryclasses" /> <arg value="-dontskipnonpubliclibraryclassmembers" /> </java> <delete file="temp.jar" /> <delete dir="${outdir-classes}" /> <mkdir dir="${outdir-classes}" /> <unzip src="optimized.jar" mce_src="optimized.jar" dest="${outdir-classes}" /> <delete file="optimized.jar" /> </target> <!-- 步骤 3. 转换class文件为dex文件. --> <target name="dex" depends="proguard"> <echo>Converting compiled files into ${intermediate-dex}... </echo> <exec executable="${dx}" failonerror="true"> <arg value="--dex" /> <arg value="--output=${intermediate-dex}" /> <arg path="${out.classes.absolute.dir}" /> </exec> </target> <!-- 步骤 4. 将资源和清单打包. --> <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> <!-- 步骤 5. 将打好的包编译为APK文件. --> <target name="debug" depends="dex, package-res-and-assets"> <echo>release:Packaging ${out-debug-package-ospath}, and signing it with a debug key...</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}" /> <arg value="-rf" /> <arg value="${src-dir-ospath}" /> </exec> </target> <!--步骤 6. 签名APK文件 --> <target name="jarsigner" depends="debug"> <echo> jarsigner ${out-signed-package-ospath}</echo> <exec executable="${jarsigner}" failonerror="true"> <arg value="-verbose" /> <arg value="-storepass" /> <arg value="${keypass}" /> <arg value="-keystore" /> <arg value="${keystore}" /> <arg value="-signedjar" /> <arg value="${out-signed-package-ospath}" /> <arg value="${out-unsigned-package-ospath}" /> <arg value="${key}" /> </exec> </target> <!--zipalign优化android程序--> <target name="zipalign" depends="jarsigner"> <echo> zipalign ${zipalign-package-ospath}</echo> <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> <!-- 安装到默认的手机或模拟器--> <target name="install" depends="zipalign"> <echo>Installing ${out-debug-package} onto default emulator...</echo> <exec executable="${adb}" failonerror="true"> <arg value="install" /> <arg value="${out-signed-package}" /> </exec> </target> </project>     好久没有写BLOG了,哈哈,本人有点小懒加上家里也没有网络,昨天学习了下ANT打包部署,在网上查了下资料综合了一下,于是有了

 

这往篇文章,研究了一天了,总算是搞定了。不多说了,上代码!

 

你可能感兴趣的:(java,android,ant,Class,resources,encoding)