首先把android sdk下的tools目录加到系统path环境变量里, 要么就得直接指定android.bat的绝对路径

对于一个新项目, 可以用这个命令创建需要的ant编译环境(可以看到android项目默认的文件结构)

android create project -k com.foo -a Test1 -t android- -p d:\temp

如果是已经存在的项目, 对主项目和子项目都运行

项目目录> android update project -s -p . -t android-

-s 是因为带子项目

-t 是指定目标版本, 版本不对会导致编译失败

其中

build.xml 是ant任务文件, 基本不用修改

custom_rules.xml 对于需要自行配置的编译任务, 写到这个文件里, 会被build.xml加载

ant.properties ant运行中涉及的变量写到这里

local.properties 里面设定了sdk.dir的路径, 不用修改

project.properties 设定了编译目标和项目类型, 如果有子项目的话, 还有子项目的路径, 不用修改

然后执行下面的命令就进行编译了

项目目录>D:\apache-ant-1.8.\bin\ant.bat clean
项目目录>D:\apache-ant-1.8.\bin\ant.bat release

根据中途报的错, 再做调整

如果子项目编译出错, 可以分别在子项目目录下运行ant clean 和 ant release, 直到排除错误

两个相似的custom_rules.xml 例子

<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules"> <property name="generated.dir" value="gen" />
<property name="generated.absolute.dir" location="${generated.dir}" />
<property name="java.compilerargs" value="-s &apos;${generated.absolute.dir}&apos;" /> <target name="-pre-compile">
<mkdir dir="${generated.absolute.dir}" />
</target> <target name="-compile" depends="-build-setup, -pre-build, -code-gen, -pre-compile">
<do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">
<!-- merge the project's own classpath and the tested project's classpath -->
<path id="project.javac.classpath">
<path refid="project.all.jars.path" />
<path refid="tested.project.classpath" />
<fileset dir="compile-libs" includes="*.jar"/>
</path>
<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> <!-- if the project is instrumented, intrument the classes -->
<if condition="${build.is.instrumented}">
<then>
<echo level="info">Instrumenting classes from ${out.absolute.dir}/classes...</echo> <!-- build the filter to remove R, Manifest, BuildConfig -->
<getemmafilter
appPackage="${project.app.package}"
libraryPackagesRefId="project.library.packages"
filterOut="emma.default.filter"/> <!-- define where the .em file is going. This may have been
setup already if this is a library -->
<property name="emma.coverage.absolute.file" location="${out.absolute.dir}/coverage.em" /> <!-- It only instruments class files, not any external libs -->
<emma enabled="true">
<instr verbosity="${verbosity}"
mode="overwrite"
instrpath="${out.absolute.dir}/classes"
outdir="${out.absolute.dir}/classes"
metadatafile="${emma.coverage.absolute.file}">
<filter excludes="${emma.default.filter}" />
<filter value="${emma.filter}" />
</instr>
</emma>
</then>
</if> <!-- if the project is a library then we generate a jar file -->
<if condition="${project.is.library}">
<then>
<echo level="info">Creating library output jar file...</echo>
<property name="out.library.jar.file" location="${out.absolute.dir}/classes.jar" />
<if>
<condition>
<length string="${android.package.excludes}" trim="true" when="greater" length="0" />
</condition>
<then>
<echo level="info">Custom jar packaging exclusion: ${android.package.excludes}</echo>
</then>
</if> <propertybyreplace name="project.app.package.path" input="${project.app.package}" replace="." with="/" /> <jar destfile="${out.library.jar.file}">
<fileset dir="${out.classes.absolute.dir}"
includes="**/*.class"
excludes="${project.app.package.path}/R.class ${project.app.package.path}/R$*.class ${project.app.package.path}/BuildConfig.class"/>
<fileset dir="${source.absolute.dir}" excludes="**/*.java ${android.package.excludes}" />
</jar>
</then>
</if> </do-only-if-manifest-hasCode>
</target> </project>

另一个

