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

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

一、配置ant

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

技术分享

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

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

技术分享技术分享

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

技术分享

二、使用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配置文件

技术分享

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

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

ant debug:生成一个测试版apk,默认使用debug key进行签名,生成的apk(your_project_name-debug.apk)在项目的bin目录下。


ant release:生成一个未签名和未aligned的apk包,project_name-release-unsigned.ap和project_name-release-unaligned.apk在项目的bin目录下。

例如:

ant debug命令

D:\Faith_yee‘s Workplace\hehenian-mobile>android update project --name hehenian-
mobile --target android-18 --path "D:\Faith_yee‘s Workplace\hehenian-mobile"
Updated project.properties
Updated local.properties
Updated file D:\Faith_yee‘s Workplace\hehenian-mobile\build.xml
Updated file D:\Faith_yee‘s Workplace\hehenian-mobile\proguard-project.txt
It seems that there are sub-projects. If you want to update them
please use the --subprojects parameter.

D:\Faith_yee‘s Workplace\hehenian-mobile>ant debug
Buildfile: D:\Faith_yee‘s Workplace\hehenian-mobile\build.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_20140101\sdk

-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 Workplace\hehenian-mobilebin\classes

-post-compile:

-obfuscate:

-dex:
      [dex] input: D:\Faith_yee‘s Workplace\hehenian-mobile\bin\classes
      [dex] input: D:\Faith_yee‘s Workplace\hehenian-mobile\libs\android-support
-v4.jar
      [dex] Using Pre-Dexed android-support-v4-017fd1f299aa33ccf51747cafa4120bf.
jar <- D:\Faith_yee‘s Workplace\hehenian-mobile\libs\android-support-v4.jar
      [dex] Found Deleted Target File
      [dex] Converting compiled files and external libraries into D:\Faith_yee‘s
 Workplace\hehenian-mobile\bin\classes.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 Workplace\hehenian
-mobile\res
   [crunch] To destination dir: D:\Faith_yee‘s Workplace\hehenian-mobile\bin\res

   [crunch] Processing image to cache: D:\Faith_yee‘s Workplace\hehenian-mobileres\drawable-hdpi\hehenianlogo_new.png => D:\Faith_yee‘s Workplace\hehenian-mobi
le\bin\res\drawable-hdpi\hehenianlogo_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 Workplace\hehenian-mobile\bin\hehenian
-mobile-debug.apk
[propertyfile] Updating property file: D:\Faith_yee‘s Workplace\hehenian-mobilebin\build.prop
[propertyfile] Updating property file: D:\Faith_yee‘s Workplace\hehenian-mobilebin\build.prop
[propertyfile] Updating property file: D:\Faith_yee‘s Workplace\hehenian-mobilebin\build.prop
[propertyfile] Updating property file: D:\Faith_yee‘s Workplace\hehenian-mobilebin\build.prop

-post-build:

debug:

BUILD SUCCESSFUL
Total time: 4 seconds

D:\Faith_yee‘s Workplace\hehenian-mobile>

技术分享

ant release命令

D:\Faith_yee‘s Workplace\hehenian-mobile>ant release
Buildfile: D:\Faith_yee‘s Workplace\hehenian-mobile\build.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_20140101\sdk

