转:Android App自动化之使用Ant编译项目多渠道打包
2013/12/13 by: shao
1. Android编译打包的整体过程
world
├── baseworld
├── floworld
├── healthworld
├── speciality
├── starworld
├── build.xml
├── code_checks.xml
├── kaiyuanxiangmu_world.keystore
└── README.md
baseworld
├── assets
├── libs
├── res
├── src
├── AndroidManifest.xml
├── lint.xml
├── proguard.cfg
├── project.properties
└── README.md
floworld/
├── assets
├── build
├── data
├── libs
├── res
├── src
├── AndroidManifest.xml
├── build.xml
├── default.properties
├── lint.xml
├── proguard.cfg
├── project.properties
└── README.md
2. 建立各个工程的ant脚本文件build.xml(位置:floworld/build.xml)
start initing ... finish initing.
3. 初始化
start initing ... finish initing. ... ...
4. 生成R.java
generating R.java for project to dir gen (using aapt) ...
generating R.java for library to dir gen (using aapt) ...
gen └── com └── tianxia ├── app │└── floworld │ └── R.java └── lib └── baseworld └── R.java
5. 编译java文件为class文件
//示例 javac -bootclasspath -s -s -s -d bin/classes *.jar
compiling java files to class files (include R.java, library and the third-party jars) ...
6. 打包class文件为classes.dex
//示例 //后面可以接任意个第三方jar路径 dx --dex --output=out/classes.dex bin/classes libs/1.jar libs/2.jar
转化成ant脚本为:
packaging class files (include the third-party jars) to calsses.dex ...
7. 打包res,assets为资源压缩包(暂且命名为res.zip)
packaging resource (include res, assets, AndroidManifest.xml, etc.) to res.zip ...
8. 使用apkbuilder命令组合classes.dex,res.zip和AndroidManifest.xml为未签名的apk
building unsigned.apk ...
这个命令比较简单。
9. 签名未签名的apk
signing the unsigned apk to final product apk ... <-- 验证密钥完整性的口令,创建时建立的 --> <-- 专用密钥的口令,就是key密码 -->
start initing ... finish initing. generating R.java for project to dir gen (using aapt) ... generating R.java for library to dir gen (using aapt) ... compiling java files to class files (include R.java, library and the third-party jars) ... packaging class files (include the third-party jars) to calsses.dex ... packaging resource (include res, assets, AndroidManifest.xml, etc.) to res.zip ... building unsigned.apk ... signing the unsigned apk to final product apk ... done.
在工程目录下运行ant:
$ant Buildfile: build.xml init: [echo] start initing ... [mkdir] Created dir: /home/openproject/world/floworld/build/latest [echo] finish initing. main: [echo] generating R.java for project to dir gen (using aapt) ... [echo] generating R.java for library to dir gen (using aapt) ... [echo] compiling java files to class files (include R.java, library and the third-party jars) ... [javac] Compiling 75 source files to /home/openproject/world/floworld/bin/classes [javac] 注意:某些输入文件使用或覆盖了已过时的 API。 [javac] 注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。 [echo] packaging class files (include the third-party jars) to calsses.dex ... [echo] packaging resource (include res, assets, AndroidManifest.xml, etc.) to res.zip ... [echo] building unsigned.apk ... [exec] [exec] THIS TOOL IS DEPRECATED. See --help for more information. [exec] [echo] signing the unsigned apk to final product apk ... [echo] done. BUILD SUCCESSFUL Total time: 28 seconds
10. 多渠道打包
AndroidManifest.XML
添加下面代码:
通过修改不同的Channel ID值,标识不同的渠道,有米广告提供了一个不错的渠道列表:http://wiki.youmi.net/PromotionChannelIDs.
#-i 表示直接修改文件 #$market是Channel ID, 后面会讲到,是来自循环一个数组 #\1,\3分别表示前面的第1,3个括号的内容,这样写很简洁 sed -i "s/\(android:value=\)\"\(.*\)\"\( android:name=\"UMENG_CHANNEL\"\)/\1\"$market\"\3/g" AndroidManifest.xml
#结合前面讲打build.xml #会在build/1.0/目录下生成floworld_1.0_appchina.apk ant -Dapk-name=floworld -Dapk-version=1.0 -Dapk-market=appchina
#定义市场列表,以空格分割 markets="dev appchina gfan" #循环市场列表,分别传值给各个脚本 for market in $markets do echo packaging floworld_1.0_$market.apk ... #替换AndroidManifest.xml中Channel值(针对友盟,其他同理) sed -i "s/\(android:value=\)\"\(.*\)\"\( android:name=\"UMENG_CHANNEL\"\)/\1\"$market\"\3/g" AndroidManifest.xml #编译对应的版本 ant -Dapk-name=floworld -Dapk-version=1.0 -Dapk-market=$market done
# ./build.sh packaging floworld_1.0_dev.apk ... Buildfile: build.xml ... ... packaging floworld_1.0_appchina.apk ... Buildfile: build.xml ... ... packaging floworld_1.0_gfan.apk ... Buildfile: build.xml ... ...
在build下生成了对应的apk文件:
build ├── 1.0 │├── floworld_1.0_appchina.apk │ ├── floworld_1.0_dev.apk │ └── floworld_1.0_gfan.apk └── README.md
成功生成!
11. 工程脚本的执行目录问题
#!/bin/bash #添加如下两行简单的代码 #1. 获取build.sh文件所在的目录 #2. 进入该build.sh所在目录,这样执行起来就没有问题了 basedir=$(cd "$(dirname "$0")";pwd) cd $basedir markets="dev appchina gfan" for market in $markets do echo packaging floworld_1.0_$market.apk ... sed -i "s/\(android:value=\)\"\(.*\)\"\( android:name=\"UMENG_CHANNEL\"\)/\1\"$market\"\3/g" AndroidManifest.xml ant -Dapk-name=floworld -Dapk-version=1.0 -Dapk-market=$market done
12. 建立整个项目的自动化编译脚本(位置:world/build.sh)
#!/bin/bash #确保进入项目跟目录 basedir=$(cd "$(dirname "$0")";pwd) cd $basedir #遍历项目下各工程目录 for file in ./* do if test -d $file then #进入工程目录 cd $basedir/$file #查找该工程目录下是否存在编译脚本build.sh if test -f build.sh then echo found build.sh in project $file. echo start building project $file ... ./build.sh fi #重要,退出工程目录到项目根目录下 cd $basedir fi done
执行该脚本:
# ./build.sh found build.sh in project ./floworld. start building project ./floworld ... packaging floworld_1.0_dev.apk ... Buildfile: build.xml ... ... found build.sh in project ./healthworld. start building project ./healthworld ... Buildfile: build.xml ...
13. 其他细节
14. 小结
来源:http://www.cnblogs.com/qianxudetianxia/archive/2012/07/04/2573687.html#2416913