一、配置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环境下输入命令:
1.
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输入如下指令
1.
ant debug:生成一个测试版apk,默认使用debug key进行签名,生成的apk(your_project_name-debug.apk)在项目的bin目录下。
2.
3.
4.
ant release:生成一个未签名和未aligned的apk包,project_name-release-unsigned.ap和project_name-release-unaligned.apk在项目的bin目录下。
例如:
ant debug命令
001.
D:\Faith_yee's Workplace\hehenian-mobile>android update project --name hehenian-
002.
mobile --target android-
18
--path
"D:\Faith_yee's Workplace\hehenian-mobile"
003.
Updated project.properties
004.
Updated local.properties
005.
Updated file D:\Faith_yee's Workplace\hehenian-mobile\build.xml
006.
Updated file D:\Faith_yee's Workplace\hehenian-mobile\proguard-project.txt
007.
It seems that there are sub-projects. If you want to update them
008.
please use the --subprojects parameter.
009.
010.
D:\Faith_yee's Workplace\hehenian-mobile>ant debug
011.
Buildfile: D:\Faith_yee's Workplace\hehenian-mobile\build.xml
012.
013.
-set-mode-check:
014.
015.
-set-debug-files:
016.
017.
-check-env:
018.
[checkenv] <a href=
"http://www.it165.net/pro/ydad/"
target=
"_blank"
class
=
"keylink"
>Android</a> SDK Tools Revision
21.1
.
0
019.
[checkenv] Installed at D:\adt-bundle-windows-x86_64_20140101\sdk
020.
021.
-setup:
022.
[echo] Project Name: hehenian-mobile
023.
[gettype] Project Type: Application
024.
025.
-set-debug-mode:
026.
027.
-debug-obfuscation-check:
028.
029.
-pre-build:
030.
031.
-build-setup:
032.
[echo] Resolving Build Target
for
hehenian-mobile...
033.
[gettarget] Project Target: <a href=
"http://www.it165.net/pro/ydad/"
target=
"_blank"
class
=
"keylink"
>Android</a>
4.3
034.
[gettarget] API level:
18
035.
[echo] ----------
036.
[echo] Creating output directories
if
needed...
037.
[echo] ----------
038.
[echo] Resolving Dependencies
for
hehenian-mobile...
039.
[dependency] Library dependencies:
040.
[dependency] No Libraries
041.
[echo] ----------
042.
[echo] Building Libraries with
'debug'
...
043.
[subant] No sub-builds to iterate on
044.
045.
-code-gen:
046.
[mergemanifest] Found Deleted Target File
047.
[mergemanifest] Merging AndroidManifest files into one.
048.
[mergemanifest] Manifest merger disabled. Using project manifest only.
049.
[echo] Handling aidl files...
050.
[aidl] No AIDL files to compile.
051.
[echo] ----------
052.
[echo] Handling RenderScript files...
053.
[renderscript] No RenderScript files to compile.
054.
[echo] ----------
055.
[echo] Handling Resources...
056.
[aapt] Found Deleted Target File
057.
[aapt] Generating resource IDs...
058.
[echo] ----------
059.
[echo] Handling BuildConfig
class
...
060.
[buildconfig] No need to generate
new
BuildConfig.
061.
062.
-pre-compile:
063.
064.
-compile:
065.
[javac] Compiling
1
source file to D:\Faith_yee's Workplace\hehenian-mobile\
066.
bin\classes
067.
068.
-post-compile:
069.
070.
-obfuscate:
071.
072.
-dex:
073.
[dex] input: D:\Faith_yee's Workplace\hehenian-mobile\bin\classes
074.
[dex] input: D:\Faith_yee's Workplace\hehenian-mobile\libs\android-support
075.
-v4.jar
076.
[dex] Using Pre-Dexed android-support-v4-017fd1f299aa33ccf51747cafa4120bf.
077.
jar <- D:\Faith_yee's Workplace\hehenian-mobile\libs\android-support-v4.jar
078.
[dex] Found Deleted Target File
079.
[dex] Converting compiled files and external libraries into D:\Faith_yee's
080.
Workplace\hehenian-mobile\bin\classes.dex...
081.
[dx] Merged dex A (
28
defs/
38
.5KiB) with dex B (
309
defs/
349
.3KiB). Resul
082.
t is
337
defs/
470
.8KiB. Took
0
.2s
083.
084.
-crunch:
085.
[crunch] Crunching PNG Files in source dir: D:\Faith_yee's Workplace\hehenian
086.
-mobile\res
087.
[crunch] To destination dir: D:\Faith_yee's Workplace\hehenian-mobile\bin\res
088.
089.
[crunch] Processing image to cache: D:\Faith_yee's Workplace\hehenian-mobile\
090.
res\drawable-hdpi\hehenianlogo_new.png => D:\Faith_yee's Workplace\hehenian-mobi
091.
le\bin\res\drawable-hdpi\hehenianlogo_new.png
092.
[crunch] Crunched
1
PNG files to update cache
093.
[crunch] libpng error: Not a PNG file
094.
095.
-
package
-resources:
096.
[aapt] Found Deleted Target File
097.
[aapt] Creating full resource
package
...
098.
099.
-
package
:
100.
[apkbuilder] Found Deleted Target File
101.
[apkbuilder] Creating hehenian-mobile-debug-unaligned.apk and signing it with a
102.
debug key...
103.
104.
-post-
package
:
105.
106.
-
do
-debug:
107.
[zipalign] Running zip align on
final
apk...
108.
[echo] Debug Package: D:\Faith_yee's Workplace\hehenian-mobile\bin\hehenian
109.
-mobile-debug.apk
110.
[propertyfile] Updating property file: D:\Faith_yee's Workplace\hehenian-mobile\
111.
bin\build.prop
112.
[propertyfile] Updating property file: D:\Faith_yee's Workplace\hehenian-mobile\
113.
bin\build.prop
114.
[propertyfile] Updating property file: D:\Faith_yee's Workplace\hehenian-mobile\
115.
bin\build.prop
116.
[propertyfile] Updating property file: D:\Faith_yee's Workplace\hehenian-mobile\
117.
bin\build.prop
118.
119.
-post-build:
120.
121.
debug:
122.
123.
BUILD SUCCESSFUL
124.
Total time:
4
seconds
125.
126.
D:\Faith_yee's Workplace\hehenian-mobile>
ant release命令
001.
D:\Faith_yee's Workplace\hehenian-mobile>ant release
002.
Buildfile: D:\Faith_yee's Workplace\hehenian-mobile\build.xml
003.
004.
-set-mode-check:
005.
006.
-set-release-mode:
007.
008.
-release-obfuscation-check:
009.
[echo] proguard.config is ${proguard.config}
010.
011.
-pre-build:
012.
013.
-check-env:
014.
[checkenv] Android SDK Tools Revision
21.1
.
0
015.
[checkenv] Installed at D:\adt-bundle-windows-x86_64_20140101\sdk
016.
017.
-setup:
018.
[echo] Project Name: hehenian-mobile
019.
[gettype] Project Type: Application
020.
021.
-build-setup:
022.
[echo] Switching between debug and non debug build: Deleting previous compi
023.
lation output...
024.
[delete] Deleting directory D:\Faith_yee's Workplace\hehenian-mobile\bin\clas
025.
ses
026.
[echo] Resolving Build Target
for
hehenian-mobile...
027.
[gettarget] Project Target: Android
4.3
028.
[gettarget] API level:
18
029.
[echo] ----------
030.
[echo] Creating output directories
if
needed...
031.
[mkdir] Created dir: D:\Faith_yee's Workplace\hehenian-mobile\bin\classes
032.
[echo] ----------
033.
[echo] Resolving Dependencies
for
hehenian-mobile...
034.
[dependency] Library dependencies:
035.
[dependency] No Libraries
036.
[echo] ----------
037.
[echo] Building Libraries with
'release'
...
038.
[subant] No sub-builds to iterate on
039.
040.
-code-gen:
041.
[mergemanifest] Found Deleted Target File
042.
[mergemanifest] Merging AndroidManifest files into one.
043.
[mergemanifest] Manifest merger disabled. Using project manifest only.
044.
[echo] Handling aidl files...
045.
[aidl] No AIDL files to compile.
046.
[echo] ----------
047.
[echo] Handling RenderScript files...
048.
[renderscript] No RenderScript files to compile.
049.
[echo] ----------
050.
[echo] Handling Resources...
051.
[aapt] Found Deleted Target File
052.
[aapt] Generating resource IDs...
053.
[echo] ----------
054.
[echo] Handling BuildConfig
class
...
055.
[buildconfig] Build type changed: Generating
new
BuildConfig
class
.
056.
057.
-pre-compile:
058.
059.
-compile:
060.
[javac] Compiling
12
source files to D:\Faith_yee's Workplace\hehenian-mobil
061.
e\bin\classes
062.
063.
-post-compile:
064.
065.
-obfuscate:
066.
067.
-dex:
068.
[dex] input: D:\Faith_yee's Workplace\hehenian-mobile\bin\classes
069.
[dex] input: D:\Faith_yee's Workplace\hehenian-mobile\libs\android-support
070.
-v4.jar
071.
[dex] Using Pre-Dexed android-support-v4-017fd1f299aa33ccf51747cafa4120bf.
072.
jar <- D:\Faith_yee's Workplace\hehenian-mobile\libs\android-support-v4.jar
073.
[dex] Found Deleted Target File
074.
[dex] Converting compiled files and external libraries into D:\Faith_yee's
075.
Workplace\hehenian-mobile\bin\classes.dex...
076.
[dx] Merged dex A (
28
defs/
38
.5KiB) with dex B (
309
defs/
349
.3KiB). Resul
077.
t is
337
defs/
470
.8KiB. Took
0
.2s
078.
079.
-crunch:
080.
[crunch] Crunching PNG Files in source dir: D:\Faith_yee's Workplace\hehenian
081.
-mobile\res
082.
[crunch] To destination dir: D:\Faith_yee's Workplace\hehenian-mobile\bin\res
083.
084.
[crunch] Processing image to cache: D:\Faith_yee's Workplace\hehenian-mobile\
085.
res\drawable-hdpi\hehenianlogo_new.png => D:\Faith_yee's Workplace\hehenian-mobi
086.
le\bin\res\drawable-hdpi\hehenianlogo_new.png
087.
[crunch] Crunched
1
PNG files to update cache
088.
[crunch] libpng error: Not a PNG file
089.
090.
-
package
-resources:
091.
[aapt] Current build type is different than previous build: forced aapt run
092.
.
093.
[aapt] Creating full resource
package
...
094.
095.
-
package
:
096.
[apkbuilder] Current build type is different than previous build: forced apkbuil
097.
der run.
098.
[apkbuilder] Creating hehenian-mobile-release-unsigned.apk
for
release...
099.
100.
-post-
package
:
101.
102.
-release-prompt-
for
-password:
103.
104.
-release-nosign:
105.
[echo] No key.store and key.alias properties found in build.properties.
106.
[echo] Please sign D:\Faith_yee's Workplace\hehenian-mobile\bin\hehenian-mo
107.
bile-release-unsigned.apk manually
108.
[echo] and run zipalign from the Android SDK tools.
109.
[propertyfile] Updating property file: D:\Faith_yee's Workplace\hehenian-mobile\
110.
bin\build.prop
111.
[propertyfile] Updating property file: D:\Faith_yee's Workplace\hehenian-mobile\
112.
bin\build.prop
113.
[propertyfile] Updating property file: D:\Faith_yee's Workplace\hehenian-mobile\
114.
bin\build.prop
115.
[propertyfile] Updating property file: D:\Faith_yee's Workplace\hehenian-mobile\
116.
bin\build.prop
117.
118.
-release-sign:
119.
120.
-post-build:
121.
122.
release:
123.
124.
BUILD SUCCESSFUL
125.
Total time:
4
seconds
126.
127.
D:\Faith_yee's Workplace\hehenian-mobile>
四、给项目生成签名后的apk
基于ant自动编译打包现有的android项目,可以在ant打包应用的时候加入签名信息,如果开发人员需要使用ant来生成有签名的apk,那么需要进行如下步骤。
1、加入签名信息:
01.
在项目的根目录下建一个ant.properties文件,输入如下内容,其中keystore密码和alias密码可以不指定(防泄漏),那么在命令执行的过程中会要求你输入。
02.
03.
#keystore的路径,必须使用正斜杠 ant是linux习惯的!
04.
key.store=
"E:/wp_android_sample/me.key"
05.
#keystore的密码
06.
#key.store.password=*****
07.
#alias名
08.
key.alias=me
09.
#alias密码
10.
#key.alias.password=******
例如:
1.
注意:keystore文件可以从eclipse导出或者通过sdktool来导出,谷歌建议此密钥只需创建一次,因为可能我们开发的多个app需要数据共享,这里就省略了。
2、在项目根目录下运行 ant release 命令就会帮你生成一个经过签名和aligned的apk,生成的apk(yourprojectname-release.apk)在bin目录下,而app的key就按配置文件里的路径保存。
001.
D:\Faith_yee's Workplace\hehenian-mobile>ant release
002.
Buildfile: D:\Faith_yee's Workplace\hehenian-mobile\build.xml
003.
004.
-check-env:
005.
[checkenv] Android SDK Tools Revision
21.1
.
0
006.
[checkenv] Installed at D:\adt-bundle-windows-x86_64_20140101\sdk
007.
008.
-setup:
009.
[echo] Project Name: hehenian-mobile
010.
[gettype] Project Type: Application
011.
012.
-pre-clean:
013.
014.
clean:
015.
[delete] Deleting directory D:\Faith_yee's Workplace\hehenian-mobile\bin
016.
[delete] Deleting directory D:\Faith_yee's Workplace\hehenian-mobile\gen
017.
[getlibpath] Library dependencies:
018.
[getlibpath] No Libraries
019.
[subant] No sub-builds to iterate on
020.
021.
-set-mode-check:
022.
023.
-set-release-mode:
024.
025.
-release-obfuscation-check:
026.
[echo] proguard.config is ${proguard.config}
027.
028.
-pre-build:
029.
030.
-check-env:
031.
[checkenv] Android SDK Tools Revision
21.1
.
0
032.
[checkenv] Installed at D:\adt-bundle-windows-x86_64_20140101\sdk
033.
034.
-setup:
035.
[echo] Project Name: hehenian-mobile
036.
[gettype] Project Type: Application
037.
038.
-build-setup:
039.
[echo] Resolving Build Target
for
hehenian-mobile...
040.
[gettarget] Project Target: Android
4.3
041.
[gettarget] API level:
18
042.
[echo] ----------
043.
[echo] Creating output directories
if
needed...
044.
[mkdir] Created dir: D:\Faith_yee's Workplace\hehenian-mobile\bin
045.
[mkdir] Created dir: D:\Faith_yee's Workplace\hehenian-mobile\bin\res
046.
[mkdir] Created dir: D:\Faith_yee's Workplace\hehenian-mobile\gen
047.
[mkdir] Created dir: D:\Faith_yee's Workplace\hehenian-mobile\bin\classes
048.
[mkdir] Created dir: D:\Faith_yee's Workplace\hehenian-mobile\bin\dexedLibs
049.
[echo] ----------
050.
[echo] Resolving Dependencies
for
hehenian-mobile...
051.
[dependency] Ordered libraries:
052.
[echo] ----------
053.
[echo] Building Libraries with
'release'
...
054.
[subant] No sub-builds to iterate on
055.
056.
-code-gen:
057.
[mergemanifest] Merging AndroidManifest files into one.
058.
[mergemanifest] Manifest merger disabled. Using project manifest only.
059.
[echo] Handling aidl files...
060.
[aidl] No AIDL files to compile.
061.
[echo] ----------
062.
[echo] Handling RenderScript files...
063.
[renderscript] No RenderScript files to compile.
064.
[echo] ----------
065.
[echo] Handling Resources...
066.
[aapt] Generating resource IDs...
067.
[echo] ----------
068.
[echo] Handling BuildConfig
class
...
069.
[buildconfig] Generating BuildConfig
class
.
070.
071.
-pre-compile:
072.
073.
-compile:
074.
[javac] Compiling
12
source files to D:\Faith_yee's Workplace\hehenian-mobil
075.
e\bin\classes
076.
077.
-post-compile:
078.
079.
-obfuscate:
080.
081.
-dex:
082.
[dex] input: D:\Faith_yee's Workplace\hehenian-mobile\bin\classes
083.
[dex] input: D:\Faith_yee's Workplace\hehenian-mobile\libs\android-support
084.
-v4.jar
085.
[dex] Pre-Dexing D:\Faith_yee's Workplace\hehenian-mobile\libs\android-sup
086.
port-v4.jar -> android-support-v4-017fd1f299aa33ccf51747cafa4120bf.jar
087.
[dex] Converting compiled files and external libraries into D:\Faith_yee's
088.
Workplace\hehenian-mobile\bin\classes.dex...
089.
[dx] Merged dex A (
28
defs/
38
.5KiB) with dex B (
309
defs/
349
.3KiB). Resul
090.
t is
337
defs/
470
.8KiB. Took
0
.1s
091.
092.
-crunch:
093.
[crunch] Crunching PNG Files in source dir: D:\Faith_yee's Workplace\hehenian
094.
-mobile\res
095.
[crunch] To destination dir: D:\Faith_yee's Workplace\hehenian-mobile\bin\res
096.
097.
[crunch] Processing image to cache: D:\Faith_yee's Workplace\hehenian-mobile\
098.
res\drawable-hdpi\hehenianlogo_new.png => D:\Faith_yee's Workplace\hehenian-mobi
099.
le\bin\res\drawable-hdpi\hehenianlogo_new.png
100.
[crunch] Processing image to cache: D:\Faith_yee's Workplace\hehenian-mobile\
101.
res\drawable-mdpi\ic_launcher.png => D:\Faith_yee's Workplace\hehenian-mobile\bi
102.
n\res\drawable-mdpi\ic_launcher.png
103.
[crunch] (processed image to cache entry D:\Faith_yee's Workplace\hehenian-
104.
mobile\bin\res\drawable-mdpi\ic_launcher.png:
0
% size of source)
105.
[crunch] Processing image to cache: D:\Faith_yee's Workplace\hehenian-mobile\
106.
res\drawable-xhdpi\ic_launcher.png => D:\Faith_yee's Workplace\hehenian-mobile\b
107.
in\res\drawable-xhdpi\ic_launcher.png
108.
[crunch] (processed image to cache entry D:\Faith_yee's Workplace\hehenian-
109.
mobile\bin\res\drawable-xhdpi\ic_launcher.png:
76
% size of source)
110.
[crunch] Processing image to cache: D:\Faith_yee's Workplace\hehenian-mobile\
111.
res\drawable-xxhdpi\ic_launcher.png => D:\Faith_yee's Workplace\hehenian-mobile\
112.
bin\res\drawable-xxhdpi\ic_launcher.png
113.
[crunch] libpng error: Not a PNG file
114.
[crunch] (processed image to cache entry D:\Faith_yee's Workplace\hehenian-
115.
mobile\bin\res\drawable-xxhdpi\ic_launcher.png:
73
% size of source)
116.
[crunch] Crunched
4
PNG files to update cache
117.
118.
-
package
-resources:
119.
[aapt] Creating full resource
package
...
120.
121.
-
package
:
122.
[apkbuilder] Current build type is different than previous build: forced apkbuil
123.
der run.
124.
[apkbuilder] Creating hehenian-mobile-release-unsigned.apk
for
release...
125.
126.
-post-
package
:
127.
128.
-release-prompt-
for
-password:
129.
130.
-release-nosign:
131.
132.
-release-sign:
133.
[echo] Signing
final
apk...
134.
[zipalign] Running zip align on
final
apk...
135.
[echo] Release Package: D:\Faith_yee's Workplace\hehenian-mobile\bin\heheni
136.
an-mobile-release.apk
137.
[propertyfile] Creating
new
property file: D:\Faith_yee's Workplace\hehenian-mob
138.
ile\bin\build.prop
139.
[propertyfile] Updating property file: D:\Faith_yee's Workplace\hehenian-mobile\
140.
bin\build.prop
141.
[propertyfile] Updating property file: D:\Faith_yee's Workplace\hehenian-mobile\
142.
bin\build.prop
143.
[propertyfile] Updating property file: D:\Faith_yee's Workplace\hehenian-mobile\
144.
bin\build.prop
145.
146.
-post-build:
147.
148.
release:
149.
150.
BUILD SUCCESSFUL
151.
Total time:
8
seconds
152.
153.
D:\Faith_yee's Workplace\hehenian-mobile>
带签名的apk打包成功!
五、项目中带有第三方的jar包或关联了library库的ant打包方法
1、带有第三方jar包的打包
只穴ky"http://www.it165.net/qq/" target="_blank" class="keylink">qqw0bXayP23vbXEamFysPy3xcjrtb3P7sS/z8K1xGxpYnPOxLz+vNDA777Nv8nS1DwvcD4KPGgzPjKhornYwarBy2xpYnJhcnm/4rXEYW50tPKw/Le9t6g8L2gzPgo8cD4KMaGi1Npkb3PA7734yOu1vWxpYnJhcnnP7sS/tcS4+cS/wrzPwqOssqLK5MjrYW5yb2lkIHVwZGF0ZSBsaWItcHJvamVjdCAtcCAuLzwvcD4KPHA+CjKhomxpYnJhcnnP7sS/zazR+bvhyfqzyWJ1aWxkLnhtbLrNbG9jYWwucHJvcGVydGllc8Xk1sPOxLz+PC9wPgo8cD4KM6Gi1Npkb3PA77vYtb3X1Ly6tcTP7sS/uPnEv8K8z8KjrMrkyOvS1M/Cw/zB7jwvcD4KPHByZSBjbGFzcz0="brush:java;">android update project --name <project_name> --target <target_ID>--path <path_to_your_project> --subprojects