程序员人生 网站导航

Ant管理安卓项目的自动化打包命令

栏目:综合技术时间:2015-07-27 08:20:28

Ant管理安卓项目的自动化打包命令

1、配置ant

1、登陆ant下载网页:http://ant.apache.org/bindownload.cgi,点击红框内容进行ant下载。


2、把ant下载包解压放进指定目录下。

3、配置ant的系统环境,以下


4、在dos环境中输入ant -version 测试ant环境配置是不是成功。


2、使用ant命令给安卓项陌生成build.xml和local.properties配置文件

1、在dos环境下输入命令:

android update project --name <project_name> --target <target_ID>--path <path_to_your_project>

注意:target可以通过命令android list target查看target的id

例如:


安卓项目中生成了build.xml和local.properties配置文件


3、使用Ant命令进行生成测试版apk和未签名的apk

编译打包项目:在已生成了的build.xml和local.properties配置文件的项目目录下用dos输入以下指令

ant debug:生成1个测试版apk,默许使用debug key进行签名,生成的apk(your_project_name-debug.apk)在项目的bin目录下。 ant release:生成1个未签名和未aligned的apk包,project_name-release-unsigned.ap和project_name-release-unaligned.apk在项目的bin目录下。

例如:

ant debug命令

D:Faith_yee's Workplacehehenian-mobile>android update project --name hehenian- mobile --target android⑴8 --path "D:Faith_yee's Workplacehehenian-mobile" Updated project.properties Updated local.properties Updated file D:Faith_yee's Workplacehehenian-mobileuild.xml Updated file D:Faith_yee's Workplacehehenian-mobileproguard-project.txt It seems that there are sub-projects. If you want to update them please use the --subprojects parameter. D:Faith_yee's Workplacehehenian-mobile>ant debug Buildfile: D:Faith_yee's Workplacehehenian-mobileuild.xml -set-mode-check: -set-debug-files: -check-env: [checkenv] Android SDK Tools Revision 21.1.0 [checkenv] Installed at D:adt-bundle-windows-x86_64_20140101sdk -setup: [echo] Project Name: hehenian-mobile [gettype] Project Type: Application -set-debug-mode: -debug-obfuscation-check: -pre-build: -build-setup: [echo] Resolving Build Target for hehenian-mobile... [gettarget] Project Target: Android 4.3 [gettarget] API level: 18 [echo] ---------- [echo] Creating output directories if needed... [echo] ---------- [echo] Resolving Dependencies for hehenian-mobile... [dependency] Library dependencies: [dependency] No Libraries [echo] ---------- [echo] Building Libraries with 'debug'... [subant] No sub-builds to iterate on -code-gen: [mergemanifest] Found Deleted Target File [mergemanifest] Merging AndroidManifest files into one. [mergemanifest] Manifest merger disabled. Using project manifest only. [echo] Handling aidl files... [aidl] No AIDL files to compile. [echo] ---------- [echo] Handling RenderScript files... [renderscript] No RenderScript files to compile. [echo] ---------- [echo] Handling Resources... [aapt] Found Deleted Target File [aapt] Generating resource IDs... [echo] ---------- [echo] Handling BuildConfig class... [buildconfig] No need to generate new BuildConfig. -pre-compile: -compile: [javac] Compiling 1 source file to D:Faith_yee's Workplacehehenian-mobile binclasses -post-compile: -obfuscate: -dex: [dex] input: D:Faith_yee's Workplacehehenian-mobileinclasses [dex] input: D:Faith_yee's Workplacehehenian-mobilelibsandroid-support -v4.jar [dex] Using Pre-Dexed android-support-v4-017fd1f299aa33ccf51747cafa4120bf. jar <- D:Faith_yee's Workplacehehenian-mobilelibsandroid-support-v4.jar [dex] Found Deleted Target File [dex] Converting compiled files and external libraries into D:Faith_yee's Workplacehehenian-mobileinclasses.dex... [dx] Merged dex A (28 defs/38.5KiB) with dex B (309 defs/349.3KiB). Resul t is 337 defs/470.8KiB. Took 0.2s -crunch: [crunch] Crunching PNG Files in source dir: D:Faith_yee's Workplacehehenian -mobile es [crunch] To destination dir: D:Faith_yee's Workplacehehenian-mobilein es [crunch] Processing image to cache: D:Faith_yee's Workplacehehenian-mobile resdrawable-hdpihehenianlogo_new.png => D:Faith_yee's Workplacehehenian-mobi lein esdrawable-hdpihehenianlogo_new.png [crunch] Crunched 1 PNG files to update cache [crunch] libpng error: Not a PNG file -package-resources: [aapt] Found Deleted Target File [aapt] Creating full resource package... -package: [apkbuilder] Found Deleted Target File [apkbuilder] Creating hehenian-mobile-debug-unaligned.apk and signing it with a debug key... -post-package: -do-debug: [zipalign] Running zip align on final apk... [echo] Debug Package: D:Faith_yee's Workplacehehenian-mobileinhehenian -mobile-debug.apk [propertyfile] Updating property file: D:Faith_yee's Workplacehehenian-mobile binuild.prop [propertyfile] Updating property file: D:Faith_yee's Workplacehehenian-mobile binuild.prop [propertyfile] Updating property file: D:Faith_yee's Workplacehehenian-mobile binuild.prop [propertyfile] Updating property file: D:Faith_yee's Workplacehehenian-mobile binuild.prop -post-build: debug: BUILD SUCCESSFUL Total time: 4 seconds D:Faith_yee's Workplacehehenian-mobile>


