使用ant自动编译、打包生成apk文件
- - CSDN博客移动开发推荐文章 上次使用命令行生成apk文件 《Android 命令行编译、打包生成apk文件》,学习命令行生成的目的是为了编写ant打下基础. 下载ant包,配置环境变量. 二、ant编译Android项目. Java代码生成class文件. aapt命令, 生成R文件. -f 如果编译生成的文件已经存在,强制覆盖.
上次使用命令行生成apk文件 《Android 命令行编译、打包生成apk文件》,学习命令行生成的目的是为了编写ant打下基础。
一、 ant环境
下载ant包,配置环境变量
二、ant编译Android项目
<exec executable="aapt"> <arg value="package" /> <arg value="-m" /> <arg value="-J" /> <arg value="gen" /> <arg value="-S" /> <arg value="res" /> <arg value="-M" /> <arg value="AndroidManifest.xml" /> <arg value="-I" /> <arg value="${android-jar}" /> <arg value="--auto-add-overlay" /> <!-- 覆盖资源 --> </exec>
<javac target="1.5" destdir="bin/classes" bootclasspath="${android-jar}"> <src path="src" /> <src path="gen" /> </javac>
3. dx 命令,把class文件转换为.dex文件
转换为ant为:
<exec executable="dx.bat" failonerror="true"> <arg value="--dex" /> <arg value="--output=${project-dir}\bin\classes.dex" /> <arg path="${project-dir}\bin\classes" /> </exec>
转换为ant为:
<exec executable="aapt"> <arg value="package" /> <arg value="-f" /> <arg value="-M" /> <arg value="AndroidManifest.xml" /> <arg value="-S" /> <arg value="res" /> <arg value="-A" /> <arg value="assets" /> <arg value="-I" /> <arg value="${android-jar}" /> <arg value="-F" /> <arg value="bin\resources.ap_" /> <arg value="--auto-add-overlay" /> </exec>
转换为ant为:
<exec executable="apkbuilder.bat"> <arg value="${project-dir}\bin\unsigned.apk" /> <arg value="-u" /> <arg value="-z" /> <arg value="${project-dir}\bin\resources.ap_" /> <arg value="-f" /> <arg value="${project-dir}\bin\classes.dex" /> </exec>
转换为ant为:
<exec executable="jarsigner"> <arg value="-keystore" /> <arg value="${project-dir}\release.keystore" /> <arg value="-storepass" /> <arg value="antPassword" /> <arg value="-keypass" /> <arg value="antPassword" /> <arg value="-signedjar" /> <arg value="${project-dir}\bin\signed.apk" /> <arg value="${project-dir}\bin\unsigned.apk" /> <arg value="release" /> </exec>
三、完整build.xml文件
<?xml version="1.0" encoding="utf-8"?> <project default="main"> <property name="android-jar" value="D:\android-sdk-windows\platforms\android-8\android.jar" /> <property name="project-dir" value="D:\ProjectDemo" /> <target name="init"> <echo>start initing ... </echo> <mkdir dir="gen" /> <delete> <fileset dir="gen"></fileset> </delete> <mkdir dir="bin/classes" /> <delete> <fileset dir="bin/classes"></fileset> </delete> <echo>finish initing. </echo> </target> <target name="main" depends="init"> <echo>generating R.java for project to dir gen (using aapt) ... </echo> <exec executable="aapt"> <arg value="package" /> <arg value="-m" /> <arg value="-J" /> <arg value="gen" /> <arg value="-S" /> <arg value="res" /> <arg value="-M" /> <arg value="AndroidManifest.xml" /> <arg value="-I" /> <arg value="${android-jar}" /> <arg value="--auto-add-overlay" /> <!-- 覆盖资源 --> </exec> <javac target="1.5" destdir="bin/classes" bootclasspath="${android-jar}"> <src path="src" /> <src path="gen" /> </javac> <echo>packaging class files (include the third-party jars) to calsses.dex ... </echo> <exec executable="dx.bat" failonerror="true"> <arg value="--dex" /> <arg value="--output=${project-dir}\bin\classes.dex" /> <arg path="${project-dir}\bin\classes" /> </exec> <echo>packaging resource (include res, assets, AndroidManifest.xml, etc.) to res.zip ... </echo> <exec executable="aapt"> <arg value="package" /> <arg value="-f" /> <arg value="-M" /> <arg value="AndroidManifest.xml" /> <arg value="-S" /> <arg value="res" /> <arg value="-A" /> <arg value="assets" /> <arg value="-I" /> <arg value="${android-jar}" /> <arg value="-F" /> <arg value="bin\resources.ap_" /> <arg value="--auto-add-overlay" /> </exec> <echo>building unsigned.apk ... </echo> <exec executable="apkbuilder.bat"> <arg value="${project-dir}\bin\unsigned.apk" /> <arg value="-u" /> <arg value="-z" /> <arg value="${project-dir}\bin\resources.ap_" /> <arg value="-f" /> <arg value="${project-dir}\bin\classes.dex" /> </exec> <echo>signing the unsigned apk to final product apk ... </echo> <exec executable="jarsigner"> <arg value="-keystore" /> <arg value="${project-dir}\release.keystore" /> <arg value="-storepass" /> <arg value="antPassword" /> <arg value="-keypass" /> <arg value="antPassword" /> <arg value="-signedjar" /> <arg value="${project-dir}\bin\signed.apk" /> <arg value="${project-dir}\bin\unsigned.apk" /> <arg value="release" /> </exec> </target> <target name="clean"> </target> </project>
原文地址: http://blog.csdn.net/androiddevelop/article/details/11100109