1.前期所準備的環境和軟件。
jdk1.7.0_79
android-sdk-linux
apache-ant-1.8.2
......................................
tar -vxzf apahce-ant-1.8.2-bin.tar.gz
chmod 755 -R apache-ant-1.8.2
2.環境配置
vi /etc/profile 修改系統配置文件
export JAVA_HOME=/usr/jvm/jdk1.7.0_79
export SDK_HOME=/usr/local/android-sdk-linux/
export ANT_HOME=/home/kf/apache-ant-1.8.2
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$SDK_HOME/tools:$SDK_HOME/build-tools/android-4.4W:$SDK_HOME/platform-tools:$PATH:$ANT_HOME/bin
source /etc/proifle 立刻將配置生效
ant -version 測試ant是否生效
3.建立項目
列出目標平臺ID
user abc # $SDK_HOME/tools/android list targets
Available Android targets:
----------
id: 1 or "android-20"
Name: Android 4.4W
Type: Platform
API level: 20
Revision: 1
Skins: WSVGA, WXGA720, WXGA800-7in, WQVGA432, WQVGA400, HVGA, WVGA800 (default), WVGA854, WXGA800, QVGA
Tag/ABIs : no ABIs.
建立新的android工程(效果同使用eclipse新建android工程一致)
$ANDROID_SDK_DIR/tools/android create project –k包名 –a項目名 –t目標平臺ID –p 路徑
user abc # $SDK_HOME/tools/android create project -k com.sy.qm -a dq -t 1 -p /home/yanfa1/abc/
user abc # ll
total 52
drwxr-xr-x 6 root root 4096 12月 14 15:55 ./
drwxrwxrwx 26 yanfa1 yanfa1 4096 12月 14 15:31 ../
-rw-r--r-- 1 root root 627 12月 14 15:36 AndroidManifest.xml
-rw-r--r-- 1 root root 698 12月 14 15:36 ant.properties
-rw-r--r-- 1 root root 105 12月 14 15:46 .ant-targets-build.xml
-rw-r--r-- 1 root root 3914 12月 14 15:44 build.xml
drwxr-xr-x 3 root root 4096 12月 14 15:46 gen/
drwxr-xr-x 2 root root 4096 12月 14 15:36 libs/
-rw-r--r-- 1 root root 422 12月 14 15:44 local.properties
-rw-r--r-- 1 root root 781 12月 14 15:44 proguard-project.txt
-rw-r--r-- 1 root root 563 12月 14 15:44 project.properties
drwxr-xr-x 8 root root 4096 12月 14 15:36 res/
drwxr-xr-x 3 root root 4096 12月 14 15:36 src/
建立ant編譯腳本build.xml
$ANDROID_SDK_DIR/tools/android update project --name項目名 –t目標平臺ID –p路徑
user abc # $SDK_HOME/tools/android update project --name dq -t 1 -p /home/yanfa1/abc/
編譯工程:
a)生成debug版本
$ ant debug
生成帶debug未簽名的apk
-package:
[apkbuilder] Current build type is different than previous build: forced apkbuilder run.
[apkbuilder] Creating dq-debug-unaligned.apk and signing it with a debug key...
-post-package:
-do-debug:
[zipalign] Running zip align on final apk...
[echo] Debug Package: /home/user/abc/bin/dq-debug.apk
[propertyfile] Creating new property file: /home/user/abc/bin/build.prop
[propertyfile] Updating property file: /home/user/abc/bin/build.prop
[propertyfile] Updating property file: /home/user/abc/bin/build.prop
[propertyfile] Updating property file: /home/user/abc/bin/build.prop
-post-build:
debug:
BUILD SUCCESSFUL
Total time: 3 seconds
user bin # ll
total 168
drwxr-xr-x 5 root root 4096 12月 14 15:59 ./
drwxr-xr-x 7 root root 4096 12月 14 15:59 ../
-rw-r--r-- 1 root root 627 12月 14 15:59 AndroidManifest.xml
-rw-r--r-- 1 root root 84 12月 14 15:59 AndroidManifest.xml.d
-rw-r--r-- 1 root root 179 12月 14 15:59 build.prop
drwxr-xr-x 3 root root 4096 12月 14 15:59 classes/
-rw-r--r-- 1 root root 1996 12月 14 15:59 classes.dex
-rw-r--r-- 1 root root 419 12月 14 15:59 classes.dex.d
drwxr-xr-x 2 root root 4096 12月 14 15:59 dexedLibs/
-rw-r--r-- 1 root root 34140 12月 14 15:59 dq.ap_
-rw-r--r-- 1 root root 599 12月 14 15:59 dq.ap_.d
-rw-r--r-- 1 root root 37481 12月 14 15:59 dq-debug.apk
-rw-r--r-- 1 root root 37472 12月 14 15:59 dq-debug-unaligned.apk
-rw-r--r-- 1 root root 113 12月 14 15:59 dq-debug-unaligned.apk.d
-rw-r--r-- 1 root root 83 12月 14 15:59 proguard.txt
drwxr-xr-x 6 root root 4096 12月 14 15:59 res/
b)生成release版本
$ ant release
生成帶release未簽名的apk
-release-prompt-for-password:
-release-nosign:
[echo] No key.store and key.alias properties found in build.properties.
[echo] Please sign /home/user/abc/bin/dq-release-unsigned.apk manually
[echo] and run zipalign from the Android SDK tools.
[propertyfile] Creating new property file: /home/user/abc/bin/build.prop
[propertyfile] Updating property file: /home/user/abc/bin/build.prop
[propertyfile] Updating property file: /home/user/abc/bin/build.prop
[propertyfile] Updating property file: /home/user/abc/bin/build.prop
-release-sign:
-post-build:
release:
BUILD SUCCESSFUL
Total time: 3 seconds
userbin # ll
total 124
drwxr-xr-x 5 root root 4096 12月 14 16:04 ./
drwxr-xr-x 7 root root 4096 12月 14 16:04 ../
-rw-r--r-- 1 root root 627 12月 14 16:04 AndroidManifest.xml
-rw-r--r-- 1 root root 84 12月 14 16:04 AndroidManifest.xml.d
-rw-r--r-- 1 root root 183 12月 14 16:04 build.prop
drwxr-xr-x 3 root root 4096 12月 14 16:04 classes/
-rw-r--r-- 1 root root 1992 12月 14 16:04 classes.dex
-rw-r--r-- 1 root root 419 12月 14 16:04 classes.dex.d
drwxr-xr-x 2 root root 4096 12月 14 16:04 dexedLibs/
-rw-r--r-- 1 root root 34117 12月 14 16:04 dq.ap_
-rw-r--r-- 1 root root 599 12月 14 16:04 dq.ap_.d
-rw-r--r-- 1 root root 35254 12月 14 16:04 dq-release-unsigned.apk
-rw-r--r-- 1 root root 114 12月 14 16:04 dq-release-unsigned.apk.d
-rw-r--r-- 1 root root 83 12月 14 16:04 proguard.txt
drwxr-xr-x 6 root root 4096 12月 14 16:04 res/
備註:
eclipse:
-rw-rw-r-- 1 yanfa1 yanfa1 2633 12月 13 10:52 AndroidManifest.xml
drwxrwxr-x 2 yanfa1 yanfa1 4096 12月 14 15:07 assets/
drwxrwxr-x 3 yanfa1 yanfa1 4096 12月 14 15:07 gen/
drwxrwxr-x 2 yanfa1 yanfa1 4096 12月 14 15:07 lib/
drwxrwxr-x 8 yanfa1 yanfa1 4096 12月 14 15:07 libs/
-rw-rw-r-- 1 yanfa1 yanfa1 781 12月 4 14:07 proguard-project.txt
-rw-rw-r-- 1 yanfa1 yanfa1 563 12月 6 10:06 project.properties
drwxrwxr-x 14 yanfa1 yanfa1 4096 12月 14 15:07 res/
drwxrwxr-x 3 yanfa1 yanfa1 4096 12月 14 15:07 src/
執行
$SDK_HOME/tools/android update project --name Project -t 1 -p /home/user/Project/
user Project # $SDK_HOME/tools/android update project --name Project -t 1 -p /home/yanfa1/Project/
Updated project.properties
Updated local.properties
Added file /home/yanfa1/Project/build.xml
Updated file /home/yanfa1/Project/proguard-project.txt
ant release
OK.
第三方包,庫放到libs下
如果引用了一些包放到lib下,只需編譯不需打包進入,如何辦呢?需新加
<property name="jar.lib.dir" value="lib" />
<property name="jar.lib.absolute.dir" location="${jar.lib.dir}" />
<property name="jar.libs.dir" value="libs" />
<property name="jar.libs.absolute.dir" location="${jar.libs.dir}" />
<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}" />
<classpath>
<fileset dir="${jar.lib.dir}" includes="*.jar" />
</classpath>
<compilerarg line="${java.compilerargs}" />
</javac>
build.sh
ant clean
ant release