ant release命令

D:Faith_yee's Workplacehehenian-mobile>ant release Buildfile: D:Faith_yee's Workplacehehenian-mobileuild.xml -set-mode-check: -set-release-mode: -release-obfuscation-check: [echo] proguard.config is ${proguard.config} -pre-build: -check-env: [checkenv] Android SDK Tools Revision 21.1.0 [checkenv] Installed at D:adt-bundle-windows-x86_64_20140101sdk -setup: [echo] Project Name: hehenian-mobile [gettype] Project Type: Application -build-setup: [echo] Switching between debug and non debug build: Deleting previous compi lation output... [delete] Deleting directory D:Faith_yee's Workplacehehenian-mobileinclas ses [echo] Resolving Build Target for hehenian-mobile... [gettarget] Project Target: Android 4.3 [gettarget] API level: 18 [echo] ---------- [echo] Creating output directories if needed... [mkdir] Created dir: D:Faith_yee's Workplacehehenian-mobileinclasses [echo] ---------- [echo] Resolving Dependencies for hehenian-mobile... [dependency] Library dependencies: [dependency] No Libraries [echo] ---------- [echo] Building Libraries with 'release'... [subant] No sub-builds to iterate on -code-gen: [mergemanifest] Found Deleted Target File [mergemanifest] Merging AndroidManifest files into one. [mergemanifest] Manifest merger disabled. Using project manifest only. [echo] Handling aidl files... [aidl] No AIDL files to compile. [echo] ---------- [echo] Handling RenderScript files... [renderscript] No RenderScript files to compile. [echo] ---------- [echo] Handling Resources... [aapt] Found Deleted Target File [aapt] Generating resource IDs... [echo] ---------- [echo] Handling BuildConfig class... [buildconfig] Build type changed: Generating new BuildConfig class. -pre-compile: -compile: [javac] Compiling 12 source files to D:Faith_yee's Workplacehehenian-mobil einclasses -post-compile: -obfuscate: -dex: [dex] input: D:Faith_yee's Workplacehehenian-mobileinclasses [dex] input: D:Faith_yee's Workplacehehenian-mobilelibsandroid-support -v4.jar [dex] Using Pre-Dexed android-support-v4-017fd1f299aa33ccf51747cafa4120bf. jar <- D:Faith_yee's Workplacehehenian-mobilelibsandroid-support-v4.jar [dex] Found Deleted Target File [dex] Converting compiled files and external libraries into D:Faith_yee's Workplacehehenian-mobileinclasses.dex... [dx] Merged dex A (28 defs/38.5KiB) with dex B (309 defs/349.3KiB). Resul t is 337 defs/470.8KiB. Took 0.2s -crunch: [crunch] Crunching PNG Files in source dir: D:Faith_yee's Workplacehehenian -mobile es [crunch] To destination dir: D:Faith_yee's Workplacehehenian-mobilein es [crunch] Processing image to cache: D:Faith_yee's Workplacehehenian-mobile resdrawable-hdpihehenianlogo_new.png => D:Faith_yee's Workplacehehenian-mobi lein esdrawable-hdpihehenianlogo_new.png [crunch] Crunched 1 PNG files to update cache [crunch] libpng error: Not a PNG file -package-resources: [aapt] Current build type is different than previous build: forced aapt run . [aapt] Creating full resource package... -package: [apkbuilder] Current build type is different than previous build: forced apkbuil der run. [apkbuilder] Creating hehenian-mobile-release-unsigned.apk for release... -post-package: -release-prompt-for-password: -release-nosign: [echo] No key.store and key.alias properties found in build.properties. [echo] Please sign D:Faith_yee's Workplacehehenian-mobileinhehenian-mo bile-release-unsigned.apk manually [echo] and run zipalign from the Android SDK tools. [propertyfile] Updating property file: D:Faith_yee's Workplacehehenian-mobile binuild.prop [propertyfile] Updating property file: D:Faith_yee's Workplacehehenian-mobile binuild.prop [propertyfile] Updating property file: D:Faith_yee's Workplacehehenian-mobile binuild.prop [propertyfile] Updating property file: D:Faith_yee's Workplacehehenian-mobile binuild.prop -release-sign: -post-build: release: BUILD SUCCESSFUL Total time: 4 seconds D:Faith_yee's Workplacehehenian-mobile>


