Android 中的ant文件说明

Android本身是支持ant打包项目的,并且SDK中自带一个build.xml文件。
通过该文件,可以对文件进行编译、打包、安装等。并且支持多种方式打包,如debug或者release。
一般的,可以按照如下方法进行操作:


首先创建一个Android工程。
工程创建好后,需要为工程添加ant支持,即创建相应的build.xml文件。
这个文件不需要用户自己创建,只需要执行如下命令即可:
<sdk>/tools/androidupdateproject-p<project>-t<target>


其中:
sdk为sdk的安装目录,其下的tools/android是我们要使用的命令。
project为项目目录。
target为项目所使用的android的targetid,也就是项目对应的android的版本。


可以将sdk的tools目录添加到环境变量~/.bashrc文件中,重启后生效,以后可以直接使用命令了。
可以通过执行以下命令查看当前sdk中所包含的target以及相应id:
androidlisttargets


执行完androidupdateproject命令后,会在项目的根目录下生成相应的ant文件。
主要有build.xml、ant.properties、local.properties三个文件。
如果没有ant.properties文件,可以自己手动添加。


其中,build.xml文件是进行antbuild的主要文件,它引用了其它的文件。
在local.properties文件中定义了sdk.dir属性,也就是当前使用的sdk的目录。
在ant.properties文件中,可以定义自己的一些属性,或者重定义一些属性。
当然,这两个属性文件可有可无,直接定义到build.xml中也没有关系。


在build.xml中的最后,引用了sdk目录下的tools/ant/build.xml文件。
这个是sdk默认的build文件,可以将其内容直接拷贝过来,也可以保持当前的引用方式。
项目下的build.xml默认执行的target是help,运行后可以看到相关的帮助信息。
通过帮助信息,可以看到其它可用的target。
一般的,我们需要修改为debug或者release。
修改好之后,再运行build.xml文件,就会执行我们需要的编译过程了。
build.xml文件的执行方法为:右键->RunAs->AntBuild。


如果是debug版的话,默认会使用debug方式签名。
如果是release版的话,需要指定相应的keystore以及私钥。
否则,最后只能生成一个没有签名的apk文件。


设定私钥库的方法是,在ant.properties文件中,添加如下内容:
key.store=<keystore>
key.alias=<key>
key.store.password=<keystorepwd>
key.alias.password=<keypwd>


其中:
keystore为私钥库文件。
key为签名需要使用的私钥。
key.store.password为私钥库的密码。
key.alias.password为私钥的密码。


两个密码相关的属性也可以不添加,而只添加私钥库及私钥。
这样的话,执行时会依次弹出相应的输入框,提示用户输入相应的密码。
这样虽然相对繁琐,但是由于没有明文指定密码,对私钥库的保护会更好一些。


如果项目引用了第三方库的话,只需要在项目根目录创建libs文件夹并将其放入即可。
如果是jar库,直接放到libs目录下;如果是so库,需要放到libs的名为armeabi的子目录下。
也可以通过设定相应的属性来指定第三方库的位置,其默认都是libs文件夹。
jar.libs.dir为jar类型库所在的目录。
native.libs.absolute.dir为so类型库所在的目录,即armeabi的父目录。


如果项目包含了jni代码,希望在打包时自动重新编译so库,可以修改build.xml文件。
修改方法为,在引用sdk的build.xml文件之前添加如下target:
<targetname="-pre-build"depends="-ndk-build">
</target>
<targetname="-ndk-build">
<execexecutable="ndk-build"failοnerrοr="true">
<argvalue="clean"/>
</exec>
<execexecutable="ndk-build"failοnerrοr="true"/>
</target>


ndk-build为ndk提供的命令,需要将ndk安装目录添加到环境变量中,添加方法同sdk/tools。
生成的so默认会放到libs/armeabi目录下。


使用默认的build.xml打包apk时,已经包含了混淆、签名、对齐优化等相关过程。
如果是使用release的方式,最后生成的apk文件已经可以直接发布了。


遗留问题:
目前采用这种方法生成的APK,虽然已经被签名了,但是,安装时错误,提示未签名。
查看APK包中的签名文件,不是默认的CERT.*,而是<key>.*。
然后,即使将名称修改成CERT.*,程序仍然不能正常安装。
如果导出debug版本,则不会有这个问题。
用ADT插件导出签名APK,也不会有这个问题。


解决方法:
产生此问题的根本原因是JDK1.7造成的,只有运行Ant使用jre1.7的版本时,才会发生该问题。
可以通过设置运行build.xml文件时使用的jre版本来解决,具体方法是:
选中build.xml->右键->RunAs->ExternalToolsConfigurations,
在右侧区域选中JRE标签页,可以看到对jre设定有三个选项:
RuninthesameJREastheworkspace使用与workspace相同版本的jre。
Executionenvironment根据相关环境选择一个jre版本。
SeparateJRE使用一个已经安装的jre的当前版本。

一般项目的jre都会设定为1.7以下的版本,所以建议选择第一个,使其与项目设定保持一致即可。




如何使用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

使用antdebug来生成用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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值