命令行下创建 Android 工程,用 Ant 进行编译部署

本文介绍了如何通过Android SDK和Ant工具在本地环境中快速创建、编译、部署Android应用程序,包括环境准备、命令行操作、使用Ant命令执行编译和部署流程,以及最终在Android模拟器上的安装过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自: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 

进到命令行下,执行: 

Java代码   收藏代码
  1. 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 

控制台下输出: 

Java代码   收藏代码
  1. Created project directory: c:\TestAndroid  
  2. Created directory C:\TestAndroid\src\cc\unmi\android\test  
  3. Added file c:\TestAndroid\src\cc\unmi\android\test\HelloAndroid.java  
  4. Created directory C:\TestAndroid\res  
  5. Created directory C:\TestAndroid\bin  
  6. Created directory C:\TestAndroid\libs  
  7. Created directory C:\TestAndroid\res\values  
  8. Added file c:\TestAndroid\res\values\strings.xml  
  9. Created directory C:\TestAndroid\res\layout  
  10. Added file c:\TestAndroid\res\layout\main.xml  
  11. Created directory C:\TestAndroid\res\drawable-hdpi  
  12. Created directory C:\TestAndroid\res\drawable-mdpi  
  13. Created directory C:\TestAndroid\res\drawable-ldpi  
  14. Added file c:\TestAndroid\AndroidManifest.xml  
  15. Added file c:\TestAndroid\build.xml  
  16. 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>


其中的<setup/>,见上面的粗体部分。


现在如果已配置好了 ant,就可以进到 c:\TestAndroid 目录下,执行 ant help, 显示出可以使用的 ant 的 target 选项: 

Java代码   收藏代码
  1. help:  
  2. [echo] Android Ant Build. Available targets:  
  3. [echo]    help:      Displays this help.  
  4. [echo]    clean:     Removes output files created by other targets.  
  5. [echo]    compile:   Compiles project's .java files into .class files.  
  6. [echo]    debug:     Builds the application and signs it with a debug key.  
  7. [echo]    release:   Builds the application. The generated apk file must be  
  8. [echo]               signed before it is published.  
  9. [echo]    install:   Installs/reinstalls the debug package onto a running  
  10. [echo]               emulator or device.  
  11. [echo]               If the application was previously installed, the  
  12. [echo]               signatures must match.  
  13. [echo]    uninstall: Uninstalls the application from a running emulator or  
  14. [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 指令最后的过程是: 

Java代码   收藏代码
  1. -package-debug-sign:  
  2. [apkbuilder] Creating Hello-debug-unaligned.apk and signing it with a debug key...  
  3.   
  4. debug:  
  5. [echo] Running zip align on final apk...  
  6. [echo] Debug Package: C:\TestAndroid\bin\Hello-debug.apk  
  7.   
  8. install:  
  9. [echo] Installing C:\TestAndroid\bin\Hello-debug.apk onto default emulator or device...  
  10. [exec]     pkg: /data/local/tmp/Hello-debug.apk  
  11. [exec] Success  
  12. [exec] 35 KB/s (13235 bytes in 0.359s)  
  13.   
  14. 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为自己的签名。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值