Android自动打包、签名、优化、上传ANT脚本

此脚本用于进行自动编译用户需要的APK安装包,使用时候,请将签名用的*.keystore放在和build.xml同一目录,并命名为android.keystore,(如想更改,请在build.xml中的jarsignertarget进行更改)

build.properties

Java代码

android.tools=${sdk.folder}tools

android_version=2.1

apk.sdk.home=D/:/androidsdktools/android-sdk_r04-windows/android-sdk-windows/

apk.tools=${apk.sdk.home}tools/

application.package=com.test.testname

bin.dir=${jde.home}bin

app.name=NewAndroid

app.source.path=G/:/test/

jdk.home=C/:/Java_1.6_U18/jdk1.6.0_18/

output.dir=../outputDir

password=123456

sdk.folder=D/:/androidsdktools/android-sdk_r04-windows/android-sdk-windows/platforms/android-2.0.1/

temp.dir=127980239039628

[java]viewplaincopy

android.tools=${sdk.folder}tools

android_version=2.1

apk.sdk.home=D/:/androidsdktools/android-sdk_r04-windows/android-sdk-windows/

apk.tools=${apk.sdk.home}tools/

application.package=com.test.testname

bin.dir=${jde.home}bin

app.name=NewAndroid

app.source.path=G/:/test/

jdk.home=C/:/Java_1.6_U18/jdk1.6.0_18/

output.dir=../outputDir

password=123456

sdk.folder=D/:/androidsdktools/android-sdk_r04-windows/android-sdk-windows/platforms/android-2.0.1/

temp.dir=127980239039628

Build.xml

Xml代码

<?xmlversion="1.0"encoding="UTF-8"?>

<projectname="Test"default="debug"basedir=".">

<propertyfile="build.properties">

</property>

<!--Theintermediatesdirectory-->

<!--Eclipseuses"bin"foritsownoutput,sowedothesame.-->

<propertyname="outdir"value="bin"/>

<!--*************************************************************************************-->

<!--Nouserservicablepartsbelow.-->

<propertyname="android-framework"value="${sdk.folder}/framework.aidl"/>

<!--Inputdirectories-->

<propertyname="resource-dir"value="res"/>

<propertyname="asset-dir"value="assets"/>

<propertyname="srcdir"value="src"/>

<conditionproperty="srcdir-ospath"value="${basedir}/${srcdir}"else="${basedir}/${srcdir}">

<osfamily="windows"/>

</condition>

<propertyname="external-libs"value="libs"/>

<conditionproperty="external-libs-ospath"value="${basedir}/${external-libs}"else="${basedir}/${external-libs}">

<osfamily="windows"/>

</condition>

<!--Outputdirectories-->

<propertyname="outdir-classes"value="${outdir}/classes"/>

<conditionproperty="outdir-classes-ospath"value="${basedir}/${outdir-classes}"else="${basedir}/${outdir-classes}">

<osfamily="windows"/>

</condition>

<conditionproperty="zipalign-package-ospath"value="${basedir}/${output.dir}/${app.name}_for_android_${android_version}_${temp.dir}.apk"else="${basedir}/${output.dir}">

<osfamily="windows"/>

</condition>

<!--CreateR.javainthesourcedirectory-->

<propertyname="outdir-r"value="src"/>

<!--Intermediatefiles-->

<propertyname="dex-file"value="classes.dex"/>

<propertyname="intermediate-dex"value="${outdir}/${dex-file}"/>

<conditionproperty="intermediate-dex-ospath"value="${basedir}/${intermediate-dex}"else="${basedir}/${intermediate-dex}">

<osfamily="windows"/>

</condition>

<!--Thefinalpackagefiletogenerate-->

<propertyname="resources-package"value="${outdir}/${ant.project.name}.ap_"/>

<conditionproperty="resources-package-ospath"value="${basedir}/${resources-package}"else="${basedir}/${resources-package}">

<osfamily="windows"/>

</condition>

<propertyname="out-debug-package"value="${outdir}/${ant.project.name}-debug.apk"/>

<conditionproperty="out-debug-package-ospath"value="${basedir}/${out-debug-package}"else="${basedir}/${out-debug-package}">

<osfamily="windows"/>

</condition>

<propertyname="out-unsigned-package"value="${outdir}/${ant.project.name}-unsigned.apk"/>

<propertyname="out-signed-package"value="${outdir}/${ant.project.name}-signed.apk"/>

<conditionproperty="out-unsigned-package-ospath"value="${basedir}/${out-unsigned-package}"else="${basedir}/${out-unsigned-package}">

<osfamily="windows"/>

</condition>

<conditionproperty="out-signed-package-ospath"value="${basedir}/${out-signed-package}"else="${basedir}/${out-signed-package}">

<osfamily="windows"/>

</condition>

<!--Tools-->

<conditionproperty="aapt"value="${android.tools}/aapt.exe"else="${android.tools}/aapt">

<osfamily="windows"/>

</condition>

<conditionproperty="zipalign"value="${apk.tools}/zipalign.exe"else="${apk.tools}/zipalign">

