1.先配置文件目录:<!-- 配置文件 local.properies -->
sdk.dir=D:\\android-sdk\\android-sdk
project.sdkenv=D:\\android-sdk\\android-sdk
project.antenv=C:\\ant_1.9.3\\lib\\ant-contrib-1.0b3.jar
#project name and version<!-- 工程名和版本-->
project.name=AndroidFly
project.version=V1.00.10
#keysore file <!-- 工程签名 -->
keystore.file=platform.keystore
key.alias=android
key.alias.password=android
key.store.password=android
#library project<!-- 工程引用 -->
library-dir=D:\\androidPro\\Core
library-dir2=D:\\androidPro\\Mavlink
2.编写build.xml
<!-- build.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project name="com.autel.maxifly" default="help" basedir=".">
<property file="local.properties" />
<!-- 环境变量-->
<property environment="env" />
<!-- 应用名称 -->
<property name="appName" value="${project.name}" />
<!-- 应用 版本号 -->
<property name="appVersions" value="${project.version}" />
<!-- SDK目录 -->
<property name="sdk-folder" value="${project.sdkenv}" />
<!-- SDK指定平台目录 -->
<property name="sdk-platform-folder" value="${sdk-
folder}/platforms/android-19" />
<!-- SDK中tools目录 -->
<property name="sdk-tools" value="${sdk-folder}/tools" />
<!-- SDK指定平台中tools目录 -->
<property name="sdk-platform-tools" value="${sdk-
folder}/build-tools/19.0.1" />
<!-- SDK命令 -->
<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" />
<property name="zipalign" value="${sdk-tools}/zipalign.exe" />
<property name="proguard-home" value="${sdk-
tools}/proguard/lib/proguard.jar"/>
<!-- 编译需要的jar -->
<property name="android-jar" value="${sdk-platform-
folder}/android.jar" />
<!--项目使用到地图服务则需要maps.jar -->
<property name="android-maps-jar" value="${sdk-folder}/add-
ons/addon-google_apis-google-19/libs/maps.jar" />
<!-- 编译aidl文件所需的预处理框架文件framework.aidl -->
<property name="framework-aidl" value="${sdk-platform-
folder}/framework.aidl" />
<!-- 清单文件 -->
<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="libs" />
<property name="external-lib-ospath" value="${basedir}/
${external-lib}" />
<property name="external-library-dir-lib-ospath"
value="${library-dir}/${external-lib}" />
<property name="external-library-dir2-lib-ospath"
value="${library-dir2}/${external-lib}" />
<!-- 使用第三方的ant包,使ant支持for循环-->
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${project.antenv}" />
</classpath>
</taskdef>
<!-- 初始化工作 -->
<target name="init">
<echo>目录初始化....</echo>
<var name="out" value="out"/>
<!-- 生成R文件的相对目录 -->
<var name="outdir-gen" value="gen" />
<!-- 编译后的文件放置目录 -->
<var name="outdir-bin" value="bin-new" />
<!-- 生成class目录 -->
<var name="outdir-classes" value="${outdir-
bin}/classes" />
<var name="outdir-classes-ospath" value="${basedir}/
${outdir-classes}" />
<!-- classes.dex相关变量 -->
<var name="dex-file" value="classes.dex" />
<var name="dex-path" value="${outdir-bin}/${dex-file}"
/>
<var name="dex-ospath" value="${basedir}/${dex-path}"
/>
<!-- 经过aapt生成的资源包文件 -->
<var name="resources-package" value="${outdir-
bin}/resources.ap_" />
<var name="resources-package-ospath"
value="${basedir}/${resources-package}" />
<!-- 未认证apk包 -->
<var name="out-unsigned-package" value="${outdir-
bin}/${appName}_${appVersions}_unsigned.apk" />
<var name="out-unsigned-package-ospath"
value="${basedir}/${out-unsigned-package}" />
<!-- 已认证apk包 -->
<var name="out-signed-package" value="${outdir-bin}/
${appName}_${appVersions}_signed.apk" />
<var name="out-signed-package-ospath"
value="${basedir}/${out-signed-package}" />
<!-- 已优化apk包 -->
<var name="zipalign-signed-package" value="${outdir-bin}/
${appName}_${appVersions}_zipalign.apk" />
<delete dir="${outdir-bin}" />
<mkdir dir="${outdir-bin}" />
<mkdir dir="${outdir-classes}" />
</target>
<!-- 根据工程中的资源文件生成R.java文件 -->
<target name="gen-R" depends="init">
<echo>生成R.java文件....</echo>
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-f" />
<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="-I" />
<arg value="${android-jar}" />
</exec>
</target>
<!-- 编译aidl文件 -->
<target name="aidl" depends="gen-R">
<echo>编译aidl文件....</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>正在编译Java源文件...</echo>
<!-- mavlink编译class -->
<javac encoding="UTF-8" destdir="${outdir-classes}"
bootclasspath="${android-jar}">
<src path="${library-dir2}/src" />
<classpath>
<fileset dir="${external-library-dir2-lib-ospath}"
includes="*.jar" />
</classpath>
</javac>
<!-- core编译class-->
<javac encoding="UTF-8" destdir="${outdir-classes}"
bootclasspath="${android-jar}">
<src path="${library-dir}/src" />
<classpath>
<fileset dir="${external-library-dir-lib-ospath}"
includes="*.jar" />
</classpath>
</javac>
<!-- 主类编译class -->
<javac encoding="UTF-8" target="1.7"
srcdir="${basedir}" includeAntRuntime="false" destdir="${outdir-
classes}" bootclasspath="${android-jar}" verbose="false" debug="on">
<classpath>
<fileset dir="${external-lib}"
includes="*.jar" />
<fileset dir="${external-library-dir-
lib-ospath}" includes="*.jar" />
<fileset dir="${external-library-dir2
-lib-ospath}" includes="*.jar" />
<filelist>
<file name="${android-maps-jar}"/>
</filelist>
</classpath>
</javac>
</target>
<!-- 进行class混淆 后期添加 -->
<!-- <target name="obfuscate" depends="compile">
<echo>正在进行class混淆。。。</echo>
<jar basedir="${outdir-classes}" destfile="${outdir-
bin}/temp.jar" />
<java jar="${android.proguard}" fork="true"
failonerror="true" >
<jvmarg value="-Dmaximum.inlined.code.length=16" />
<arg value="@${basedir}/proguard.cfg" />
<arg value="-injars ${outdir-bin}/temp.jar" />
<arg value="-outjars ./dist/obfuscated.jar " />
<arg value="-libraryjars ${android-jar}" />
<arg value="-libraryjars
${library_annotation}/annotations.jar"/>第三方jar包需要引用,用于辅助
编译
</java>
<delete dir="${outdir-classes}"/>
<mkdir dir="${outdir-classes}" />
<unzip src="./dist/obfuscated.jar" dest="${outdir-
classes}"/>
<delete dir="./dist"/>
<delete file="${outdir-bin}/temp.jar"/>
</target> -->
<!-- 将.class文件转化成.dex文件 -->
<target name="dex" depends="compile">
<echo>正在将.class文件转化成.dex文件...</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>正在打包资源文件...</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>正在生成未签名apk...</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="-rj" />
<arg value="${external-lib-ospath}"/>
<!-- 将so文件打包进去 -->
<arg value="-nf" />
<arg value="${external-lib-ospath}" />
</exec>
</target>
<!-- 对apk进行签名 -->
<target name="jarsigner" depends="package">
<echo>正在为apk文件签名...(默认为开发模式下签名文件)
</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="${out-signed-package-ospath}" />
<arg value="${out-unsigned-package-ospath}" />
<!-- 证书的别名 -->
<arg value="${key.alias}" />
</exec>
<echo>签名完成</echo>
<!-- 删除未签证apk -->
<delete file="${out-unsigned-package-ospath}" />
</target>
<!-- 打包的应用程序进行优化 -->
<target name="zipalign" depends="jarsigner">
<echo>正在优化apk... </echo>
<exec executable="${zipalign}" failonerror="true">
<arg value="-v" />
<arg value="4" />
<arg value="${out-signed-package-ospath}" />
<arg value="${zipalign-signed-package}" />
</exec>
<echo>优化完成</echo>
<!-- 删除未优化apk -->
<delete file="${out-signed-package-ospath}" />
</target>
<!-- 发布 -->
<target name="release" depends="zipalign">
<echo>APK打包完成,apk对应的路径为:${zipalign-signed-
package}</echo>
</target>
</project>
3.编写批处理文件
cmd -> ant realease 就可以了
已有 0 人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