4、给项陌生成签名后的apk

基于ant自动编译打包现有的android项目,可以在ant打包利用的时候加入签名信息,如果开发人员需要使用ant来生成有签名的apk,那末需要进行以下步骤。

1、加入签名信息:

在项目的根目录下建1个ant.properties文件,输入以下内容,其中keystore密码和alias密码可以不指定(防泄漏),那末在命令履行的进程中会要求你输入。 #keystore的路径,必须使用正斜杠 ant是linux习惯的! key.store= "E:/wp_android_sample/me.key" #keystore的密码 #key.store.password=***** #alias名 key.alias=me #alias密码 #key.alias.password=******

例如:


注意:keystore文件可以从eclipse导出或通过sdktool来导出,谷歌建议此密钥只需创建1次,由于可能我们开发的多个app需要数据同享,这里就省略了。

2、在项目根目录下运行 ant release 命令就会帮你生成1个经过签名和aligned的apk,生成的apk(yourprojectname-release.apk)在bin目录下,而app的key就按配置文件里的路径保存。

D:Faith_yee's Workplacehehenian-mobile>ant release Buildfile: D:Faith_yee's Workplacehehenian-mobileuild.xml -check-env: [checkenv] Android SDK Tools Revision 21.1.0 [checkenv] Installed at D:adt-bundle-windows-x86_64_20140101sdk -setup: [echo] Project Name: hehenian-mobile [gettype] Project Type: Application -pre-clean: clean: [delete] Deleting directory D:Faith_yee's Workplacehehenian-mobilein [delete] Deleting directory D:Faith_yee's Workplacehehenian-mobilegen [getlibpath] Library dependencies: [getlibpath] No Libraries [subant] No sub-builds to iterate on -set-mode-check: -set-release-mode: -release-obfuscation-check: [echo] proguard.config is ${proguard.config} -pre-build: -check-env: [checkenv] Android SDK Tools Revision 21.1.0 [checkenv] Installed at D:adt-bundle-windows-x86_64_20140101sdk -setup: [echo] Project Name: hehenian-mobile [gettype] Project Type: Application -build-setup: [echo] Resolving Build Target for hehenian-mobile... [gettarget] Project Target: Android 4.3 [gettarget] API level: 18 [echo] ---------- [echo] Creating output directories if needed... [mkdir] Created dir: D:Faith_yee's Workplacehehenian-mobilein [mkdir] Created dir: D:Faith_yee's Workplacehehenian-mobilein es [mkdir] Created dir: D:Faith_yee's Workplacehehenian-mobilegen [mkdir] Created dir: D:Faith_yee's Workplacehehenian-mobileinclasses [mkdir] Created dir: D:Faith_yee's Workplacehehenian-mobileindexedLibs [echo] ---------- [echo] Resolving Dependencies for hehenian-mobile... [dependency] Ordered libraries: [echo] ---------- [echo] Building Libraries with 'release'... [subant] No sub-builds to iterate on -code-gen: [mergemanifest] Merging AndroidManifest files into one. [mergemanifest] Manifest merger disabled. Using project manifest only. [echo] Handling aidl files... [aidl] No AIDL files to compile. [echo] ---------- [echo] Handling RenderScript files... [renderscript] No RenderScript files to compile. [echo] ---------- [echo] Handling Resources... [aapt] Generating resource IDs... [echo] ---------- [echo] Handling BuildConfig class... [buildconfig] Generating BuildConfig class. -pre-compile: -compile: [javac] Compiling 12 source files to D:Faith_yee's Workplacehehenian-mobil einclasses -post-compile: -obfuscate: -dex: [dex] input: D:Faith_yee's Workplacehehenian-mobileinclasses [dex] input: D:Faith_yee's Workplacehehenian-mobilelibsandroid-support -v4.jar [dex] Pre-Dexing D:Faith_yee's Workplacehehenian-mobilelibsandroid-sup port-v4.jar -> android-support-v4-017fd1f299aa33ccf51747cafa4120bf.jar [dex] Converting compiled files and external libraries into D:Faith_yee's Workplacehehenian-mobileinclasses.dex... [dx] Merged dex A (28 defs/38.5KiB) with dex B (309 defs/349.3KiB). Resul t is 337 defs/470.8KiB. Took 0.1s -crunch: [crunch] Crunching PNG Files in source dir: D:Faith_yee's Workplacehehenian -mobile es [crunch] To destination dir: D:Faith_yee's Workplacehehenian-mobilein es [crunch] Processing image to cache: D:Faith_yee's Workplacehehenian-mobile resdrawable-hdpihehenianlogo_new.png => D:Faith_yee's Workplacehehenian-mobi lein esdrawable-hdpihehenianlogo_new.png [crunch] Processing image to cache: D:Faith_yee's Workplacehehenian-mobile resdrawable-mdpiic_launcher.png => D:Faith_yee's Workplacehehenian-mobilei n esdrawable-mdpiic_launcher.png [crunch] (processed image to cache entry D:Faith_yee's Workplacehehenian- mobilein esdrawable-mdpiic_launcher.png: 0% size of source) [crunch] Processing image to cache: D:Faith_yee's Workplacehehenian-mobile resdrawable-xhdpiic_launcher.png => D:Faith_yee's Workplacehehenian-mobile in esdrawable-xhdpiic_launcher.png [crunch] (processed image to cache entry D:Faith_yee's Workplacehehenian- mobilein esdrawable-xhdpiic_launcher.png: 76% size of source) [crunch] Processing image to cache: D:Faith_yee's Workplacehehenian-mobile resdrawable-xxhdpiic_launcher.png => D:Faith_yee's Workplacehehenian-mobile bin esdrawable-xxhdpiic_launcher.png [crunch] libpng error: Not a PNG file [crunch] (processed image to cache entry D:Faith_yee's Workplacehehenian- mobilein esdrawable-xxhdpiic_launcher.png: 73% size of source) [crunch] Crunched 4 PNG files to update cache -package-resources: [aapt] Creating full resource package... -package: [apkbuilder] Current build type is different than previous build: forced apkbuil der run. [apkbuilder] Creating hehenian-mobile-release-unsigned.apk for release... -post-package: -release-prompt-for-password: -release-nosign: -release-sign: [echo] Signing final apk... [zipalign] Running zip align on final apk... [echo] Release Package: D:Faith_yee's Workplacehehenian-mobileinheheni an-mobile-release.apk [propertyfile] Creating new property file: D:Faith_yee's Workplacehehenian-mob ileinuild.prop [propertyfile] Updating property file: D:Faith_yee's Workplacehehenian-mobile binuild.prop [propertyfile] Updating property file: D:Faith_yee's Workplacehehenian-mobile binuild.prop [propertyfile] Updating property file: D:Faith_yee's Workplacehehenian-mobile binuild.prop -post-build: release: BUILD SUCCESSFUL Total time: 8 seconds D:Faith_yee's Workplacehehenian-mobile>


带签名的apk打包成功!

5、项目中带有第3方的jar包或关联了library库的ant打包方法

1、带有第3方jar包的打包

只要把第3方的jar包放入到项目下的libs文件夹里就能够

2、关联了library库的ant打包方法

1、在dos里进入到library项目的根目录下,并输入anroid update lib-project -p ./

2、library项目一样会生成build.xml和local.properties配置文件

3、在dos里回到自己的项目根目录下,输入以下命令

android update project --name <project_name> --target <target_ID>--path <path_to_your_project> --subprojects
------分隔线----------------------------
------分隔线----------------------------

最新技术推荐