<osfamily="windows"/>

</condition>

<conditionproperty="jarsigner"value="${jdk.home}/bin/jarsigner.exe"else="${jdk.home}/bin/jarsigner">

<osfamily="windows"/>

</condition>

<conditionproperty="aidl"value="${android.tools}/aidl.exe"else="${android.tools}/aidl">

<osfamily="windows"/>

</condition>

<conditionproperty="adb"value="${apk.tools}/adb.exe"else="${apk.tools}/adb">

<osfamily="windows"/>

</condition>

<conditionproperty="dx"value="${android.tools}/dx.bat"else="${android.tools}/dx">

<osfamily="windows"/>

</condition>

<conditionproperty="apk-builder"value="${apk.tools}/apkbuilder.bat"else="${apk.tools}/apkbuilder">

<osfamily="windows"/>

</condition>

<propertyname="android-jar"value="${sdk.folder}/android.jar"/>

<!--Rules-->

<!--Createtheoutputdirectoriesiftheydon'texistyet.-->

<targetname="dirs"depends="init">

<echo>Creatingoutputdirectoriesifneeded...</echo>

<mkdirdir="${outdir}"/>

<mkdirdir="${outdir-classes}"/>

</target>

<!--GeneratetheR.javafileforthisproject'sresources.-->

<targetname="resource-src"depends="dirs">

<echo>GeneratingR.java/Manifest.javafromtheresources...</echo>

<execexecutable="${aapt}"failonerror="true">

<argvalue="package"/>

<argvalue="-m"/>

<argvalue="-J"/>

<argvalue="${outdir-r}"/>

<argvalue="-M"/>

<argvalue="AndroidManifest.xml"/>

<argvalue="-S"/>

<argvalue="${resource-dir}"/>

<argvalue="-I"/>

<argvalue="${android-jar}"/>

</exec>

</target>

<!--Generatejavaclassesfrom.aidlfiles.-->

<targetname="aidl"depends="dirs">

<echo>CompilingaidlfilesintoJavaclasses...</echo>

<applyexecutable="${aidl}"failonerror="true">

<argvalue="-p${android-framework}"/>

<argvalue="-I${srcdir}"/>

<filesetdir="${srcdir}">

<includename="**/*.aidl"/>

</fileset>

</apply>

</target>

<!--Compilethisproject's.javafilesinto.classfiles.-->

<targetname="compile"depends="dirs,resource-src,aidl">

<javacencoding="ascii"target="1.5"debug="true"extdirs=""srcdir="."destdir="${outdir-classes}"bootclasspath="${android-jar}">

<classpath>

<filesetdir="${external-libs}"includes="*.jar"/>

</classpath>

</javac>

</target>

<!--Convertthisproject's.classfilesinto.dexfiles.-->

<targetname="dex"depends="compile">

<echo>Convertingcompiledfilesandexternallibrariesinto${outdir}/${dex-file}...</echo>

<applyexecutable="${dx}"failonerror="true"parallel="true">

<argvalue="--dex"/>

<scriptsrc="/javascripts/tinymce/themes/advanced/langs/zh.js"type="text/javascript"><!--mce:0--></script><scriptsrc="/javascripts/tinymce/plugins/javaeye/langs/zh.js"type="text/javascript"><!--mce:1--></script><argvalue="--output=${intermediate-dex-ospath}"/>

<argpath="${outdir-classes-ospath}"/>

<filesetdir="${external-libs}"includes="*.jar"/>

</apply>

</target>

<!--Puttheproject'sresourcesintotheoutputpackagefile.-->

<targetname="package-res-and-assets">

<echo>Packagingresourcesandassets...</echo>

<execexecutable="${aapt}"failonerror="true">

<argvalue="package"/>

<argvalue="-f"/>

<argvalue="-M"/>

<argvalue="AndroidManifest.xml"/>

<argvalue="-S"/>

<argvalue="${resource-dir}"/>

<argvalue="-A"/>

<argvalue="${asset-dir}"/>

<argvalue="-I"/>

<argvalue="${android-jar}"/>

<argvalue="-F"/>

<argvalue="${resources-package}"/>

</exec>

</target>

<!--Sameaspackage-res-and-assets,butwithout"-A${asset-dir}"-->

<targetname="package-res-no-assets">

<echo>Packagingresources...</echo>

<execexecutable="${aapt}"failonerror="true">

<argvalue="package"/>

<argvalue="-f"/>

<argvalue="-M"/>

<argvalue="AndroidManifest.xml"/>

<argvalue="-S"/>

<argvalue="${resource-dir}"/>

<!--Noassetsdirectory-->

<argvalue="-I"/>

<argvalue="${android-jar}"/>

<argvalue="-F"/>

<argvalue="${resources-package}"/>

</exec>

</target>

<!--Invokethepropertargetdependingonwhetherornot

anassetsdirectoryispresent.-->

<!--TODO:findanicerwaytoincludethe"-A${asset-dir}"argument

onlywhentheassetsdirexists.-->

