首先把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.

Ant 命令行编译Android项目的更多相关文章

  1. 一个使用命令行编译Android项目的工具类

    一个使用命令行编译Android项目的工具类 简单介绍 编译apk项目须要使用的几个工具,基本都在sdk中,它们各自是(Windows系统): 1.aapt.exe 资源打包工具 2.android. ...

  2. 命令行编译java项目

    命令行编译java项目 项目名: testproj 目录 src -> cn -> busix -> test bin lib 编译项目 cd testproj javac -d . ...

  3. Windows下使用命令行编译Qt项目(解决DLL丢失问题)

    一.前言 我之前用Qt做了个hello world,结果各种报错,一大堆DLL找不到,今天用命令行编译就通过了 二.准备工作 1.Visual Studio(有nmake就行) 2.Qt 3.把qma ...

  4. 用Gradle命令行编译Android工程

    在Android sdk 目录下的samples/android-21/ 文件夹下,任找一个工程,如果在命令行直接编译 可能会报这种错误:gradle buile.gradle FAILURE: Bu ...

  5. 命令行编译vs2013项目

    echo off path %SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319\ msbuild.exe .\src\ElectricManagement. ...

  6. 命令行编译vs10项目工程

    参考网址: http://www.oschina.net/question/234345_42135 1. 1.1.使用的命令行为:开始-->所有程序--> vs2020 --> V ...

  7. Android 命令行编译、打包生成apk文件

    一.搭建搭建环境 1. 安装JDK 和 Android SDK   2. 配置环境变量 D:\android-sdk-windows\tools C:\Program Files\Java\jdk1. ...

  8. VS2010命令行编译C#和VC项目

    VS2010命令行编译C#和VC项目 VS2010命令行编译C#和VC项目 根据需要动态创建数据库字段后,需要动态创建或者调整页面,那就需要编译这些页面和后台文件.因此使用命令行编译将会非常方便,对于 ...

  9. [Android] 基于 Linux 命令行构建 Android 应用(二):命令行管理项目

    创建 Android 项目 在命令行创建 Android 项目需要用到 android 工具(该工具由 Android SDK 提供,位于 <sdk>/tools/ 目录下.).它能自动生 ...

随机推荐

  1. Android 动态创建Fragment

    Fragment是activity的界面中的一部分或一种行为.可以把多个Fragment组合到一个activity中来创建一个多界面并且可以在多个activity中重用一个Fragment.可以把Fr ...

  2. Swift开发第八篇——方法嵌套&命名空间

    本篇分为两部分: 一.Swift中的方法嵌套 二.Swift中的命名空间 一.Swift中的方法嵌套 在 swift 中我们可以让方法嵌套方法,如: func appendQuery(var url: ...

  3. 浅析Dagger2的使用

    什么是Dagger2 Dagger是为Android和Java平台提供的一个完全静态的,在编译时进行依赖注入的框架,原来是由Square公司维护,现在由Google维护. 我们知道Dagger是一个依 ...

  4. Masonry(AutoLayout)的使用

    Masonry 仍然在维持. 如果使用 Swift 开发, 建议使用 SnapKit. Masonry 以一种简便可读的代码实现子控件自动布局 ,甚至可以实现一些动画,是一中轻量级的框架. Mason ...

  5. 2.4 CMMI2级——需求管理(Requirements Management)

    人是会死的,需求是会变的.相信大家都经历了很多需求变更的痛苦,项目被拖延,成本高涨,十有七八是需求管理没有做好导致的.有哪一些需求管理方面的常见问题呢,这里列举一下: 1.因为项目进度赶等原因,在很多 ...

  6. Visual studio 2013安装

    最头疼装一些需要安装插件的软件,刚开始下载了VS2013的一个版本,安装到一半就发现还得装一些必要插件,然后得重新删了再安装,倒弄了还几次才装好.这是第一次安装时是出现的情况. 第二次尝试安装,把所有 ...

  7. C#语言基础——结构体和枚举类型

    结构体和枚举类型 一.结构体(struct) 结构类型是用户自己定义的一种类型,它是由其他类型组合而成的,可包含构造函数.常数.字段.方法.属性.索引器.运算符.事件和嵌套类型的值类型.结构在几个重要 ...

  8. Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 2)及总结

    Exercise 1:Linear Regression---实现一个线性回归 关于如何实现一个线性回归,请参考:http://www.cnblogs.com/hapjin/p/6079012.htm ...

  9. 安装Mysql 5.7.1

    现在安装MySQL变成了一件非常人性化的事情,因为有了MySQL-installer这个工具,它可以帮助我们全程安装MySQL.     下面我来简单介绍一下如何使用,以供新手学习:     .首先下 ...

  10. my_strcat()

    char* my_strcat(char* S1,const char* S2){ //严格符合strcat()的接口形式,需要的S1空间是两个字符串空间总和-1. int i=0,j=0; whil ...