Eclipse下配置Ant脚本并自动打包带签名的Android apk
eclipse虽然用的比较少,但是在一些古老的项目上还会用到滴,其中最麻烦的事情就是打带签名包的问题,不太方便,下面通过本文给大家记录下配置ant,自动打包带签名apk的过程,作为备份。(PC环境为MAC)
1,第一步得安ant,下载对应安装包,解压后配置环境变量:
exportANT_HOME="/Users/yanzi/work/apache-ant-1.9.4"
exportPATH=${PATH}:${ANT_HOME}/bin
通过whichant检查是否安装成功。
2,在项目目录下运行:androidupdateproject-p./如果有依赖包,就到每个依赖包目录下再运行下这个命令。运行完这个命令后会多两个文件:build.xml和local.properties
local.properties文件内容:
sdk.dir=/Users/yanzi/work/android-tool/android-sdk-macosx
build.xml文件内容:
<?xmlversion="1.0"encoding="UTF-8"?>
<projectname="TongCheng"default="help">
<!--Thelocal.propertiesfileiscreatedandupdatedbythe'android'tool.
ItcontainsthepathtotheSDK.Itshould*NOT*becheckedinto
VersionControlSystems.-->
<propertyfile="local.properties"/>
<!--Theant.propertiesfilecanbecreatedbyyou.Itisonlyeditedbythe
'android'tooltoaddpropertiestoit.
ThisistheplacetochangesomeAntspecificbuildproperties.
Herearesomepropertiesyoumaywanttochange/update:
source.dir
Thenameofthesourcedirectory.Defaultis'src'.
out.dir
Thenameoftheoutputdirectory.Defaultis'bin'.
Forotheroverridableproperties,lookatthebeginningoftherules
filesintheSDK,attools/ant/build.xml
PropertiesrelatedtotheSDKlocationortheprojecttargetshould
beupdatedusingthe'android'toolwiththe'update'action.
Thisfileisanintegralpartofthebuildsystemforyour
applicationandshouldbecheckedintoVersionControlSystems.
-->
<propertyfile="ant.properties"/>
<!--ifsdk.dirwasnotsetfromoneofthepropertyfile,then
getitfromtheANDROID_HOMEenvvar.
Thismustbedonebeforeweloadproject.propertiessince
theproguardconfigcanusesdk.dir-->
<propertyenvironment="env"/>
<conditionproperty="sdk.dir"value="${env.ANDROID_HOME}">
<issetproperty="env.ANDROID_HOME"/>
</condition>
<!--Theproject.propertiesfileiscreatedandupdatedbythe'android'
tool,aswellasADT.
Thiscontainsprojectspecificpropertiessuchasprojecttarget,andlibrary
dependencies.Lowerlevelbuildpropertiesarestoredinant.properties
(orin.classpathforEclipseprojects).
Thisfileisanintegralpartofthebuildsystemforyour
applicationandshouldbecheckedintoVersionControlSystems.-->
<loadpropertiessrcFile="project.properties"/>
<!--quickcheckonsdk.dir-->
<fail
message="sdk.dirismissing.Makesuretogeneratelocal.propertiesusing'androidupdateproject'ortoinjectitthroughtheANDROID_HOMEenvironmentvariable."
unless="sdk.dir"
/>
<!--
Importperprojectcustombuildrulesifpresentattherootoftheproject.
Thisistheplacetoputcustomintermediarytargetssuchas:
-pre-build
-pre-compile
-post-compile(Thisistypicallyusedforcodeobfuscation.
Compiledcodelocation:${out.classes.absolute.dir}
Ifthisisnotdoneinplace,override${out.dex.input.absolute.dir})
-post-package
-post-build
-pre-clean
-->
<importfile="custom_rules.xml"optional="true"/>
<!--Importtheactualbuildfile.
Tocustomizeexistingtargets,therearetwooptions:
-Customizeonlyonetarget:
-copy/pastethetargetintothisfile,*before*the
<import>task.
-customizeittoyourneeds.
-Customizethewholecontentofbuild.xml
-copy/pastethecontentoftherulesfiles(minusthetopnode)
intothisfile,replacingthe<import>task.
-customizetoyourneeds.
***********************
******IMPORTANT******
***********************
Inallcasesyoumustupdatethevalueofversion-tagbelowtoread'custom'insteadofaninteger,
inordertoavoidhavingyourfilebeoverriddenbytoolssuchas"androidupdateproject"
-->
<!--version-tag:1-->
<importfile="${sdk.dir}/tools/ant/build.xml"/>
</project>
这两个文件都不用动,最上面project的name里写上自己想要的名字。然后项目根目录下新建个ant.properties,内容如下:
#ThisfileisusedtooverridedefaultvaluesusedbytheAntbuildsystem. # #ThisfilemustbecheckedinVersionControlSystems,asitis #integraltothebuildsystemofyourproject. #ThisfileisonlyusedbytheAntscript. #Youcanusethistooverridedefaultvaluessuchas #'source.dir'forthelocationofyourjavasourcefolderand #'out.dir'forthelocationofyouroutputfolder. #Youcanalsouseitdefinehowthereleasebuildsaresignedbydeclaring #thefollowingproperties: #'key.store'forthelocationofyourkeystoreand #'key.alias'forthenameofthekeytouse. #Thepasswordwillbeaskedduringthebuildwhenyouusethe'release'target. key.alias=com.tongcheng key.store=tongcheng.keystore key.store.password=12377778 key.alias.password=12377778
配置上keystore的相关信息。
这样先执行antclean,然后执行antrelease就ok了!
3,当然为了更爽,你还需要下面这个脚本:
vimrelease.sh,内容如下:
echo-----------------start---------
start=$(date+%s)
antclean
antrelease
adbuninstallcom.android.tongcheng
adbinstallbin/TongCheng-release.apk
end=$(date+%s)
echo"----over,耗时$[${end}-${start}]秒---"
之后每次想安装带签名的包,直接./release.sh即可!
以上所述是小编给大家介绍的Eclipse下配置Ant脚本并自动打包带签名的Androidapk的相关内容,希望对大家有所帮助!