在java编译那些事儿中提到了用ant去编译Java项目,今天扩展到用它来构建Android项目,其实道理是相通的,变化的只是使用的形式。ant构建相比IDE的好处是多个子项目使用自定义jar包时,ant可以更好的完成自动化构建,一个命令就搞定整个项目的编译而不用手工的导出jar包然后再将其放到指定目录。这就是高效的构建工具所标榜的。
先来说说ant在Linux下的安装(那篇文章写的太过简单,其实也是很简单的,别看mannul中写的那么复杂)。
[linc@localhost ant]$ cp ~/Downloads/apache-ant-1.9.4-bin.zip . [linc@localhost ant]$ unzip apache-ant-1.9.4-bin.zip [linc@localhost ant]$ cd apache-ant-1.9.4 [linc@localhost apache-ant-1.9.4]$ ls bin fetch.xml INSTALL lib manual README etc get-m2.xml KEYS LICENSE NOTICE WHATSNEW
15 export ANT_HOME=/home/linc/dev/ant/apache-ant-1.9.4 16 export JAVA_HOME=/usr/java/jdk1.6.0_25 17 export PATH=${PATH}:${ANT_HOME}/bin为了让修改立即生效,执行一下 source ~/.bashrc
[linc@localhost apache-ant-1.9.4]$ ant Buildfile: build.xml does not exist! Build failed
[linc@localhost BallGame]$ android -h update project Usage: android [global options] update project [action options] Global options: -h --help : Help on a specific command. -v --verbose : Verbose mode, shows errors, warnings and all messages. --clear-cache: Clear the SDK Manager repository manifest cache. -s --silent : Silent mode, shows errors only. Action "update project": Updates an Android project (must already have an AndroidManifest.xml). Options: -l --library : Directory of an Android library to add, relative to this project‘s directory. -p --path : The project‘s directory. [required] -n --name : Project name. -t --target : Target ID to set for the project. -s --subprojects: Also updates any projects in sub-folders, such as test projects.
除了参数-p路径是必须的,其他参数都可以不带。但是建议名称-n要加上,否则会默认以Activity名称来命名。-t可以不做修改,项目的Target Id其实就是指Android API的版本,我们可以查看项目的project.properties,以此为基准,如下:
[linc@localhost BallGame]$ tail project.properties ... # Project target. target=android-4
[linc@localhost BallGame]$ android update project -p . Updated local.properties No project name specified, using Activity name ‘MainActivity‘. If you wish to change it, edit the first line of build.xml. Added file ./build.xml Added file ./proguard-project.txt
也可以指定更全的参数:
[linc@localhost BallGame]$ android update project -p . -n BallGame -t android-4 Updated project.properties Updated local.properties Updated file ./build.xml Updated file ./proguard-project.txt至此,build.xml在我们不动一枪的情况下搞定了。
Android应用开发高效工具集1---ant构建简单Android项目
原文地址:http://blog.csdn.net/lincyang/article/details/40950153