Skip to content

Instantly share code, notes, and snippets.

@ChenShiChao
Created February 13, 2015 04:03
Show Gist options
  • Save ChenShiChao/0d6d38697cd3179a06fb to your computer and use it in GitHub Desktop.
Save ChenShiChao/0d6d38697cd3179a06fb to your computer and use it in GitHub Desktop.
使用Ant编译Android工程
1、下载 ant-contrib-1.0b3.jar【下载链接 http://sourceforge.net/projects/ant-contrib/files/ant-contrib/ 中的1.0b3 OR http://pan.baidu.com/s/1jGgjGBO 】
2、安装Ant 【下载链接 http://ant.apache.org/bindownload.cgi】,使用命令行:ant -version查看是否安装成功
3、把 ant-contrib-1.0b3.jar 拷贝到 android SDK目录下tools的ant下
4、修改工程下的ant.properties、local.properties
5、工程项目->project->build,没有错误的话;在工程目录下运行命令ant deploy即可,release版本生成路径在ant.properties中
java.encoding=utf-8
out.absolute.dir=/X_DISK/Android_uplus/mopand/git/compile
gos.path=/X_DISK/Android_uplus/mopand/git/release
key.store=/X_DISK/Android_uplus/mopand/git/youjia-android/123456.keystore
key.store.password=123456
key.alias=123456
key.alias.password=123456
market_channels=uplustest
<?xml version="1.0" encoding="UTF-8"?>
<project name="youjia-android" default="help">
<!-- The local.properties file is created and updated by the 'android' tool.
It contains the path to the SDK. It should *NOT* be checked into
Version Control Systems. -->
<property file="local.properties" />
<!-- The ant.properties file can be created by you. It is only edited by the
'android' tool to add properties to it.
This is the place to change some Ant specific build properties.
Here are some properties you may want to change/update:
source.dir
The name of the source directory. Default is 'src'.
out.dir
The name of the output directory. Default is 'bin'.
For other overridable properties, look at the beginning of the rules
files in the SDK, at tools/ant/build.xml
Properties related to the SDK location or the project target should
be updated using the 'android' tool with the 'update' action.
This file is an integral part of the build system for your
application and should be checked into Version Control Systems.
-->
<property file="ant.properties" />
<!-- 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>
<!-- The project.properties file is created and updated by the 'android'
tool, as well as ADT.
This contains project specific properties such as project target, and library
dependencies. Lower level build properties are stored in ant.properties
(or in .classpath for Eclipse projects).
This file is an integral part of the build system for your
application and should be checked into Version Control Systems. -->
<loadproperties srcFile="project.properties" />
<!-- quick check on sdk.dir -->
<fail
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
unless="sdk.dir"
/>
<!--
Import per project custom build rules if present at the root of the project.
This is the place to put custom intermediary targets such as:
-pre-build
-pre-compile
-post-compile (This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir})
-post-package
-post-build
-pre-clean
-->
<import file="custom_rules.xml" optional="true" />
<!-- Import the actual build file.
To customize existing targets, there are two options:
- Customize only one target:
- copy/paste the target into this file, *before* the
<import> task.
- customize it to your needs.
- Customize the whole content of build.xml
- copy/paste the content of the rules files (minus the top node)
into this file, replacing the <import> task.
- customize to your needs.
***********************
****** IMPORTANT ******
***********************
In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
in order to avoid having your file be overridden by tools such as "android update project"
-->
<!-- version-tag: 1 -->
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${sdk.dir}/tools/ant/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<import file="${sdk.dir}/tools/ant/build.xml" />
<target name="deploy" >
<foreach
delimiter=","
list="${market_channels}"
param="channel"
target="modify_manifest" >
</foreach>
</target>
<target name="modify_manifest" >
<!-- <replaceregexp file="AndroidManifest.xml" encoding="utf-8" match="android:value="(.*)"" replace=""/> -->
<replaceregexp
byline="false"
flags="g" >
<regexp pattern='android:name="QHOPENSDK_CHANNEL" android:value="(.*)"' />
<substitution expression='android:name="QHOPENSDK_CHANNEL" android:value="${channel}"' />
<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>
<replaceregexp
byline="false"
flags="g" >
<regexp pattern='android:name="ANROID_CHANNEL" android:value="(.*)"' />
<substitution expression='android:name="ANROID_CHANNEL" android:value="${channel}"' />
<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>
<!-- <property name="out.release.file" value="${out.absolute.dir}/${channel}.apk"/> -->
<antcall target="release" />
<copy tofile="${gos.path}/android_${channel}.apk" >
<fileset
dir="${out.absolute.dir}/"
includes="android-android-release.apk" />
</copy>
<delete includeEmptyDirs="true" >
<fileset
dir="${out.absolute.dir}"
includes="**/*" />
</delete>
<echo message="===========================" />
</target>
</project>
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
# location of the SDK. This is only used by Ant
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=/X_DISK/android-sdk-mac
安装完 Android SDK 后可以在命令行下 Android.bat 命令创建一个示例项目,有 Ant 的话还可直接用 Ant 来编译部署到模拟器上运行。
环境准备:
告诉 path 要指向到 Android SDK 目录的 tools 子目录中,如 d:\android-sdk-windows\tools
要用 ant 编译部署的话,再把 ant 的 bin 目录加入到 path 上,如 D:\apache-ant-1.8.2\bin
CMD 进到命令行下,执行:
android create project -k cc.unmi.android.test -n Hello -a HelloAndroid -t 5 -p c:\TestAndroid
注:以上各参数的意义,可参考:http://developer.android.com/guide/developing/projects/projects-cmdline.html
-k 工程包名: cc.unmi.android.test
-n 工程名 : Hello
-a Activity子类名: HelloAndroid
-t 工程使用的平台 Target: 5 ( 基于Android SDK1.6),是执行 android list targets 显示出的 target id 值
-p 工程存储路径: c:\TestAndroid
控制台下输出:
Created project directory: c:\TestAndroid
Created directory C:\TestAndroid\src\cc\unmi\android\test
Added file c:\TestAndroid\src\cc\unmi\android\test\HelloAndroid.java
Created directory C:\TestAndroid\res
Created directory C:\TestAndroid\bin
Created directory C:\TestAndroid\libs
Created directory C:\TestAndroid\res\values
Added file c:\TestAndroid\res\values\strings.xml
Created directory C:\TestAndroid\res\layout
Added file c:\TestAndroid\res\layout\main.xml
Created directory C:\TestAndroid\res\drawable-hdpi
Created directory C:\TestAndroid\res\drawable-mdpi
Created directory C:\TestAndroid\res\drawable-ldpi
Added file c:\TestAndroid\AndroidManifest.xml
Added file c:\TestAndroid\build.xml
Added file c:\TestAndroid\proguard.cfg
我们知道生成了什么文件,目录下存在 build.xml 文件。这时候,还没 gen 目录,bin 目录是空的。
现在如果已配置好了 ant,就可以进到 c:\TestAndroid 目录下,执行 ant help, 显示出可以使用的 ant 的 target 选项:
help:
[echo] Android Ant Build. Available targets:
[echo] help: Displays this help.
[echo] clean: Removes output files created by other targets.
[echo] compile: Compiles project's .java files into .class files.
[echo] debug: Builds the application and signs it with a debug key.
[echo] release: Builds the application. The generated apk file must be
[echo] signed before it is published.
[echo] install: Installs/reinstalls the debug package onto a running
[echo] emulator or device.
[echo] If the application was previously installed, the
[echo] signatures must match.
[echo] uninstall: Uninstalls the application from a running emulator or
[echo] device.
也就是可以用 ant compile, ant release, ant install 等进行编译,打包,部署等。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment