最最近项目要实现一个自动打包的功能,网上参考了不少资料,觉得还是使用ant打包比较方便。
到最后的写出的配置信息如下所示:
build.properties
#project name and version project.name=project1 project.version=1.0.0 #android platform version android-platform=android-16 #keysore file ketstore.file=project1.keystore key.alias=project1 key.alias.password=*** key.store.password=***
build.xml
<?xml version="1.0" encoding="UTF-8"?> <project name="autopack" default="zipalignapk"> <!-- 项目原路径 四个项目都存放在该目录下面--> <!-- <property name="copy.src.dir" value="${basedir}/project"/> --> <property name="copy.src.dir" value="/data/project"/> <!--打包配置 --> <property file="build.properties" /> <!-- 拷贝项目保存路径 --> <property name="userId" value="${userId}"/> <property name="appVersion" value="v1.0.01" /> <property name="copy.dir" value="${basedir}/tmp/${userId}"/> <!-- 版本 --> <property name="project.version" value="${project.version}" /> <!-- 应用名称 --> <property name="project.name" value="${project.name}" /> <property name="project.briefing" value="${project.briefing}" /> <!-- 第三方library --> <!-- lib工程相关配置 project4 --> <property name="project4.dir" value="${basedir}/tmp/${userId}/project4" /> <property name="project4.res" location="${project4.dir}/res" /> <property name="project4.src" location="${project4.dir}/src" /> <property name="project4.lib" location="${project4.dir}/libs" /> <!-- lib工程相关配置 project3 --> <property name="project3.dir" value="${basedir}/tmp/${userId}/project3" /> <property name="project3.res" location="${project3.dir}/res" /> <property name="project3.src" location="${project3.dir}/src" /> <property name="project3.lib" location="${project3.dir}/libs" /> <!-- lib工程相关配置 project2 --> <property name="project2.dir" value="${basedir}/tmp/${userId}/project2" /> <property name="project2.res" location="${project2.dir}/res" /> <property name="project2.src" location="${project2.dir}/src" /> <property name="project2.lib" location="${project2.dir}/libs" /> <!-- 指定环境变量参数为:SystemVariable windows--> <property environment="SystemVariable" /> <!-- 指定环境变量参数为:env linux--> <!-- <property environment="env" />--> <!-- SDK目录(获取操作系统环境变量SDK_HOME的值) --> <property name="sdk-folder" value="${SystemVariable.SDK_HOME}" /> <!-- SDK指定平台目录 --> <property name="sdk-platform-folder" value="${sdk-folder}/platforms/${android-platform}" /> <!-- SDK中tools目录 --> <property name="sdk-tools" value="${sdk-folder}/tools" /> <!-- SDK指定平台中tools目录 --> <property name="sdk-build-tools" value="${sdk-folder}/build-tools/19.1.0" /> <!-- 使用到的命令(当前系统为windows,如果系统为linux,可将.bat文件替换成相对应的命令) --> <property name="aapt" location="${sdk-build-tools}/aapt" /> <property name="dx" location="${sdk-build-tools}/dx" /> <property name="zipalign" location="${sdk-build-tools}/zipalign" /> <property name="apkbuilder" location="${sdk-tools}/apkbuilder" /> <property name="jarsigner" location="${SystemVariable.JAVA_HOME}/bin/jarsigner" /> <!-- 编译需要的jar --> <property name="android-jar" location="${sdk-platform-folder}/android.jar" /> <!-- 主项目 源文件目录 --> <property name="project1.dir" value="${basedir}/tmp/${userId}/project1" /> <property name="project1.src" location="${project1.dir}/src" /> <property name="project1.bin" location="${project1.dir}/bin" /> <property name="project1.gen" location="${project1.dir}/gen" /> <property name="project1.res" location="${project1.dir}/res" /> <property name="project1.lib" location="${project1.dir}/libs" /> <property name="project1.assets" location="${project1.dir}/assets" /> <property name="manifest-xml" location="${project1.dir}/AndroidManifest.xml" /> <property name="project1.dex" value="${project1.dir}/bin/classes.dex" /> <property name="project1.resources" value="${project1.dir}/bin/resources.ap_" /> <!-- 未签名APK包 --> <property name="project1.unsigned" value="${project1.bin}/${project.name}-unsigned.apk" /> <!-- 证书文件 --> <property name="keystore-file" value="${basedir}/${ketstore.file}" /> <!-- 签名APK包 --> <property name="project1.signed" value="${project1.bin}/${project.name}.apk" /> <!-- 签名APK优化包 --> <property name="out-signed-zipaligned" value="${basedir}/build/${userId}.apk" /> <!-- 初始化工作 --> <target name="copy-dir"> <echo>======== copy ======</echo> <delete dir="${copy.dir}" /> <mkdir dir="${copy.dir}" /> <copy encoding="UTF-8" todir="${copy.dir}" overwrite="true"> <fileset dir="${copy.src.dir}"> <!-- 忽略隐藏文件 --> <exclude name=".*" /> <exclude name=".*/*" /> </fileset> </copy> </target> <!-- 清理不需要文件 --> <target name="clean-dir" depends="copy-dir"> <echo>======== clean ==========</echo> <delete includeemptydirs="true" verbose="true"> <fileset dir="${project1.bin}" includes="**/*"></fileset> </delete> <delete includeemptydirs="true" verbose="true"> <fileset dir="${project1.gen}" includes="**/*"></fileset> </delete> <mkdir dir="${project1.bin}/classes" /> </target> <!-- 根据用户设置修改APP信息 --> <target name="update" depends="clean-dir"> <echo>======== 根据用户设置修改APP名称和简介信息 ==========</echo> <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="${basedir}/xmltask.jar" /> <!--需要修改的文件 --> <xmltask source="${project1.res}/values/strings.xml" dest="${project1.res}/values/strings.xml" preserveType="true" encoding="utf-8"> <replace path="/resources/string[@name='app_name']/text()" withText="${project.name}"/> <!--<replace path="/resources/string[@name='about']/text()" withText="关于我们"/> <replace path="/resources/string[@name='about_activity']/text()" withText="关于我们"/> <replace path="/resources/string[@name='about_app']/text()" withText="${project.briefing}"/>--> </xmltask> <echo>======== 根据用户设置修改APP LOGO图片 ==========</echo> <delete file="${project1.res}/drawable-hdpi/notify_newmessage.png"/> <!-- todir:需要的替换文件目录 file:替换文件 --> <copy todir="${project1.res}/drawable-hdpi" file="${basedir}/build/${userId}/120-120/notify_newmessage.png"/> </target> <!-- 根据工程中的资源文件生成R.java文件 --> <target name="generate-R" depends="update"> <echo>======== 生成R.java文件 ========</echo> <exec executable="${aapt}" failonerror="true"> <arg value="package" /> <arg value="-m" /> <arg value="--auto-add-overlay" /> <arg value="-J" /> <!--R.java文件的生成路径--> <arg value="${project1.gen}" /> <!-- 指定清单文件 --> <arg value="-M" /> <arg value="${manifest-xml}" /> <!-- 指定资源目录 --> <arg value="-S" /> <arg value="${project1.res}" /> <!-- 注意点:同时需要调用Library的res--> <arg value="-S" /> <arg value="${project2.res}" /> <arg value="-S" /> <arg value="${project3.res}" /> <arg value="-S" /> <arg value="${project4.res}" /> <arg value="--extra-packages" /> <!-- 设置包名 --> <arg value="com.project2:com.djh.project3:com.project4" /> <arg value="-A" /> <arg value="${project1.assets}" /> <!-- 导入类库 --> <arg value="-I" /> <arg value="${android-jar}" /> </exec> </target> <!-- 第三方jar包需要引用,用于辅助编译 --> <target name="import-jar" depends="generate-R"> <echo>======== import jar ========</echo> <path id="compile.libs"> <fileset dir="${project1.lib}" includes="*.jar" /> <fileset dir="${project2.lib}" includes="*.jar" /> <fileset dir="${project3.lib}" includes="*.jar" /> <fileset dir="${project4.lib}" includes="*.jar" /> </path> </target> <!-- 将工程中的java源文件编译成class文件 --> <target name="compile" depends="import-jar"> <echo> ========java源文件编译成class文件 ======== </echo> <!-- 库应用编译class 生成的class文件全部保存到主项目的classes目录下--> <javac encoding="UTF-8" destdir="${project1.bin}/classes" bootclasspath="${android-jar}" target="1.6" source="1.6" includeantruntime="true"> <compilerarg line="-encoding UTF-8 " /> <src path="${project2.src}" /><!-- 库应用源码 --> <src path="${project1.gen}" /><!-- R.java 资源类的导入 --> <classpath refid="compile.libs" /> </javac> <javac encoding="UTF-8" destdir="${project1.bin}/classes" bootclasspath="${android-jar}" target="1.6" source="1.6" includeantruntime="true"> <compilerarg line="-encoding UTF-8 " /> <src path="${project2.src}" /> <src path="${project1.gen}" /> <classpath refid="compile.libs" /> </javac> <javac encoding="UTF-8" destdir="${project1.bin}/classes" bootclasspath="${android-jar}" target="1.6" source="1.6" includeantruntime="true"> <compilerarg line="-encoding UTF-8 " /> <src path="${project4.src}" /> <src path="${project1.gen}" /> <classpath refid="compile.libs" /> </javac> <!-- 主应用编译class --> <javac encoding="UTF-8" destdir="${project1.bin}/classes" bootclasspath="${android-jar}" target="1.6" source="1.6" includeantruntime="true"> <compilerarg line="-encoding UTF-8 " /> <src path="${project1.src}" /><!-- 工程源码--> <src path="${project1.gen}" /> <classpath refid="compile.libs" /> </javac> </target> <!-- 将.class文件转化成.dex文件 --> <target name="dex" depends="compile"> <echo> ======== 将.class文件转化成.dex文件 ========</echo> <exec executable="${dx}" failonerror="true"> <arg value="--dex" /> <!-- 输出文件 --> <arg value="--output=${project1.dex}" /> <!-- 要生成.dex文件的源classes和libraries --> <arg value="${project1.bin}/classes" /> <!-- <arg value="${external-lib-ospath}" /> --> <arg value="${project1.lib}" /> <arg value="${project2.lib}" /> <arg value="${project3.lib}" /> <arg value="${project4.lib}" /> </exec> </target> <!-- 将资源文件放进输出目录 --> <target name="assets"> <echo>======== 将资源文件放进输出目录 ========</echo> <exec executable="${aapt}" failonerror="true"> <arg value="package" /> <arg value="-f" /> <arg value="-M" /> <arg value="${manifest-xml}" /> <arg value="-S" /> <arg value="${project1.res}"/> <arg value="-S"/> <arg value="${project2.res}"/> <arg value="-S"/> <arg value="${project3.res}"/> <arg value="-S"/> <arg value="${project4.res}"/> <!-- asset目录也打包 --> <arg value="-A" /> <arg value="${project1.assets}" /> <arg value="-I" /> <arg value="${android-jar}" /> <!-- 输出资源压缩包 --> <arg value="-F" /> <arg value="${project1.resources}" /> <arg value="--auto-add-overlay" /> </exec> </target> <!-- 打包成未签证的apk --> <target name="package" depends="dex,assets"> <echo>======== 打包成未签证的apk ========</echo> <java classpath="${sdk-tools}/lib/sdklib.jar" classname="com.android.sdklib.build.ApkBuilderMain"> <!-- 输出路径 --> <arg value="${project1.unsigned}" /> <arg value="-u" /> <arg value="-z" /> <!-- 资源压缩包 --> <arg value="${project1.resources}" /> <arg value="-f" /> <!-- dex压缩文件 --> <arg value="${project1.dex}" /> <arg value="-rj" /> <arg value="${project1.lib}"/> <!-- 将主项目libs下面的so库打包 --> <arg value="-nf" /> <arg value="${project1.lib}" /> </java> </target> <!-- 对apk进行签证 --> <target name="jarsigner" depends="package"> <echo>======== 对apk进行签证 ${basedir}/${keystore-file} ========</echo> <exec executable="${jarsigner}" failonerror="true"> <arg value="-keystore" /> <arg value="${keystore-file}" /> <arg value="-storepass" /> <arg value="${key.store.password}" /> <arg value="-keypass" /> <arg value="${key.alias.password}" /> <arg value="-signedjar" /> <arg value="${project1.signed}" /> <arg value="${project1.unsigned}" /> <!-- 不要忘了证书的别名 --> <arg value="${key.alias}" /> </exec> </target> <!-- 发布 --> <target name="release" depends="jarsigner"> <echo>======== 删除未签证apk ========</echo> <delete file="${project1.unsigned}" /> <echo>========删除其他文件,最后只保留apk ========</echo> <delete dir="${project1.bin}/classes"/> <delete file="${project1.dex}" /> <delete file="${project1.resources}" /> <echo>======== 生成apk完成 ========</echo> </target> <!-- 打包的应用程序进行优化 --> <target name="zipalignapk" depends="release"> <echo>======== 优化apk ========</echo> <echo>======== 删除已存在的 优化apk ========</echo> <delete file="${out-signed-zipaligned}"/> <exec executable="${zipalign}" failonerror="true"> <arg value="-v" /> <arg value="4" /> <arg value="${project1.signed}" /> <arg value="${out-signed-zipaligned}" /> </exec> </target> </project>
注意:这里还有一个project1.keystore的文件,这是apk的认证文件,这个我就不提供了。在打包是还需要把xmltask.jar 添加打包环境中去。
好了,配置信息已经写好了,
在运行中输入cmd,并进入build.xml目录中,并输入:
ant build.xml -DuserId=1 -Dproject.name=QQ -Dproject.briefing=这是QQ
输入后回车即可执行打包。