创建项目
创建一个项目,命名为HelloWorld:
E:\soft\adt-bundle-windows-x86-20131030\adt-bundle-windows-x86-20131030\sdk\tools>android create project -n HelloWorld -t 9 -p E:/HelloWorld -k com.zzj.helloworld -a Helloworld
上面的命令中,-n指定项目的名称,-t指定android平台,-p指定项目存放路径,-k指定项目包名,-a指定Activity名称。
使用命令 android list targets 可查看可选的android平台:
E:\soft\adt-bundle-windows-x86-20131030\adt-bundle-windows-x86-20131030\sdk\tools>android list targets
Available Android targets:
----------
id: 1 or "android-4"
Name: Android 1.6
Type: Platform
API level: 4
Revision: 3
Skins: HVGA, QVGA, WVGA800 (default), WVGA854
ABIs : armeabi
----------
id: 2 or "android-8"
Name: Android 2.2
Type: Platform
API level: 8
Revision: 3
Skins: HVGA, QVGA, WQVGA400, WQVGA432, WVGA800 (default), WVGA854
ABIs : armeabi
----------
id: 3 or "android-10"
Name: Android 2.3.3
Type: Platform
API level: 10
Revision: 2
Skins: HVGA, QVGA, WQVGA400, WQVGA432, WVGA800 (default), WVGA854
ABIs : armeabi, x86
----------
id: 4 or "Google Inc.:Google APIs:10"
Name: Google APIs
Type: Add-On
Vendor: Google Inc.
Revision: 2
Description: Android + Google APIs
Based on Android 2.3.3 (API level 10)
Libraries:
* com.android.future.usb.accessory (usb.jar)
API for USB Accessories
* com.google.android.maps (maps.jar)
API for Google Maps
Skins: WVGA854, WQVGA400, HVGA, WQVGA432, WVGA800 (default), QVGA
ABIs : armeabi
----------
id: 5 or "android-14"
Name: Android 4.0
Type: Platform
API level: 14
Revision: 3
Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, W
XGA720, WXGA800
ABIs : armeabi-v7a
----------
id: 6 or "Google Inc.:Google APIs:14"
Name: Google APIs
Type: Add-On
Vendor: Google Inc.
Revision: 2
Description: Android + Google APIs
Based on Android 4.0 (API level 14)
Libraries:
* com.android.future.usb.accessory (usb.jar)
API for USB Accessories
* com.google.android.maps (maps.jar)
API for Google Maps
Skins: WVGA854, WQVGA400, WSVGA, WXGA720, HVGA, WQVGA432, WVGA800 (default)
, QVGA, WXGA800
ABIs : armeabi-v7a
----------
id: 7 or "android-16"
Name: Android 4.1.2
Type: Platform
API level: 16
Revision: 4
Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, W
XGA720, WXGA800, WXGA800-7in
ABIs : armeabi-v7a
----------
id: 8 or "Google Inc.:Google APIs:16"
Name: Google APIs
Type: Add-On
Vendor: Google Inc.
Revision: 3
Description: Android + Google APIs
Based on Android 4.1.2 (API level 16)
Libraries:
* com.google.android.media.effects (effects.jar)
Collection of video effects
* com.android.future.usb.accessory (usb.jar)
API for USB Accessories
* com.google.android.maps (maps.jar)
API for Google Maps
Skins: WVGA854, WQVGA400, WSVGA, WXGA800-7in, WXGA720, HVGA, WQVGA432, WVGA
800 (default), QVGA, WXGA800
ABIs : armeabi-v7a
----------
id: 9 or "android-17"
Name: Android 4.2.2
Type: Platform
API level: 17
Revision: 2
Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, W
XGA720, WXGA800, WXGA800-7in
ABIs : armeabi-v7a, mips, x86
----------
id: 10 or "Google Inc.:Google APIs:17"
Name: Google APIs
Type: Add-On
Vendor: Google Inc.
Revision: 3
Description: Android + Google APIs
Based on Android 4.2.2 (API level 17)
Libraries:
* com.google.android.media.effects (effects.jar)
Collection of video effects
* com.android.future.usb.accessory (usb.jar)
API for USB Accessories
* com.google.android.maps (maps.jar)
API for Google Maps
Skins: WVGA854, WQVGA400, WSVGA, WXGA800-7in, WXGA720, HVGA, WQVGA432, WVGA
800 (default), QVGA, WXGA800
ABIs : armeabi-v7a
----------
id: 11 or "android-19"
Name: Android 4.4.2
Type: Platform
API level: 19
Revision: 3
Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, W
XGA720, WXGA800, WXGA800-7in
ABIs : armeabi-v7a
----------
id: 12 or "Google Inc.:Glass Development Kit Preview:19"
Name: Glass Development Kit Preview
Type: Add-On
Vendor: Google Inc.
Revision: 8
Description: Preview of the Glass Development Kit
Based on Android 4.4.2 (API level 19)
Libraries:
* com.google.android.glass (gdk.jar)
APIs for Glass Development Kit Preview
Skins: WVGA854, WQVGA400, WSVGA, WXGA800-7in, WXGA720, HVGA, WQVGA432, WVGA
800 (default), QVGA, WXGA800
ABIs : no ABIs.
----------
id: 13 or "Google Inc.:Google APIs:19"
Name: Google APIs
Type: Add-On
Vendor: Google Inc.
Revision: 5
Description: Android + Google APIs
Based on Android 4.4.2 (API level 19)
Libraries:
* com.google.android.media.effects (effects.jar)
Collection of video effects
* com.android.future.usb.accessory (usb.jar)
API for USB Accessories
* com.google.android.maps (maps.jar)
API for Google Maps
Skins: WVGA854, WQVGA400, WSVGA, WXGA800-7in, WXGA720, HVGA, WQVGA432, WVGA
800 (default), QVGA, WXGA800
ABIs : armeabi-v7a
----------
id: 14 or "Google Inc.:Google APIs (x86 System Image):19"
Name: Google APIs (x86 System Image)
Type: Add-On
Vendor: Google Inc.
Revision: 5
Description: Android x86 + Google APIs
Based on Android 4.4.2 (API level 19)
Libraries:
* com.google.android.media.effects (effects.jar)
Collection of video effects
* com.android.future.usb.accessory (usb.jar)
API for USB Accessories
* com.google.android.maps (maps.jar)
API for Google Maps
Skins: WVGA854, WQVGA400, WSVGA, WXGA800-7in, WXGA720, HVGA, WQVGA432, WVGA
800 (default), QVGA, WXGA800
ABIs : x86
可以看到,9对应的target是android-17,也就是Android 4.2.2。
项目目录结构如下图:
生成了ant.properties和build.xml两个文件,可见可以用ant构建该项目。
构建、打包、安装以及卸载APK
注:先确认安装了ant。
进入到项目的根目录下,运行命令ant:
E:\HelloWorld>ant
Buildfile: E:\HelloWorld\build.xml
help:
[echo] Android Ant Build. Available targets:
[echo] help: Displays this help.
[echo] clean: Removes output files created by other targets.
[echo] This calls the same target on all dependent projects.
[echo] Use 'ant nodeps clean' to only clean the local project
[echo] debug: Builds the application and signs it with a debug key.
[echo] The 'nodeps' target can be used to only build the
[echo] current project and ignore the libraries using:
[echo] 'ant nodeps debug'
[echo] release: Builds the application. The generated apk file must be
[echo] signed before it is published.
[echo] The 'nodeps' target can be used to only build the
[echo] current project and ignore the libraries using:
[echo] 'ant nodeps release'
[echo] instrument:Builds an instrumented package and signs it with a
[echo] debug key.
[echo] test: Runs the tests. Project must be a test project and
[echo] must have been built. Typical usage would be:
[echo] ant [emma] debug install test
[echo] emma: Transiently enables code coverage for subsequent
[echo] targets.
[echo] install: Installs the newly build package. Must either be used
[echo] in conjunction with a build target (debug/release/
[echo] instrument) or with the proper suffix indicating
[echo] which package to install (see below).
[echo] If the application was previously installed, the
[echo] application is reinstalled if the signature matches.
[echo] installd: Installs (only) the debug package.
[echo] installr: Installs (only) the release package.
[echo] installi: Installs (only) the instrumented package.
[echo] installt: Installs (only) the test and tested packages (unless
[echo] nodeps is used as well.
[echo] uninstall: Uninstalls the application from a running emulator or
[echo] device. Also uninstall tested package if applicable
[echo] unless 'nodeps' is used as well.
BUILD SUCCESSFUL
Total time: 0 seconds
从上可以看出,android项目提供的build.xml文件包含如下常用命令生成target:
1.clean。清除项目生成的内容,恢复原来的样子。
2.debug。生成一个调试用的APK包,使用debug key进行签名。
3.release。打包一个发布用的APK(需要提供签名,否则不会生成Apk文件)。
4.test。运行测试。
5.install。将Apk安装到模拟器上。
6.uninstall。从模拟器上卸载应用。
下面我们生成一个调试用的APK。
E:\HelloWorld>ant debug
执行该命令后,在项目的根目录下生成了gen子目录,里面包含了R.java文件;在bin目录下生成Apk文件:
执行安装命令:
E:\HelloWorld>ant debug install
部分输出信息:
debug:
install:
[echo] Installing E:\HelloWorld\bin\HelloWorld-debug.apk onto default emulator or device...
[exec] pkg: /data/local/tmp/HelloWorld-debug.apk
[exec] Success
[exec] 2316 KB/s (37068 bytes in 0.015s)
BUILD SUCCESSFUL
Total time: 10 seconds
卸载:
E:\HelloWorld>ant uninstall
Buildfile: E:\HelloWorld\build.xml
-check-env:
[checkenv] Android SDK Tools Revision 22.3.0
[checkenv] Installed at E:\soft\adt-bundle-windows-x86-20131030\adt-bundle-windows-x86-20131030\sdk
-setup:
[echo] Project Name: HelloWorld
[gettype] Project Type: Application
uninstall:
[echo] Uninstalling com.zzj.helloworld from the default emulator or device...
[exec] Success
BUILD SUCCESSFUL
Total time: 2 seconds