用命令行编译APK(英文版)

zyygive 2012-08-17

HowtobuildAndroidapplicationpackage(.apk)fromthecommandlineusingtheSDKtools

Helloallandroiddevelopers,IjustwanttosharemyexperiencebuildingandroidapkmanuallyusingsdktoolswithoutusingEclipse.MyoriginalgoalismotivatedfirstlybythedesiretoincorporatecontinuousintegrationaspecttoAndroiddevelopmentprocessandsecondlytoditchtheADTeclipsepluginsincethepluginauto-buildprocessblocksEclipseUIifyouhavelargeresources,assetsinyourAndroidproject,andaslowcomputerlikemine.IamusingCruiseControlasmycontinuousintegrationtool.

Belowisoneofthemanyapkbuildprocesses:

Buildprocess

Thegoodthingaboutbuildingmanuallyyourapkisthatyoudon’thavetonameyourresourcesdirectorytores,youcannameitanythingyouwant.

Youcanfindantscriptsin:<SDK_HOME>\platforms\android-1.5\templates\android-rules.xml

Step1:GenerateResourcejavacodeandpackagedResources

aaptpackage-f-M${manifest.file}-F${packaged.resource.file}-I${path.to.android-jar.library}-S${android-resource-directory}[-m-J${folder.to.output.the.R.java}]

Step2:Compilejavasourcecodes+R.java

usejavac

Step3:ConvertclassestoDalvikbytecodes

usedx.bat

dx.bat–dex–output=${output.dex.file}${compiled.classes.directory}${jarfiles..}

Step4:CreateunsignedAPK

useapkbuilder

apkbuilder${output.apk.file}-u-z${packagedresource.file}-f${dex.file}

or

apkbuilder${output.apk.file}-u-z${packagedresource.file}-f${dex.file}-rf${source.dir}-rj${libraries.dir}

-rf=resourcesrequiredforcompiledsourcefiles?

-rj=resourcesrequiredforjarfiles

Step6:Generateakey

usekeytool

Step7:SignAPK

usejarsigner

jarsigner-keystore${keystore}-storepass${keystore.password}-keypass${keypass}-signedjar${signed.apkfile}${unsigned.apkfile}${keyalias}

Step8:Publish

useadb

adb-dinstall-r${signed.apk}

InspectingyourAPKfile:

aaptlist-vlatest.apk

Openquestions:

1.Canyouincludemorethanonedexfileintheapk?

2.Canyouhavedexfilenamedotherthanclasses.dexintheapk?

3.Doesanapkhavetohaveapackagedresource?

Note:IfuponinstallingyourappusingadbyouseethiserrorcodeFAILED_INSTALL_DEXOPTthenmostlikelythateitheryoudon’thaveclasses.dexoryoudon’thaveapackagedresourceintheapk

相关推荐