http://www.oschina.net/question/54100_30501?sort=default&p=2#answers
通常我们习惯用eclipse来开发Android程序,它会自动帮我们打包当前的应用程序。如果在Navigator视图下,我们可以看到以下几个文件:
在上图中,com包放置的是我们的class文件,classes.dex是class文件经过转换后的可以在dalvik上跑的精简类文件,resources.ap_是经过打包的资源文件,ant.apk就是最终的打包文件。
使用ANT来对应用打包,一般会经过以下几个步骤:
1.用aapt命令生成R.java文件
2.用aidl命令生成相应java文件
3.用javac命令编译java源文件生成class文件
4.用dx.bat将class文件转换成classes.dex文件
5.用aapt命令生成资源包文件resources.ap_
6.用apkbuilder.bat打包资源和classes.dex文件,生成unsigned.apk
7.用jarsinger命令对apk认证,生成signed.apk
为了便于理解和记忆,下面来用一张流程图来说明以上的几个过程:
以上就是整体的流程,下面我们就对其每个部分进行做出详细讲解,把每一个步骤都弄清楚了。
我们需要先熟悉一下每一个步骤所使用到的命令:
1.aapt(Android Asset Packaging Tool)命令,根据资源文件生成R.java文件
参数说明:
-f 强制覆盖已存在的文件。
-m 在-J指定的位置下自动生成相应的包的目录。
-J 指定R.java文件生成的目录。
-S 指定资源目录。
-M 指定清单文件。
-I 引入类库。
注意,我们当前所在的位置是ant项目根目录,所以必要时需要输入很多关于命令的路径,以下示例也是一样。
2.aidl(Android Interface Definition Language)命令,根据.aidl定义文件生成java文件
上面的示例所在位置为com/scott/ant下,根据包中的Person.aidl文件,在gen对应的目录中生成Person.java文件,示例中只是处理单一文件,下文中会讲述如何处理目录中的多个aidl文件。
3.javac(Java Compiler)命令,根据源文件生成对应的class文件
参数说明:
-d <目录> 指定存放生成的类文件的位置
-bootclasspath <路径> 覆盖引导类文件的位置
示例中并没有考虑到引用类路径下面的类库,复杂的情况会在稍后遇到的。
4.dx命令,将class文件转换成.dex文件
以上示例是将bin目录下的class文件转换成classes.dex文件,输出到bin目录,我们也许会用到第三方类库,等一会就会看到。
5.aapt将资源文件打包
参数说明:
-f 强制覆盖
-M 指定Manifest文件
-S 指定资源目录
-A 指定资产目录
-I 指定引入的类库
-F 指定要生成的包
6.apkbuilder命令,根据classes.dex文件和resources.ap_生成为签证的apk包
参数说明:
-rf 参照源文件的目录的结构
7.jarsigner命令,对上面生成的apk包进行签证
在签证的过程中,需要使用到证书文件,需要注意的是最后的release是证书的别名,关于如何创建证书,请看下图:
当然也可以在eclipse里使用ADT提供的图形界面完成以上步骤,选中项目,点击右键,“Android Tools=>Export Signed Application Package”,然后再其中的Keystore selection环节选择“Create new keystore”,然后按照提示填写信息就可以了。
以上是我们使用到的命令,接下来我们就该来分析一下ANT所必须的build.xml:
首先我们需要定义大量的变量属性,用来表示使用到的路径、目录等,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
<
project
name
=
"ant"
default
=
"release"
>
<
property
environment
=
"env"
/>
<
property
name
=
"appName"
value
=
"${ant.project.name}"
/>
<
property
name
=
"sdk-folder"
value
=
"${env.ANDROID_SDK_HOME}"
/>
<
property
name
=
"sdk-platform-folder"
value
=
"${sdk-folder}/platforms/android-8"
/>
<
property
name
=
"sdk-tools"
value
=
"${sdk-folder}/tools"
/>
<
property
name
=
"sdk-platform-tools"
value
=
"${sdk-platform-folder}/tools"
/>
<
property
name
=
"aapt"
value
=
"${sdk-platform-tools}/aapt"
/>
<
property
name
=
"aidl"
value
=
"${sdk-platform-tools}/aidl"
/>
<
property
name
=
"dx"
value
=
"${sdk-platform-tools}/dx.bat"
/>
<
property
name
=
"apkbuilder"
value
=
"${sdk-tools}/apkbuilder.bat"
/>
<
property
name
=
"jarsigner"
value
=
"${env.JAVA_HOME}/bin/jarsigner"
/>
<
property
name
=
"android-jar"
value
=
"${sdk-platform-folder}/android.jar"
/>
<
property
name
=
"android-maps-jar"
value
=
"${sdk-folder}/add-ons/addon_google_apis_google_inc_8/libs/maps.jar"
/>
<
property
name
=
"framework-aidl"
value
=
"${sdk-platform-folder}/framework.aidl"
/>
<
property
name
=
"outdir-gen"
value
=
"gen"
/>
<
property
name
=
"outdir-bin"
value
=
"bin"
/>
<
property
name
=
"manifest-xml"
value
=
"AndroidManifest.xml"
/>
<
property
name
=
"resource-dir"
value
=
"res"
/>
<
property
name
=
"asset-dir"
value
=
"assets"
/>
<
property
name
=
"srcdir"
value
=
"src"
/>
<
property
name
=
"srcdir-ospath"
value
=
"${basedir}/${srcdir}"
/>
<
property
name
=
"external-lib"
value
=
"lib"
/>
<
property
name
=
"external-lib-ospath"
value
=
"${basedir}/${external-lib}"
/>
<
property
name
=
"outdir-classes"
value
=
"${outdir-bin}"
/>
<
property
name
=
"outdir-classes-ospath"
value
=
"${basedir}/${outdir-classes}"
/>
<
property
name
=
"dex-file"
value
=
"classes.dex"
/>
<
property
name
=
"dex-path"
value
=
"${outdir-bin}/${dex-file}"
/>
<
property
name
=
"dex-ospath"
value
=
"${basedir}/${dex-path}"
/>
<
property
name
=
"resources-package"
value
=
"${outdir-bin}/resources.ap_"
/>
<
property
name
=
"resources-package-ospath"
value
=
"${basedir}/${resources-package}"
/>
<
property
name
=
"out-unsigned-package"
value
=
"${outdir-bin}/${appName}-unsigned.apk"
/>
<
property
name
=
"out-unsigned-package-ospath"
value
=
"${basedir}/${out-unsigned-package}"
/>
<
property
name
=
"keystore-file"
value
=
"${basedir}/release.keystore"
/>
<
property
name
=
"out-signed-package"
value
=
"${outdir-bin}/${appName}.apk"
/>
<
property
name
=
"out-signed-package-ospath"
value
=
"${basedir}/${out-signed-package}"
/>
...
project
>
|
1
2
3
4
5
6
7
|
<
target
name
=
"init"
>
<
echo
>Initializing all output directories...
echo
>
<
delete
dir
=
"${outdir-bin}"
/>
<
mkdir
dir
=
"${outdir-bin}"
/>
<
mkdir
dir
=
"${outdir-classes}"
/>
target
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<
target
name
=
"gen-R"
depends
=
"init"
>
<
echo
>Generating R.java from the resources...
echo
>
<
exec
executable
=
"${aapt}"
failonerror
=
"true"
>
<
arg
value
=
"package"
/>
<
arg
value
=
"-f"
/>
<
arg
value
=
"-m"
/>
<
arg
value
=
"-J"
/>
<
arg
value
=
"${outdir-gen}"
/>
<
arg
value
=
"-S"
/>
<
arg
value
=
"${resource-dir}"
/>
<
arg
value
=
"-M"
/>
<
arg
value
=
"${manifest-xml}"
/>
<
arg
value
=
"-I"
/>
<
arg
value
=
"${android-jar}"
/>
exec
>
target
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<
target
name
=
"aidl"
depends
=
"gen-R"
>
<
echo
>Compiling .aidl into java files...
echo
>
<
apply
executable
=
"${aidl}"
failonerror
=
"true"
>
<
arg
value
=
"-p${framework-aidl}"
/>
<
arg
value
=
"-I${srcdir}"
/>
<
arg
value
=
"-o${outdir-gen}"
/>
<
fileset
dir
=
"${srcdir}"
>
<
include
name
=
"**/*.aidl"
/>
fileset
>
apply
>
target
>
|
接下来是将源文件编译成class文件:
1
2
3
4
5
6
7
8
9
10
11
12
|
<
target
name
=
"compile"
depends
=
"aidl"
>
<
echo
>Compiling java source code...
echo
>
<
javac
encoding
=
"utf-8"
target
=
"1.5"
srcdir
=
"."
destdir
=
"${outdir-classes}"
bootclasspath
=
"${android-jar}"
>
<
classpath
>
<
fileset
dir
=
"${external-lib}"
includes
=
"*.jar"
/>
<
filelist
>
<
file
name
=
"${android-maps-jar}"
/>
filelist
>
classpath
>
javac
>
target
>
|
接着是将class文件转换成classes.dex:
1
2
3
4
5
6
7
8
9
10
11
12
|
<
target
name
=
"dex"
depends
=
"compile"
>
<
echo
>Converting compiled files and external libraries into a .dex file...
echo
>
<
exec
executable
=
"${dx}"
failonerror
=
"true"
>
<
arg
value
=
"--dex"
/>
<
arg
value
=
"--output=${dex-ospath}"
/>
<
arg
value
=
"${outdir-classes-ospath}"
/>
<
arg
value
=
"${external-lib-ospath}"
/>
exec
>
target
>
|
然后是将资源文件打包:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<
target
name
=
"package-res-and-assets"
>
<
echo
>Packaging resources and assets...
echo
>
<
exec
executable
=
"${aapt}"
failonerror
=
"true"
>
<
arg
value
=
"package"
/>
<
arg
value
=
"-f"
/>
<
arg
value
=
"-M"
/>
<
arg
value
=
"${manifest-xml}"
/>
<
arg
value
=
"-S"
/>
<
arg
value
=
"${resource-dir}"
/>
<
arg
value
=
"-A"
/>
<
arg
value
=
"${asset-dir}"
/>
<
arg
value
=
"-I"
/>
<
arg
value
=
"${android-jar}"
/>
<
arg
value
=
"-F"
/>
<
arg
value
=
"${resources-package}"
/>
exec
>
target
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<
target
name
=
"package"
depends
=
"dex, package-res-and-assets"
>
<
echo
>Packaging unsigned apk for release...
echo
>
<
exec
executable
=
"${apkbuilder}"
failonerror
=
"true"
>
<
arg
value
=
"${out-unsigned-package-ospath}"
/>
<
arg
value
=
"-u"
/>
<
arg
value
=
"-z"
/>
<
arg
value
=
"${resources-package-ospath}"
/>
<
arg
value
=
"-f"
/>
<
arg
value
=
"${dex-ospath}"
/>
<
arg
value
=
"-rf"
/>
<
arg
value
=
"${srcdir-ospath}"
/>
exec
>
<
echo
>It will need to be signed with jarsigner before being published.
echo
>
target
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<
target
name
=
"jarsigner"
depends
=
"package"
>
<
echo
>Packaging signed apk for release...
echo
>
<
exec
executable
=
"${jarsigner}"
failonerror
=
"true"
>
<
arg
value
=
"-keystore"
/>
<
arg
value
=
"${keystore-file}"
/>
<
arg
value
=
"-storepass"
/>
<
arg
value
=
"123456"
/>
<
arg
value
=
"-keypass"
/>
<
arg
value
=
"123456"
/>
<
arg
value
=
"-signedjar"
/>
<
arg
value
=
"${out-signed-package-ospath}"
/>
<
arg
value
=
"${out-unsigned-package-ospath}"
/>
<
arg
value
=
"release"
/>
exec
>
target
>
|
1
2
3
4
5
6
|
<
target
name
=
"release"
depends
=
"jarsigner"
>
<
delete
file
=
"${out-unsigned-package-ospath}"
/>
<
echo
>APK is released. path:${out-signed-package-ospath}
echo
>
target
>
|
这样就完成了build.xml的编辑,eclipse继承了ANT,所以我们可以在eclipse中直接运行,也可以在代码中调用。
首先我们需要下载ANT,然后配置相应的环境变量信息,最后我们这样调用:
1
2
3
4
5
6
7
8
|
Process p = Runtime.getRuntime().exec(
"ant.bat -buildfile d:/workspace/ant/build.xml"
);
InputStream is = p.getInputStream();
BufferedReader br =
new
BufferedReader(
new
InputStreamReader(is));
String line =
null
;
while
((line = br.readLine()) !=
null
) {
System.out.println(line);
}
System.out.println(
"SUCCESS."
);
|
全文完!
原文出处:http://blog.csdn.net/liuhe688/article/details/6679879