首先把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 例子
<?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 '${generated.absolute.dir}'" />
<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 '${generated.absolute.dir}'" />
<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=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
<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發布SVN WEB項目到Tomcat以及利用post-commit自動提交編譯更新 http://www.linuxidc.com/Linux/2014-11/109331.htm
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11