安装完 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:\TestAndroidCreated directory C:\TestAndroid\src\cc\unmi\android\testAdded file c:\TestAndroid\src\cc\unmi\android\test\HelloAndroid.javaCreated directory C:\TestAndroid\resCreated directory C:\TestAndroid\binCreated directory C:\TestAndroid\libsCreated directory C:\TestAndroid\res\valuesAdded file c:\TestAndroid\res\values\strings.xmlCreated directory C:\TestAndroid\res\layoutAdded file c:\TestAndroid\res\layout\main.xmlCreated directory C:\TestAndroid\res\drawable-hdpiCreated directory C:\TestAndroid\res\drawable-mdpiCreated directory C:\TestAndroid\res\drawable-ldpiAdded file c:\TestAndroid\AndroidManifest.xmlAdded file c:\TestAndroid\build.xmlAdded file c:\TestAndroid\proguard.cfg

我们知道生成了什么文件,目录下存在 build.xml 文件。这时候,还没 gen 目录,bin 目录是空的。

现在如果已配置好了 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.apkinstall:[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为自己的签名。

更多相关文章

  1. Android(安卓)异步加载解决方案
  2. Android小白成长之初级篇:NDK配置
  3. 编译移植android 2.3到tiny210
  4. Android(安卓)Makefile分析
  5. Android(安卓)开发艺术探索笔记(十五) 之 Android(安卓)的线程和线
  6. Android处理异步耗时任务,AsyncTask使用教程
  7. Gradle 构建 android 应用常见问题解决指南
  8. ASM (Android(安卓)Screen Monitor) Android真机抓屏
  9. Android的源代码结构

随机推荐

  1. ES5、ES6 如何实现继承
  2. 谈谈 CSS 预处理器
  3. Spring认证 - Bean 范围教程
  4. RabbitMQ 高可用之如何确保消息成功消费
  5. PHP技术交流微信群
  6. PHP CURL/JSON应用
  7. ELK 安装配置
  8. 单片机学习笔记,三个步骤教你学!
  9. 如何把C++的源程序改写成C语言
  10. PHP中的国际化日历类