<?xml version="1.0" encoding="UTF-8"?>
<project name="app_project" default="help">
<property name="generated.dir" value=".apt_generated" />
<property name="generated.absolute.dir" location="${generated.dir}" />
<property name="java.compilerargs" value="-s &apos;${generated.absolute.dir}&apos;" /> <target name="-pre-compile">
<mkdir dir="${generated.absolute.dir}" />
</target> <!-- Compiles this project's .java files into .class files. -->
<target name="-compile" depends="-pre-build, -build-setup, -code-gen, -pre-compile">
<do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">
<!-- merge the project's own classpath and the tested project's classpath -->
<path id="project.javac.classpath">
<path refid="project.all.jars.path" />
<path refid="tested.project.classpath" />
<path path="${java.compiler.classpath}" />
<fileset dir="compile-libs" includes="*.jar" />
</path>
<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> <!-- if the project is instrumented, intrument the classes -->
<if condition="${build.is.instrumented}">
<then>
<echo level="info">Instrumenting classes from ${out.absolute.dir}/classes...</echo> <!-- build the filter to remove R, Manifest, BuildConfig -->
<getemmafilter
appPackage="${project.app.package}"
libraryPackagesRefId="project.library.packages"
filterOut="emma.default.filter"/> <!-- define where the .em file is going. This may have been setup already if this is a library -->
<property name="emma.coverage.absolute.file" location="${out.absolute.dir}/coverage.em" /> <!-- It only instruments class files, not any external libs -->
<emma enabled="true">
<instr verbosity="${verbosity}"
mode="overwrite"
instrpath="${out.absolute.dir}/classes"
outdir="${out.absolute.dir}/classes"
metadatafile="${emma.coverage.absolute.file}">
<filter excludes="${emma.default.filter}" />
<filter value="${emma.filter}" />
</instr>
</emma>
</then>
</if> <!-- if the project is a library then we generate a jar file -->
<if condition="${project.is.library}">
<then>
<echo level="info">Creating library output jar file...</echo>
<property name="out.library.jar.file" location="${out.absolute.dir}/classes.jar" />
<if>
<condition>
<length string="${android.package.excludes}" trim="true" when="greater" length="0" />
</condition>
<then>
<echo level="info">Custom jar packaging exclusion: ${android.package.excludes}</echo>
</then>
</if> <propertybyreplace name="project.app.package.path" input="${project.app.package}" replace="." with="/" /> <jar destfile="${out.library.jar.file}">
<fileset dir="${out.classes.absolute.dir}"
includes="**/*.class"
excludes="${project.app.package.path}/R.class ${project.app.package.path}/R$*.class ${project.app.package.path}/BuildConfig.class"/>
<fileset dir="${source.absolute.dir}" excludes="**/*.java ${android.package.excludes}" />
</jar>
</then>
</if> </do-only-if-manifest-hasCode>
</target>
</project>

ant.properties的例子, 和上面的xml是对应的

key.store=D:/workAndroid/files/android_key.store
key.alias=alia
key.store.password=
key.alias.password=
java.encoding=UTF-
java.target=
java.source=

在生成的apk中使用版本号文件名, 而不是用默认的 xxx-release.apk

http://jeffreysambells.com/2013/02/14/build-your-android-apk-with-the-manifest-version-number

    <target name="-set-release-mode"
depends="rename-release-with-version-number,android_rules.-set-release-mode">
<echo message="target: ${build.target}"></echo>
</target> <target name="rename-release-with-version-number">
<xmlproperty file="AndroidManifest.xml" prefix="themanifest" collapseAttributes="true"/>
<!-- see ${sdk.dir}/tools/ant/build.xml -set-release-mode -->
<property name="out.packaged.file"
location="${out.absolute.dir}/${ant.project.name}-${themanifest.manifest.android:versionName}-release-unsigned.apk" />
<property name="out.final.file"
location="${out.absolute.dir}/${ant.project.name}-${themanifest.manifest.android:versionName}-release.apk" />
</target>

The -set-release-mode target overrides the same target in ${sdk.dir}/tools/ant/build.xml where the out.final.file file name is originally defined. To add the version number, I override -set-release-mode by calling my rename-release-with-version-number target in the dependencies and then calling the original android_rules.-set-release-mode to finish any other setup there.

The rename-release-with-version-number target simply reads in the manifest and adds the version number to both out.final.file and out.packaged.file.

Now the build APK is ProjectName-version-release.apk and it’s automatically used by the rest of the ant build process without clumsy file renaming.

最新文章

  1. 用vue.js学习es6(三):数组、对象和函数的解构
  2. Java集合系列:-----------08HashMap的底层实现
  3. 使用quartz 定时任务
  4. 兼容,原来在这里就已经開始--------Day34
  5. SQL Server解决死锁问题
  6. css的小demo
  7. linux centos 配置 svn 服务器
  8. js下读取input中的value值
  9. 改写BlogEngine.NET头像上传实现方式(使用baidu.flash.avatarMaker)
  10. 从Freelancer的热门Skill看看你应该学什么?
  11. python入门篇
  12. async ,await 有图有真相
  13. JavaSE_坚持读源码_HashMap对象_get_Java1.7
  14. Class 泛型
  15. 锐捷客户端下虚拟机VMware无法联网的问题
  16. Sprint第二个计划
  17. STM32 Seminar 2007 -- Timer
  18. Java消息队列三道面试题详解!
  19. HDUOJ-----2175取(m堆)石子游戏
  20. mybatis update数据时无异常但没更新成功;update异常时如数据超出大小限制,造成死锁

热门文章

  1. self&amp;super
  2. iOS 学习 - 10下载(3) NSURLSession 音乐 篇
  3. CRM项目经验总结-从DAO层到链接数据池
  4. MySql链接字符串 各种程序连接大合集(包括asp.net,c#,等等)
  5. db2简单语句记录
  6. 面试题整理:SQL(二)
  7. Java堆、栈和常量池
  8. 定义类Human,具有若干属性和功能;定义其子类Man、Woman; 在主类Test中分别创建子类、父类和上转型对象,并测试其特性。
  9. js日期校验
  10. 烂泥:学习Nagios(二):Nagios配置