====== 如何使用ant编译Android工程 ====== 很早就知道Android支持命令行方式创建和编译工程,但大多数情况下我们都是直接使用Eclipse+ADT的方式来处理的。 最近做的项目就是使用ant来编译的,之前也没研究过ant怎么样,所以一直不知道怎么用ant来编译Android工程。 今天在Gentoo下试了下,没有做任何设置ant就可以用了,应该是以前我不知不觉就安装好了。 ===== 安装ant ===== 如果是在Windows下,需要到网站上去下载:http://ant.apache.org/bindownload.cgi 再设置好环境变量,这个就不说了。 ===== 使用android命令创建工程 ===== ant编译需要依赖于build.xml文件,如果是用Eclipse生成的工程,是没有这个文件的。显然Google支持ant编译自然不会让我们去手动生成的文件了,她为我们准备了android命令: 输入android create project查看如何创建一个Android工程: pjq@gentoo-pjq ~/android/workspace $ android create project Error: The parameters --target, --path, --package, --activity must be defined for action 'create project' Usage: android [global options] action [action options] Global options: -v --verbose Verbose mode: errors, warnings and informational messages are printed. -h --help Help on a specific command. -s --silent Silent mode: only errors are printed out. Action "create project": Creates a new Android Project. Options: -n --name Project name -t --target Target id of the new project [required] -p --path Location path of new project [required] -k --package Package name [required] -a --activity Activity name [required] 使用命令: android create project -n AndroidAnt -t android-8 -p ./AndroidAnt -k net.impjq.androidant -a HelloAndroidAnt Created project directory: ./AndroidAnt Created directory /home/pjq/android/workspace/AndroidAnt/src/net/impjq/androidant Added file ./AndroidAnt/src/net/impjq/androidant/HelloAndroidAnt.java Created directory /home/pjq/android/workspace/AndroidAnt/res Created directory /home/pjq/android/workspace/AndroidAnt/bin Created directory /home/pjq/android/workspace/AndroidAnt/libs Created directory /home/pjq/android/workspace/AndroidAnt/res/values Added file ./AndroidAnt/res/values/strings.xml Created directory /home/pjq/android/workspace/AndroidAnt/res/layout Added file ./AndroidAnt/res/layout/main.xml Created directory /home/pjq/android/workspace/AndroidAnt/res/drawable-hdpi Created directory /home/pjq/android/workspace/AndroidAnt/res/drawable-mdpi Created directory /home/pjq/android/workspace/AndroidAnt/res/drawable-ldpi Added file ./AndroidAnt/AndroidManifest.xml Added file ./AndroidAnt/build.xml 很明显目录结构和用Eclipse生成的一样,只是多了build.xml,这个就是为了在命令行用ant编译而准备的。 ===== 使用ant编译Android工程 ===== 编译这个新生成的工程 pjq@gentoo-pjq ~/android/workspace $ cd AndroidAnt/ pjq@gentoo-pjq ~/android/workspace/AndroidAnt $ ls AndroidManifest.xml build.xml local.properties bin default.properties res build.properties libs src pjq@gentoo-pjq ~/android/workspace/AndroidAnt $ ant Buildfile: /home/pjq/android/workspace/AndroidAnt/build.xml [setup] Android SDK Tools Revision 6 [setup] Project Target: Android 2.2 [setup] API level: 8 [setup] WARNING: No minSdkVersion value set. Application will install on all Android versions. [setup] Importing rules file: platforms/android-8/ant/ant_rules_r2.xml help: [echo] Android Ant Build. Available targets: [echo] help: Displays this help. [echo] clean: Removes output files created by other targets. [echo] compile: Compiles project's .java files into .class files. [echo] debug: Builds the application and signs it with a debug key. [echo] release: Builds the application. The generated apk file must be [echo] signed before it is published. [echo] install: Installs/reinstalls the debug package onto a running [echo] emulator or device. [echo] If the application was previously installed, the [echo] signatures must match. [echo] uninstall: Uninstalls the application from a running emulator or [echo] device. BUILD SUCCESSFUL Total time: 2 seconds 使用ant debug来生成用debug key签名的APK。 ant debug pjq@gentoo-pjq ~/android/workspace/AndroidAnt $ ant debug Buildfile: /home/pjq/android/workspace/AndroidAnt/build.xml [setup] Android SDK Tools Revision 6 [setup] Project Target: Android 2.2 [setup] API level: 8 [setup] WARNING: No minSdkVersion value set. Application will install on all Android versions. [setup] Importing rules file: platforms/android-8/ant/ant_rules_r2.xml -compile-tested-if-test: -dirs: [echo] Creating output directories if needed... [mkdir] Created dir: /home/pjq/android/workspace/AndroidAnt/gen [mkdir] Created dir: /home/pjq/android/workspace/AndroidAnt/bin/classes -resource-src: [echo] Generating R.java / Manifest.java from the resources... -aidl: [echo] Compiling aidl files into Java classes... compile: [javac] /home/pjq/android/android-sdk-linux_86/platforms/android-8/ant/ant_rules_r2.xml:255: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to /home/pjq/android/workspace/AndroidAnt/bin/classes -dex: [echo] Converting compiled files and external libraries into /home/pjq/android/workspace/AndroidAnt/bin/classes.dex... -package-resources: [echo] Packaging resources [aaptexec] Creating full resource package... -package-debug-sign: [apkbuilder] Creating AndroidAnt-debug-unaligned.apk and signing it with a debug key... [apkbuilder] Using keystore: /home/pjq/.android/debug.keystore debug: [echo] Running zip align on final apk... [echo] Debug Package: /home/pjq/android/workspace/AndroidAnt/bin/AndroidAnt-debug.apk BUILD SUCCESSFUL Total time: 8 seconds 呃,这个过程好熟悉,想起来了,之前TweeQ就是这样编译的。 ===== 安装APK ===== 再使用adb install 安装APK adb install bin/AndroidAnt-debug.apk ====== 参考 ====== *http://zero1.javaeye.com/blog/655983 *http://www.cnmsdn.com/html/201002/1266985517ID857.html *