一、工作需求:
ANT工具,一个命令发布整个工程。包括的工作:
(1)压缩Web工程中的CSS和JS代码,导出到Android工程的assets目录下;
(2)编译工程的库工程,另外一个Android工程;
(3)编译Android工程,打包,混淆,签名,发布。
二、工作中常见问题:
(1)生成build.xml文件:
E:\MyCode\Eclipse\android\iSettings>android list targets | moreAvailable Android targets:----------id: 1 or "android-9" Name: Android 2.3.1 Type: Platform API level: 9 Revision: 2 Skins: HVGA, QVGA, WQVGA400, WQVGA432, WVGA800 (default), WVGA854 Tag/ABIs : default/armeabi----------id: 2 or "android-15" Name: Android 4.0.3 Type: Platform API level: 15 Revision: 3 Skins: WXGA800, WVGA854, WXGA720, WQVGA400, WSVGA, WVGA800 (default), QVGA, WQVGA432, HVGA Tag/ABIs : default/armeabi-v7a----------id: 3 or "android-17" Name: Android 4.2.2 Type: Platform API level: 17 Revision: 2-- More --E:\MyCode\Eclipse\android\iSettings>android update project -p . -t 2Updated project.propertiesUpdated local.propertiesUpdated file E:\MyCode\Eclipse\android\iSettings\proguard-project.txtIt seems that there are sub-projects. If you want to update themplease use the --subprojects parameter.
命令android update project -p . -t 2中,点号“.”代表当前目录,“2”代表使用API level: 15的环境。
此时生成了3个文件:build.xml,local.properties,proguard-project.txt
(2)sdk.dir,如何配置:
查看local.properties,里面只有一行内容:
sdk.dir=D:\\OpenSource\\android-sdk-windows
当它被上传到到SVN服务器被别人下载后,别人将使用不了这个环境。查看build.xml,里面有如下描述:
<!-- if sdk.dir was not set from one of the property file, then get it from the ANDROID_HOME env var. This must be done before we load project.properties since the proguard config can use sdk.dir --> <property environment="env" /> <condition property="sdk.dir" value="${env.ANDROID_HOME}"> <isset property="env.ANDROID_HOME" /> </condition>
所以需要做的是:
1、配置环境变量ANDROID_HOME:
E:\MyCode\Eclipse\android\iSettings>echo %ANDROID_HOME%D:\OpenSource\android-sdk-windows
2、删除local.properties
我想阅读本文的读者都会配置环境变量吧。
(3)编译错误的处理:
参考代码%ANDROID_HOME%/tools/ant/build.xml:
<javac encoding="${java.encoding}" source="${java.source}" target="${java.target}" debug="true" extdirs="" includeantruntime="false" destdir="${out.classes.absolute.dir}" bootclasspathref="project.target.class.path" verbose="${verbose}" classpathref="project.javac.classpath" fork="${need.javac.fork}"> <src path="${source.absolute.dir}" /> <src path="${gen.absolute.dir}" /> <compilerarg line="${java.compilerargs}" /> </javac>
1)编译中的-Xlint:deprecation -Xlint:unchecked等警告:
在build.xml中增加以下语句来设置java.compilerargs属性:
<property name="java.compilerargs" value="-Xlint:deprecation -Xlint:unchecked" />
2)找不到包错误:
1>jar库文件不在libs目录下:
设置属性project.all.jars.path:
<target name="-pre-compile"> <echo>!!!!!!!!!!!! -pre-compile !!!!!!!!!!!!!</echo> <echo message="JARPATH=${toString:project.all.jars.path}"></echo> <path id="spark.lib"> <fileset dir="BasicAndroidSourceLibrary/framework_intermediates/" includes="*.jar" /> <fileset dir="BasicAndroidSourceLibrary/android-common_intermediates/" includes="*.jar" /> <fileset dir="BasicAndroidSourceLibrary/core/" includes="*.jar" /> <fileset dir="libs/" includes="*.jar" /> </path> <path id="project.all.jars.path"> <path refid="spark.lib"></path> <path path="${toString:project.all.jars.path}"></path> </path> <echo message="JARPATH=${toString:project.all.jars.path}"></echo> </target>
2>jar库文件是有顺序要求的:
设置属性project.target.class.path:
<target name="-pre-compile"> <echo>!!!!!!!!!!!! -pre-compile !!!!!!!!!!!!!</echo> <echo message="bootclasspath=${toString:project.target.class.path}"></echo> <path id="spark.lib"> <fileset dir="BasicAndroidSourceLibrary/framework_intermediates/" includes="*.jar" /> <fileset dir="BasicAndroidSourceLibrary/android-common_intermediates/" includes="*.jar" /> <fileset dir="BasicAndroidSourceLibrary/core/" includes="*.jar" /> <fileset dir="libs/" includes="*.jar" /> <path path="${toString:project.all.jars.path}"></path> <path path="${toString:project.target.class.path}"></path> </path> <path id="project.target.class.path"> <path refid="spark.lib"></path> <path path="${toString:project.target.class.path}"></path> </path> <echo message="bootclasspath=${toString:project.target.class.path}"></echo> </target>
3>说明:
为什么会有有顺序要求的jar库文件?因为程序员要优先使用某个库文件里面的类,当没有的时候才使用其他的库里面的类。
有个很常见的应用场景,码农们编译了Android源代码,[email protected],但是Android SDK里面没有那个函数,怎么办?只有使用Android 源代码编译出来的库文件。
具体来说主要是以下文件:
#见http://blog.csdn.net/xixinyan/article/details/6942074所描述out\target\common\obj\JAVA_LIBRARIES\framework_intermediates/classes.jar out\target\common\obj\JAVA_LIBRARIES\android-common_intermediates\ classes.jar ;
如上2>就是使用了这种Android SDK作为外部库,然后排了顺序后交给javac编译工程源码的。
(4)签名配置:
生成keystore文件后,配置属性key.store和key.alias就行了
<property name="key.store" value="???.keystore" /><property name="key.alias" value="???" />
(5)其他:
ant cleanant debugant releaseant installdant installr