Ant实现Android项目自动化打包
需要的资源:
Ant下载地址:http://ant.apache.org/
Ant环境变量需要配置-----详情看百度经验
1.build.xml 脚本
<span style="font-family:Microsoft YaHei;"><?xml version="1.0" encoding="utf-8" standalone="no"?> <project default="release" name="yourprojectName"> <property file="local.properties"/> <property file="project.properties"/> <property environment="env"/> <property name="channelname" value="eshore_android"/> <property name="channelkey" value="1234567890"/> <property location="${apk.out.dir}/${project.name}_${channelname}_${project.version}.apk" name="out.final.file"/> <property name="appName" value="yourappName"/> <!--basedir:项目的路径 library-dir*:依赖项目的路径 --> <property name="basedir" value="D:\project"/> <property name="library-dir" value="D:\library1"/> <property name="library-dir2" value="D:\library2"/> <property name="library-dir3" value="D:\library3"/> <!--替换工程中出现的包名 不需要改包名则此处可删除 --> <property name="old_package_name" value="com.test.test"/> <property name="new_package_name" value="com.test.newname"/> <!--替换Manifest.xml的包名 通过字符串匹配 不需要改包名则此处可删除--> <replaceregexp byline="true" encoding="UTF-8" flags="g"> <regexp pattern="${old_package_name}"/> <substitution expression="${new_package_name}"/> <fileset dir="${basedir}" includes="AndroidManifest.xml"/> </replaceregexp> <!--同理替换*.java文件的包名 通过字符串匹配 不需要改包名则此处可删除--> <replaceregexp byline="true" encoding="UTF-8" flags="g"> <regexp pattern="${old_package_name}"/> <substitution expression="${new_package_name}"/> <fileset dir="${basedir}/src" includes="**/*.java"/> </replaceregexp> <!--同理替换*.xml文件的包名 (自定义布局引用的包名) 通过字符串匹配 不需要改包名则此处可删除--> <replaceregexp byline="true" encoding="UTF-8" flags="g"> <regexp pattern="${old_package_name}"/> <substitution expression="${new_package_name}"/> <fileset dir="${basedir}/res/layout" includes="*.xml"/> </replaceregexp> <!--Android SDK的目录路径--> <property name="sdk-folder" value="D:\Android SDK\android-sdk"/> <property name="sdk-platform-folder" value="${sdk-folder}\platforms\android-19"/> <property name="sdk-tools" value="${sdk-folder}\tools"/> <property name="sdk-platform-tools" value="${sdk-folder}\platform-tools"/> <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="apkbuilder" value="${sdk-tools}\apkbuilder.bat"/> <property name="jarsigner" value="${env.JAVA_HOME}\bin\jarsigner"/> <property name="android-jar" value="${sdk-platform-folder}\android.jar"/> <property name="framework-aidl" value="${sdk-platform-folder}\framework.aidl"/> <property name="outdir-gen" value="gen"/> <property name="outdir-gen-path" value="${basedir}\${outdir-gen}"/> <property name="outdir-bin" value="out"/> <property name="outdir-bin-path" value="${basedir}\${outdir-bin}"/> <property name="manifest-xml" value="${basedir}\AndroidManifest.xml"/> <property name="resource-dir" value="res"/> <property name="asset-dir" value="assets"/> <property name="srcdir" value="src"/> <property name="srcdir-ospath" value="${basedir}\${srcdir}"/> <property name="external-lib" value="libs"/> <property name="external-lib-ospath" value="${basedir}\${external-lib}"/> <property name="outdir-classes" value="${outdir-bin}"/> <property name="outdir-classes-ospath" value="${basedir}\${outdir-classes}"/> <property name="dex-file" value="classes.dex"/> <property name="dex-path" value="${outdir-bin}\${dex-file}"/> <property name="dex-ospath" value="${basedir}\${dex-path}"/> <property name="resources-package" value="${outdir-bin}\resources.ap_"/> <property name="resources-package-ospath" value="${basedir}\${resources-package}"/> <property name="out-unsigned-package" value="${appName}-unsigned.apk"/> <property name="out-unsigned-package-ospath" value="${basedir}\${outdir-bin}\${out-unsigned-package}"/> <!--工程的密钥--> <property name="keystore-file" value="${basedir}\android.keystore"/> <!--打包的.apk名称--> <property name="out-signed-package" value="Project-release.apk"/> <property name="out-signed-package-ospath" value="${basedir}\${outdir-bin}\${out-signed-package}"/> <!--修改外层的包名文件夹 不需要改包名则此处可删除--> <target name="copyfile"> <echo>copy new package file</echo> <condition property="buildexists"> <available file="${basedir}/src/com/test/test" type="dir"></available> </condition> <antcall target="trueCondition"> </antcall> </target> <target name="trueCondition" if="buildexists"> <echo>trueCondition</echo> <move file="${basedir}/src/com/test/test" tofile="${basedir}/src/com/test/newname"/> </target> <!--初始化输出路径--> <target name="init"> <echo>Initializing all output directories...</echo> <delete dir="${outdir-bin}"/> <mkdir dir="${outdir-bin}"/> <mkdir dir="${outdir-classes}"/> <!--不需要改包名则此处可删除--> <antcall target="copyfile"> </antcall> </target> <!--依赖target-init 从资源生成R.java文件--> <target depends="init" name="gen-R"> <echo>Generating R.java from the resources...</echo> <echo>basedir path:${basedir}</echo> <echo>Res path:${resource-dir}</echo> <echo>Src path:${srcdir-ospath}</echo> <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="-S"/> <arg value="${library-dir2}/res"/> <arg value="-S"/> <arg value="${library-dir3}/res"/> <arg value="-I"/> <arg value="${android-jar}"/> <arg value="--auto-add-overlay"/> </exec> </target> <!--编译成Java文件中--> <target depends="gen-R" name="aidl"> <echo>Compiling .aidl into java files...</echo> <apply executable="${aidl}" failonerror="true"> <arg value="-p${framework-aidl}"/> <arg value="-I${srcdir}"/> <arg value="-o${outdir-gen}"/> <fileset dir="${srcdir}"> <include name="**/*.aidl"/> </fileset> </apply> </target> <!--编译源代码--> <target depends="aidl" name="compile"> <echo>Compiling java source code...</echo> <javac bootclasspath="${android-jar}" destdir="${outdir-classes}" encoding="utf-8" includeantruntime="on" target="1.6"> <src path="src"/> <src path="gen"/> <src path="${library-dir}\src"/> <classpath> <fileset dir="${external-lib}" includes="*.jar"/> <fileset dir="${library-dir}\libs" includes="*.jar"/> <filelist> <file name="${android-maps-jar}"/> </filelist> </classpath> </javac> </target> <!--将编译后的文件和外部库成dex文件--> <target depends="compile" name="dex"> <echo>Converting compiled files and external libraries into a .dex file... </echo> <exec executable="${dx}" failonerror="true"> <arg value="--dex"/> <arg value="--output=${dex-ospath}"/> <arg value="${outdir-classes-ospath}"/> <arg value="${external-lib-ospath}"/> </exec> </target> <!--包装assets的资源--> <target depends="dex" 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="${manifest-xml}"/> <arg value="-S"/> <arg value="${resource-dir}"/> <arg value="-S"/> <arg value="${library-dir2}/res"/> <arg value="-S"/> <arg value="${library-dir3}/res"/> <arg value="-A"/> <arg value="${asset-dir}"/> <arg value="-I"/> <arg value="${android-jar}"/> <arg value="-F"/> <arg value="${resources-package}"/> <arg value="--auto-add-overlay"/> </exec> </target> <!--包装未签名的APK发布--> <target depends="dex, package-res-and-assets" name="package"> <echo>Packaging unsigned apk for release...</echo> <exec executable="${apkbuilder}" failonerror="true"> <arg value="${out-unsigned-package-ospath}"/> <arg value="-u"/> <arg value="-z"/> <arg value="${resources-package-ospath}"/> <arg value="-f"/> <arg value="${dex-ospath}"/> <arg value="-rf"/> <arg value="${srcdir-ospath}"/> <arg value="-nf"/> <arg value="${library-dir}\libs"/> </exec> <echo>It will need to be signed with jarsigner before being published. </echo> </target> <!--包装签名的APK发布--> <target depends="package" name="jarsigner"> <echo>Packaging signed apk for release...</echo> <exec executable="${jarsigner}" failonerror="true"> <arg value="-digestalg"/> <arg value="SHA1"/> <arg value="-sigalg"/> <arg value="MD5withRSA"/> <arg value="-keystore"/> <arg value="${keystore-file}"/> <arg value="-storepass"/> <arg value="123456"/> <arg value="-keypass"/> <arg value="123456"/> <arg value="-signedjar"/> <arg value="${out-signed-package-ospath}"/> <arg value="${out-unsigned-package-ospath}"/> <arg value="android.keystore"/> </exec> </target> <target depends="jarsigner" name="release"> <echo>APK is released. path:${out-unsigned-package-ospath}</echo> <echo>APK is released. path:${out-signed-package-ospath}</echo> </target> </project></span>
2.java代码执行build.xml脚本
<span style="font-family:Microsoft YaHei;"></span><pre name="code" class="java"><span style="font-family:Microsoft YaHei;"></span><pre name="code" class="java"><span style="font-family:Microsoft YaHei;">AntCreateApp.java</span>public class AntCreateApp { private String projectBasePath; // "D:\\prijectName"; private String signApk = "Project-release.apk"; private String reNameApk = "appName"; private Project project = new Project();public void setProjectBasePath(String _projectBasePath) {this.projectBasePath = _projectBasePath;} public String getProjectBasePath() {return projectBasePath;} public String getSignApk() {return signApk;}public String getReNameApk() {return reNameApk;}public void init(String _buildFile, String _baseDir) throws Exception {project.init();DefaultLogger consoleLogger = new DefaultLogger();consoleLogger.setErrorPrintStream(System.err);consoleLogger.setOutputPrintStream(System.out);consoleLogger.setMessageOutputLevel(org.apache.tools.ant.Project.MSG_INFO);project.addBuildListener(consoleLogger);// Set the base directory. If none is given, "." is used.if (_baseDir == null)_baseDir = new String(".");project.setBasedir(_baseDir);System.out.println("_buildFile:" + _buildFile);System.out.println("new_buildFile:" + getProjectBasePath()+ File.separator + "build.xml");if (_buildFile == null)_buildFile = new String(getProjectBasePath() + File.separator+ "build.xml"); ProjectHelper.configureProject(project, new File(_buildFile));}public void runTarget(String _target) throws Exception {// Test if the project existsif (project == null)throw new Exception("No target can be launched because the project has not been initialized. Please call the 'init' method first !");// If no target is specified, run the default one.if (_target == null)_target = project.getDefaultTarget();// Run the targetproject.executeTarget(_target);} }
调用的方法:
<span style="font-family:Microsoft YaHei;">public String compile() { AntCreateApp antCreateApp = new AntCreateApp(); antCreateApp.setProjectBasePath("projectPath"); try { String fileName = antCreateApp.getProjectBasePath() + "\\build.xml"; System.out.println("fileName:" + fileName); antCreateApp.init(fileName, antCreateApp.getProjectBasePath()); antCreateApp.runTarget(null); renameFile.delete(); boolean renametag = file.renameTo(renameFile); System.out.println("rename------>" + renametag); System.out.println("file ------>" + file.getAbsolutePath()); System.out.println("rename------>" + renameFile.getAbsolutePath()); System.out.println("---------ant自动化打包结束----------"); } catch (Exception e) { e.printStackTrace(); System.out.println("---------ant自动化打包中发生异常----------"); } }</span>