命令行ant编译java,Ant 命令行编译Android项目

本文详细介绍了如何在Android项目中配置Ant编译环境,包括设置路径、创建项目、更新项目版本、自定义custom_rules.xml及ant.properties文件。重点讲解了关键步骤和常见问题解决方法,帮助开发者高效管理Android项目构建过程。

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

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

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

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

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

-s 是因为带子项目

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

其中

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

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

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

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

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

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

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

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

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

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

两个相似的custom_rules.xml 例子

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}">

Instrumenting classes from ${out.absolute.dir}/classes...

appPackage="${project.app.package}"

libraryPackagesRefId="project.library.packages"

filterOut="emma.default.filter"/>

mode="overwrite"

instrpath="${out.absolute.dir}/classes"

outdir="${out.absolute.dir}/classes"

metadatafile="${emma.coverage.absolute.file}">

Creating library output jar file...

Custom jar packaging exclusion: ${android.package.excludes}

includes="**/*.class"

excludes="${project.app.package.path}/R.class ${project.app.package.path}/R$*.class ${project.app.package.path}/BuildConfig.class"/>

另一个

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}">

Instrumenting classes from ${out.absolute.dir}/classes...

appPackage="${project.app.package}"

libraryPackagesRefId="project.library.packages"

filterOut="emma.default.filter"/>

mode="overwrite"

instrpath="${out.absolute.dir}/classes"

outdir="${out.absolute.dir}/classes"

metadatafile="${emma.coverage.absolute.file}">

Creating library output jar file...

Custom jar packaging exclusion: ${android.package.excludes}

includes="**/*.class"

excludes="${project.app.package.path}/R.class ${project.app.package.path}/R$*.class ${project.app.package.path}/BuildConfig.class"/>

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

key.store=D:/workAndroid/files/android_key.store

key.alias=alia

key.store.password=111111

key.alias.password=111111

java.encoding=UTF-8

java.target=7

java.source=7

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

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

depends="rename-release-with-version-number,android_rules.-set-release-mode">

location="${out.absolute.dir}/${ant.project.name}-${themanifest.manifest.android:versionName}-release-unsigned.apk" />

location="${out.absolute.dir}/${ant.project.name}-${themanifest.manifest.android:versionName}-release.apk" />

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.

0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值