-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 Workplace\hehenian-mobile\bin\clas
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 Workplace\hehenian-mobile\bin\classes
     [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 Workplace\hehenian-mobil
e\bin\classes

-post-compile:

-obfuscate:

-dex:
      [dex] input: D:\Faith_yee‘s Workplace\hehenian-mobile\bin\classes
      [dex] input: D:\Faith_yee‘s Workplace\hehenian-mobile\libs\android-support
-v4.jar
      [dex] Using Pre-Dexed android-support-v4-017fd1f299aa33ccf51747cafa4120bf.
jar <- D:\Faith_yee‘s Workplace\hehenian-mobile\libs\android-support-v4.jar
      [dex] Found Deleted Target File
      [dex] Converting compiled files and external libraries into D:\Faith_yee‘s
 Workplace\hehenian-mobile\bin\classes.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 Workplace\hehenian
-mobile\res
   [crunch] To destination dir: D:\Faith_yee‘s Workplace\hehenian-mobile\bin\res

   [crunch] Processing image to cache: D:\Faith_yee‘s Workplace\hehenian-mobileres\drawable-hdpi\hehenianlogo_new.png => D:\Faith_yee‘s Workplace\hehenian-mobi
le\bin\res\drawable-hdpi\hehenianlogo_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 Workplace\hehenian-mobile\bin\hehenian-mo
bile-release-unsigned.apk manually
     [echo] and run zipalign from the Android SDK tools.
[propertyfile] Updating property file: D:\Faith_yee‘s Workplace\hehenian-mobilebin\build.prop
[propertyfile] Updating property file: D:\Faith_yee‘s Workplace\hehenian-mobilebin\build.prop
[propertyfile] Updating property file: D:\Faith_yee‘s Workplace\hehenian-mobilebin\build.prop
[propertyfile] Updating property file: D:\Faith_yee‘s Workplace\hehenian-mobilebin\build.prop

-release-sign:

-post-build:

release:

BUILD SUCCESSFUL
Total time: 4 seconds

D:\Faith_yee‘s Workplace\hehenian-mobile>

技术分享

四、给项目生成签名后的apk

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

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来导出,谷歌建议此密钥只需创建一次,因为可能我们开发的多个app需要数据共享,这里就省略了。

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

D:\Faith_yee‘s Workplace\hehenian-mobile>ant release
Buildfile: D:\Faith_yee‘s Workplace\hehenian-mobile\build.xml

-check-env:
 [checkenv] Android SDK Tools Revision 21.1.0
 [checkenv] Installed at D:\adt-bundle-windows-x86_64_20140101\sdk

-setup:
     [echo] Project Name: hehenian-mobile
  [gettype] Project Type: Application

-pre-clean:

clean:
   [delete] Deleting directory D:\Faith_yee‘s Workplace\hehenian-mobile\bin
   [delete] Deleting directory D:\Faith_yee‘s Workplace\hehenian-mobile\gen
[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_20140101\sdk

-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 Workplace\hehenian-mobile\bin
    [mkdir] Created dir: D:\Faith_yee‘s Workplace\hehenian-mobile\bin\res
    [mkdir] Created dir: D:\Faith_yee‘s Workplace\hehenian-mobile\gen
    [mkdir] Created dir: D:\Faith_yee‘s Workplace\hehenian-mobile\bin\classes
    [mkdir] Created dir: D:\Faith_yee‘s Workplace\hehenian-mobile\bin\dexedLibs
     [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 Workplace\hehenian-mobil
e\bin\classes

-post-compile:

-obfuscate:

-dex:
      [dex] input: D:\Faith_yee‘s Workplace\hehenian-mobile\bin\classes
      [dex] input: D:\Faith_yee‘s Workplace\hehenian-mobile\libs\android-support
-v4.jar
      [dex] Pre-Dexing D:\Faith_yee‘s Workplace\hehenian-mobile\libs\android-sup
port-v4.jar -> android-support-v4-017fd1f299aa33ccf51747cafa4120bf.jar
      [dex] Converting compiled files and external libraries into D:\Faith_yee‘s
 Workplace\hehenian-mobile\bin\classes.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 Workplace\hehenian
-mobile\res
   [crunch] To destination dir: D:\Faith_yee‘s Workplace\hehenian-mobile\bin\res

   [crunch] Processing image to cache: D:\Faith_yee‘s Workplace\hehenian-mobileres\drawable-hdpi\hehenianlogo_new.png => D:\Faith_yee‘s Workplace\hehenian-mobi
le\bin\res\drawable-hdpi\hehenianlogo_new.png
   [crunch] Processing image to cache: D:\Faith_yee‘s Workplace\hehenian-mobileres\drawable-mdpi\ic_launcher.png => D:\Faith_yee‘s Workplace\hehenian-mobile\bi
n\res\drawable-mdpi\ic_launcher.png
   [crunch]   (processed image to cache entry D:\Faith_yee‘s Workplace\hehenian-
mobile\bin\res\drawable-mdpi\ic_launcher.png: 0% size of source)
   [crunch] Processing image to cache: D:\Faith_yee‘s Workplace\hehenian-mobileres\drawable-xhdpi\ic_launcher.png => D:\Faith_yee‘s Workplace\hehenian-mobile\b
in\res\drawable-xhdpi\ic_launcher.png
   [crunch]   (processed image to cache entry D:\Faith_yee‘s Workplace\hehenian-
mobile\bin\res\drawable-xhdpi\ic_launcher.png: 76% size of source)
   [crunch] Processing image to cache: D:\Faith_yee‘s Workplace\hehenian-mobileres\drawable-xxhdpi\ic_launcher.png => D:\Faith_yee‘s Workplace\hehenian-mobilebin\res\drawable-xxhdpi\ic_launcher.png
   [crunch] libpng error: Not a PNG file
   [crunch]   (processed image to cache entry D:\Faith_yee‘s Workplace\hehenian-
mobile\bin\res\drawable-xxhdpi\ic_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 Workplace\hehenian-mobile\bin\heheni
an-mobile-release.apk
[propertyfile] Creating new property file: D:\Faith_yee‘s Workplace\hehenian-mob
ile\bin\build.prop
[propertyfile] Updating property file: D:\Faith_yee‘s Workplace\hehenian-mobilebin\build.prop
[propertyfile] Updating property file: D:\Faith_yee‘s Workplace\hehenian-mobilebin\build.prop
[propertyfile] Updating property file: D:\Faith_yee‘s Workplace\hehenian-mobilebin\build.prop

-post-build:

release:

BUILD SUCCESSFUL
Total time: 8 seconds

D:\Faith_yee‘s Workplace\hehenian-mobile>

技术分享

带签名的apk打包成功!

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

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

只要把第三方的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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。