<targetname="package-res">

<availablefile="${asset-dir}"type="dir"property="res-target"value="and-assets"/>

<propertyname="res-target"value="no-assets"/>

<antcalltarget="package-res-${res-target}"/>

</target>

<!--Packagetheapplicationandsignitwithadebugkey.

Thisisthedefaulttargetwhenbuilding.Itisusedfordebug.-->

<targetname="debug"depends="dex,package-res">

<echo>Packaging${out-debug-package},andsigningitwithadebugkey...</echo>

<execexecutable="${apk-builder}"failonerror="true">

<argvalue="${out-debug-package-ospath}"/>

<argvalue="-z"/>

<argvalue="${resources-package-ospath}"/>

<argvalue="-f"/>

<argvalue="${intermediate-dex-ospath}"/>

<argvalue="-rf"/>

<argvalue="${srcdir-ospath}"/>

<argvalue="-rj"/>

<argvalue="${external-libs-ospath}"/>

</exec>

</target>

<!--Packagetheapplicationwithoutsigningit.

Thisallowsfortheapplicationtobesignedlaterwithanofficialpublishingkey.-->

<targetname="release"depends="dex,package-res">

<echo>Packaging${out-unsigned-package}forrelease...</echo>

<execexecutable="${apk-builder}"failonerror="true">

<argvalue="${out-unsigned-package-ospath}"/>

<argvalue="-u"/>

<argvalue="-z"/>

<argvalue="${resources-package-ospath}"/>

<argvalue="-f"/>

<argvalue="${intermediate-dex-ospath}"/>

<argvalue="-rf"/>

<argvalue="${srcdir-ospath}"/>

<argvalue="-rj"/>

<argvalue="${external-libs-ospath}"/>

</exec>

<echo>Itwillneedtobesignedwithjarsignerbeforebeingpublished.</echo>

</target>

<!--Installthepackageonthedefaultemulator-->

<targetname="install"depends="debug">

<echo>Installing${out-debug-package}ontodefaultemulator...</echo>

<execexecutable="${adb}"failonerror="true">

<argvalue="install"/>

<argvalue="${out-debug-package}"/>

</exec>

</target>

<targetname="reinstall"depends="debug">

<echo>Installing${out-debug-package}ontodefaultemulator...</echo>

<execexecutable="${adb}"failonerror="true">

<argvalue="install"/>

<argvalue="-r"/>

<argvalue="${out-debug-package}"/>

</exec>

</target>

<!--Uinstallthepackagefromthedefaultemulator-->

<targetname="uninstall">

<echo>Uninstalling${application.package}fromthedefaultemulator...</echo>

<execexecutable="${adb}"failonerror="true">

<argvalue="uninstall"/>

<argvalue="${application.package}"/>

</exec>

</target>

<!--初始化目录-->

<targetname="init"depends="Copy_Ressource">

<echomessage="Initoutputdirectory.....">

</echo>

<mkdirdir="${output.dir}"/>

</target>

<!--拷贝资源-->

<targetname="Copy_Ressource">

<echomessage="Copyappresource.">

</echo>

<copytodir="${asset-dir}"overwrite="true"failonerror="false">

<filesetdir="${app.source.path}">

<includename="*.*"/>

</fileset>

</copy>

</target>

<!--进行签名-->

<targetname="jarsigner"depends="release">

<execexecutable="${jarsigner}"failonerror="true">

<argvalue="-verbose"/>

<argvalue="-storepass"/>

<argvalue="${password}"/>

<argvalue="-keystore"/>

<argvalue="android.keystore"/>

<argvalue="-signedjar"/>

<argvalue="${out-signed-package-ospath}"/>

<argvalue="${out-unsigned-package-ospath}"/>

<argvalue="android.keystore"/>

</exec>

</target>

<!--进行优化-->

<targetname="zipalign"depends="jarsigner">

<execexecutable="${zipalign}"failonerror="true">

<argvalue="-v"/>

<argvalue="-f"/>

<argvalue="4"/>

<argvalue="${out-signed-package-ospath}"/>

<argvalue="${zipalign-package-ospath}"/>

</exec>

</target>

<!--直接上传到手机中去-->

<targetname="adb"depends="zipalign">

<execexecutable="${adb}"failonerror="true">

<argvalue="install"/>

<argvalue="-r"/>

<argvalue="${zipalign-package-ospath}"/>

</exec>

</target>

</project>

如果keystore不在此目录,

你就修改:

<targetname="jarsigner"depends="release">

<execexecutable="${jarsigner}"failonerror="true">

<argvalue="-verbose"/>

<argvalue="-storepass"/>

<argvalue="${password}"/>

<argvalue="-keystore"/>

<argvalue="android.keystore"/>

<argvalue="-signedjar"/>

<argvalue="${out-signed-package-ospath}"/>

<argvalue="${out-unsigned-package-ospath}"/>

<argvalue="android"/>

</exec>

</target>

中的

<argvalue="android.keystore"/>

<argvalue="android"/>

路径,

这个两个参数值要一样即可

相关推荐