使用Ant脚本打包Android项目,并且通过Hudson构建进行管理

build.xml脚本代码如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<project name="android_target" default="package">
	<!-- 引用属性配置文件 -->
	<property file="build.properties">
	</property>
	<!-- ANT环境变量 -->
	<property environment="env" />
	<!-- 应用名称 -->
	<property name="appName" value="${ant.project.name}" />
	<!-- SDK目录(获取操作系统环境变量ANDROID_SDK_HOME的值) -->
	<property name="sdk-folder" value="${env.ANDROID_SDK_HOME}" />

	<!-- SDK指定平台目录 -->
	<property name="sdk-platform-folder" value="${sdk-folder}/platforms/android-8" />
	<!-- SDK中tools目录 -->
	<property name="sdk-tools" value="${sdk-folder}/tools" />
	<!-- SDK指定平台中tools目录 -->
	<!-- <property name="sdk-platform-tools" value="${sdk-platform-folder}/tools" /> 
	-->
	<property name="sdk-platform-tools" value="${sdk-folder}/platform-tools" />

	<!-- 使用到的命令(当前系统为windows,如果系统为linux,可将.bat文件替换成相对应的命令) -->
	<property name="aapt" value="${sdk-platform-tools}/aapt" />
	<property name="aidl" value="${sdk-platform-tools}/aidl" />
	<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" />

	<!-- 编译需要的jar; 如果项目使用到地图服务则需要maps.jar -->
	<property name="android-jar" value="${sdk-platform-folder}/android.jar" />
	<property name="android-maps-jar" value="${sdk-folder}/add-ons/addon_google_apis_google_inc_8/libs/maps.jar" />

	<!-- 编译aidl文件所需的预处理框架文件framework.aidl -->
	<property name="framework-aidl" value="${sdk-platform-folder}/framework.aidl" />

	<!-- 生成R文件的相对目录 -->
	<property name="outdir-gen" value="gen" />
	<!-- 编译后的文件放置目录 -->
	<property name="outdir-bin" value="bin" />

	<!-- 清单文件 -->
	<property name="manifest-xml" value="AndroidManifest.xml" />
	<!-- 源文件目录 -->
	<property name="resource-dir" value="res" />
	<property name="asset-dir" value="assets" />

	<!-- java源文件目录 -->
	<property name="srcdir" value="src" />
	<property name="srcdir-ospath" value="${basedir}/${srcdir}" />

	<!-- 外部类库所在目录 -->
	<property name="external-lib" value="lib" />
	<property name="external-lib-ospath" value="${basedir}/${external-lib}" />

	<!-- 生成class目录 -->
	<property name="outdir-classes" value="${outdir-bin}" />
	<property name="outdir-classes-ospath" value="${basedir}/${outdir-classes}" />

	<!-- classes.dex相关变量 -->
	<property name="dex-file" value="classes.dex" />
	<property name="dex-path" value="${outdir-bin}/${dex-file}" />
	<property name="dex-ospath" value="${basedir}/${dex-path}" />

	<!-- 经过aapt生成的资源包文件 -->
	<property name="resources-package" value="${outdir-bin}/resources.ap_" />
	<property name="resources-package-ospath" value="${basedir}/${resources-package}" />

	<!-- 未认证apk包 -->
	<property name="out-unsigned-package" value="${outdir-bin}/${appName}-unsigned.apk" />
	<property name="out-unsigned-package-ospath" value="${basedir}/${out-unsigned-package}" />

	<!-- 证书文件 -->
	<property name="keystore-file" value="${basedir}/release.keystore" />

	<!-- 已认证apk包 -->
	<property name="out-signed-package" value="${outdir-bin}/${appName}.apk" />
	<property name="out-signed-package-ospath" value="${basedir}/${out-signed-package}" />

	<!-- 初始化工作 -->
	<target name="init">
		<echo>Initializing all output directories...</echo>
		<delete dir="${outdir-bin}" />
		<delete dir="${basedir}/gen" />
		<delete file="${basedir}/resources-package" />
		<touch file="${basedir}/resources-package" />
		<mkdir dir="${outdir-bin}" />
		<mkdir dir="${outdir-classes}" />
		<mkdir dir="${basedir}/gen" />
	</target>

	<!-- 根据工程中的资源文件生成R.java文件  -->
	<target name="gen-R" depends="init">
		<echo>Generating R.java from the resources...</echo>
		<exec executable="${aapt}" failonerror="true">
			<arg value="package" />
			<arg value="-f" />
			<arg value="-m" />
			<arg value="-J" />
			<arg value="${outdir-gen}" />
			<arg value="-S" />
			<arg value="${resource-dir}" />
			<arg value="-M" />
			<arg value="${manifest-xml}" />
			<arg value="-I" />
			<arg value="${android-jar}" />
		</exec>

	</target>

	<!-- 编译aidl文件 -->
	<target name="aidl" depends="gen-R">
		<echo>Compiling .aidl into java files...</echo>
		<apply executable="${aidl}" failonerror="true">
			<!-- 指定预处理文件 -->
			<arg value="-p${framework-aidl}" />
			<!-- aidl声明的目录 -->
			<arg value="-I${srcdir}" />
			<!-- 目标文件目录 -->
			<arg value="-o${outdir-gen}" />
			<!-- 指定哪些文件需要编译 -->
			<fileset dir="${srcdir}">
				<include name="**/*.aidl" />
			</fileset>
		</apply>

	</target>

	<!-- 将工程中的java源文件编译成class文件 -->
	<target name="compile" depends="aidl">
		<echo>Compiling java source code...</echo>
		<javac encoding="utf-8" target="1.5" srcdir="." destdir="${outdir-classes}" bootclasspath="${android-jar}" includeantruntime="on">
			<classpath>
				<fileset dir="${external-lib}" includes="*.jar" />
				<!--  <filelist>
	                <file name="${android-maps-jar}"/>
	            </filelist>-->
			</classpath>
		</javac>
	</target>

	<!-- 将.class文件转化成.dex文件 -->
	<target name="dex" depends="compile">
		<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}" />
			<!-- 要生成.dex文件的源classes和libraries -->
			<arg value="${outdir-classes-ospath}" />
			<arg value="${external-lib-ospath}" />
		</exec>
	</target>

	<!-- 将资源文件放进输出目录 -->
	<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="${manifest-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>
	<!-- 打包成未签证的apk -->
	<target name="package" depends="dex, package-res-and-assets">
		<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}" />
			<!-- libs目录下的so文件
			<arg value="-rj" />
			<arg value="${external-lib-ospaths}" />
			<arg value="-nf" />
			<arg value="${external-lib-ospaths}" />
			-->
		</exec>
		<echo>It will need to be signed with jarsigner before being published.</echo>
	</target>

	<!-- 对apk进行签证 -->
	<target name="jarsigner" depends="package">
		<echo>Packaging signed apk for release...</echo>
		<exec executable="${jarsigner}" failonerror="true">
			<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="release" />
		</exec>
	</target>


	<!-- 发布 -->
	<target name="release" depends="jarsigner">
		<!-- 删除未签证apk -->
		<delete file="${out-unsigned-package-ospath}" />
		<echo>APK is released. path:${out-signed-package-ospath}</echo>
	</target>

	<!-- 调试 -->
	<target name="debug">
		<echo message="${apkbuilder}">
		</echo>
		<echo message="${out-unsigned-package-ospath}">
		</echo>
		<echo message="${resources-package-ospath}">
		</echo>
		<echo message="${dex-ospath}">
		</echo>
		<echo message="${srcdir-ospath}">
		</echo>
		<echo message="${resources-package}"></echo>
		<echo message="${dx}"></echo>
	</target>
</project>

 

 

将此脚本放在svn项目的根目录下,通过Hudson中ant的配置默认情况下回自动找到并且会自动执行这个脚本

你可能感兴趣的:(android)