转自:http://venus585625.iteye.com/blog/1121400
安装完 Android SDK 后可以在命令行下 Android.bat 命令创建一个示例项目,有 Ant 的话还可直接用 Ant 来编译部署到模拟器上运行。
环境准备:
告诉 path 要指向到 Android SDK 目录的 tools 子目录中,如 d:\android-sdk-windows\tools
要用 ant 编译部署的话,再把 ant 的 bin 目录加入到 path 上,如 D:\apache-ant-1.8.2\bin
进到命令行下,执行:
- android create project -k cc.unmi.android.test -n Hello -a HelloAndroid -t 5 -p c:\TestAndroid
注:以上各参数的意义,可参考:http://developer.android.com/guide/developing/projects/projects-cmdline.html
-k 工程包名: cc.unmi.android.test
-n 工程名 : Hello
-a Activity子类名: HelloAndroid
-t 工程使用的平台 Target: 5 ( 基于Android SDK1.6),是执行 android list targets 显示出的 target id 值
-p 工程存储路径: c:\TestAndroid
控制台下输出:
- Created project directory: c:\TestAndroid
- Created directory C:\TestAndroid\src\cc\unmi\android\test
- Added file c:\TestAndroid\src\cc\unmi\android\test\HelloAndroid.java
- Created directory C:\TestAndroid\res
- Created directory C:\TestAndroid\bin
- Created directory C:\TestAndroid\libs
- Created directory C:\TestAndroid\res\values
- Added file c:\TestAndroid\res\values\strings.xml
- Created directory C:\TestAndroid\res\layout
- Added file c:\TestAndroid\res\layout\main.xml
- Created directory C:\TestAndroid\res\drawable-hdpi
- Created directory C:\TestAndroid\res\drawable-mdpi
- Created directory C:\TestAndroid\res\drawable-ldpi
- Added file c:\TestAndroid\AndroidManifest.xml
- Added file c:\TestAndroid\build.xml
- Added file c:\TestAndroid\proguard.cfg
我们知道生成了什么文件,目录下存在 build.xml 文件。这时候,还没 gen 目录,bin 目录是空的。
我们来看下build.xml的内容:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Hello" default="help">
<!-- The local.properties file is created and updated by the 'android'
tool.
It contains the path to the SDK. It should *NOT* be checked into
Version Control Systems. -->
<property file="local.properties" />
<!-- The build.properties file can be created by you and is never touched
by the 'android' tool. This is the place to change some of the
default property values used by the Ant rules.
Here are some properties you may want to change/update:
source.dir
The name of the source directory. Default is 'src'.
out.dir
The name of the output directory. Default is 'bin'.
Properties related to the SDK location or the project target should
be updated using the 'android' tool with the 'update' action.
This file is an integral part of the build system for your
application and should be checked into Version Control Systems.
-->
<property file="build.properties" />
<!-- The default.properties file is created and updated by the 'android'
tool, as well as ADT.
This file is an integral part of the build system for your
application and should be checked into Version Control Systems. -->
<property file="default.properties" />
<!-- Required pre-setup import -->
<import file="${sdk.dir}/tools/ant/pre_setup.xml" />
<!-- extension targets. Uncomment the ones where you want to do custom work
in between standard targets -->
<!--
<target name="-pre-build">
</target>
<target name="-pre-compile">
</target>
[This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir}]
<target name="-post-compile">
</target>
-->
<!-- Execute the Android Setup task that will setup some properties
specific to the target, and import the build rules files.
The rules file is imported from
<SDK>/tools/ant/
Depending on the project type it can be either:
- main_rules.xml
- lib_rules.xml
- test_rules.xml
To customize existing targets, there are two options:
- Customize only one target:
- copy/paste the target into this file, *before* the
<setup> task.
- customize it to your needs.
- Customize the whole script.
- copy/paste the content of the rules files (minus the top node)
into this file, *after* the <setup> task
- disable the import of the rules by changing the setup task
below to <setup import="false" />.
- customize to your needs.
-->
<setup />
</project>
现在如果已配置好了 ant,就可以进到 c:\TestAndroid 目录下,执行 ant help, 显示出可以使用的 ant 的 target 选项:
- 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.
也就是可以用 ant compile, ant release, ant install 等进行编译,打包,部署等。
ant compile 在 bin 下编译出 class 文件,生成 gen 目录及 R.java。
ant release 在 bin 下生成 classes.dex, Hello.ap_ 和 Hello-unsigned.apk。
要是你现在启动了 Android 模拟器,启动模拟器的命令是 emulator -avd avd名。关于创建 avd 的做法这里略去。
那现在可以执行 ant install
ant install 指令最后的过程是:
- -package-debug-sign:
- [apkbuilder] Creating Hello-debug-unaligned.apk and signing it with a debug key...
- debug:
- [echo] Running zip align on final apk...
- [echo] Debug Package: C:\TestAndroid\bin\Hello-debug.apk
- install:
- [echo] Installing C:\TestAndroid\bin\Hello-debug.apk onto default emulator or device...
- [exec] pkg: /data/local/tmp/Hello-debug.apk
- [exec] Success
- [exec] 35 KB/s (13235 bytes in 0.359s)
- BUILD SUCCESSFUL
在 c:\TestAndroid 下新生成了 Hello-debug-unaligned.apk 和 Hello-debug.apk, 然后部署到模拟器上的是 Hello-debug.apk。当然有了 apk,你也可以直接使用 adb install Hello-debug.apk 来安装到模拟器上。当然如果adb root 可以获取到root权限的话,也可以adb push Hello-debug.apk /system/app
执行完指令之后,你就可以在模拟器或者Devices上看到。
如果需要用自己的key进行sign,且在编译的时候
在build.properties里面添加:
key.store=demo.keystore
key.alias=demo.keystore
进行签名。
demo.keystore为自己的签名。