一般情况下,我们用Eclipse开发Android应用,几乎从来没有考虑用命令行来编译,因为Eclipse太强大了以至于Android开发成为了傻瓜。
今天,我决心来尝试一下用ant来编译Android应用,ant编译Android应用其实就是调用多个Android SDK提供的多个编译工具,最后编译打包签名,形成apk文件。
我的开发环境是Windows XP,Java是1.6的,Eclipse是adt自带的那个。下面是用ant来编译Android应用的build.xml的内容。
start initing ... finish initing. generating R.java for project to dir gen (using aapt) ... generating R.java for library to dir gen (using aapt) ... compiling java files to class files (include R.java, library and the third-party jars) ... packaging class files (include the third-party jars) to calsses.dex ... packaging resource (include res, assets, AndroidManifest.xml, etc.) to res.zip ... building unsigned.apk ... signing the unsigned apk to final product apk ... done.
上面的build.xml文件在我的机器上测试通过了。注意,我加粗的部分就是要稍作修改的部分,你应该懂的。
要注意的是,在执行之前要用 keytool -genkey -alias yourname 来生成一个密钥。并将.keystore文件复制到目录下,或者指定具体的位置。否则,最后一步不能成功的话,就无法生成在手机上可安装使用的apk文件了。
E:\workspace\Hello8>ant
Buildfile: E:\workspace\Hello8\build.xml
init:
[echo] start initing ...
[mkdir] Created dir: E:\workspace\Hello8\out
[mkdir] Created dir: E:\workspace\Hello8\gen
[mkdir] Created dir: E:\workspace\Hello8\bin\classes
[mkdir] Created dir: E:\workspace\Hello8\build\latest
[echo] finish initing.
main:
[echo] generating R.java for project to dir gen (using aapt) ...
[echo] generating R.java for library to dir gen (using aapt) ...
[echo] compiling java files to class files (include R.java, library and the
third-party jars) ...
[javac] E:\workspace\Hello8\build.xml:76: warning: 'includeantruntime' was n
ot set, defaulting to build.sysclasspath=last; set to false for repeatable build
s
[javac] Compiling 2 source files to E:\workspace\Hello8\bin\classes
[echo] packaging class files (include the third-party jars) to calsses.dex
...
[echo] packaging resource (include res, assets, AndroidManifest.xml, etc.)
to res.zip ...
[echo] building unsigned.apk ...
[exec]
[exec] THIS TOOL IS DEPRECATED. See --help for more information.
[exec]
[echo] signing the unsigned apk to final product apk ...
[exec]
[exec] 警告:
[exec] 签名者证书将在六个月内过期。
[echo] done.
BUILD SUCCESSFUL
Total time: 9 seconds
E:\workspace\Hello8>
因为我在用keytool -genkey 的时候没有指定有效期,默认就是六个月。这在实际使用的时候,肯定是要指定时长的,比如10年20年之类的。
之所以,我能够这么快就能成功的使用ant编译android应用,全都是下面这篇文章的功能(不过我修改了一下build.xml才运行通过):
http://www.cnblogs.com/qianxudetianxia/archive/2012/07/04/2573687.html#commentform
在此,对该文的作者表示感谢和敬意!