<?xml version="1.0" encoding="UTF-8"?> <project name="epeiwang" 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 --> <!-- 表示咱們引用了sdk的ant的build文件--> <import file="${sdk.dir}/tools/ant/build.xml" /> </project>
#keystore文件存放目錄
key.store=./epeiwang_keystore
#keystore別名
key.alias=epeiwang_keystore
#keystore密碼
key.store.password=xxxxxxx
#組織密碼
key.alias.password=xxxxxxxx
#若是尚未生成keystore證書,可使用下面命令在項目目錄下生成一個test.keystore證書文件
#generate test.keystore
#keytool -genkey -alias test.keystore -keyalg RSA -validity 20000 -keystore test.keystore
#apk.dir表示存放最終生成apk的目錄
apk.dir=./apk
#定義項目名稱
app.name=epeiwang
#渠道號,多個渠道號用逗號分隔,每一個渠道號不要使用違規字符例如/:等,由於渠道號會在打包的時候放在apk的文件名中,因此包含#違規字符將沒法生成最終的apk,哥就是被這個細節給坑了一個下午。這裏定義了兩個渠道號myapp-12345和BAI-3s322d
market_channels=epeiwang,baidu,91
#測試環境服務器配置
test.server.url=192.168.1.10/epeiwang
test.server.image.url=192.168.1.9
test.epeiwang.url=192.168.1.10
#生產環境服務器配置
rel.server.url=111.111.111.222/epeiwang
rel.server.image.url=111.111.111.229
rel.epeiwang.url=www.epeiwang.com
#測試環境標識 給apk命名的時候用
test.tag.name=test
#生產環境標識 給apk命名的時候用
release.tag.name=release
<?xml version="1.0" encoding="UTF-8"?> <project name="custom_rules" > <!-- 引用ant-contlib這個擴展包,聲明一下 --> <taskdef resource="net/sf/antcontrib/antcontrib.properties" > <classpath> <pathelement location="${ant.ANT_HOME}/lib/ant-contrib-1.0b3.jar" /> </classpath> </taskdef> <!-- 定義一個時間變量,打完包後跟渠道號一塊兒命名apk --> <tstamp> <format pattern="yyyyMMddhhmm" property="pktime" unit="hour" /> </tstamp> <!-- 建立apk存放目錄 --> <mkdir dir="${apk.dir}" > </mkdir> <!-- 替換參數 而後打包APK --> <target name="replace_parameter" > <!-- 替換服務器配置 --> <replaceregexp byline="false" encoding="UTF-8" flags="g" > <!-- 這個是正則表達式匹配hostconfig中epeiwang_server的值 --> <regexp pattern="epeiwang_server>(.*)</epeiwang_server" /> <substitution expression="epeiwang_server>${server_url}</epeiwang_server" /> <fileset dir="" includes="res/xml/hostconfig.xml" /> </replaceregexp> <replaceregexp byline="false" encoding="UTF-8" flags="g" > <!-- 這個是正則表達式匹配hostconfig中epeiwang_img_server的值 --> <regexp pattern="epeiwang_img_server>(.*)</epeiwang_img_server" /> <substitution expression="epeiwang_img_server>${server_image_url}</epeiwang_img_server" /> <fileset dir="" includes="res/xml/hostconfig.xml" /> </replaceregexp> <replaceregexp byline="false" encoding="UTF-8" flags="g" > <!-- 這個是正則表達式匹配hostconfig中epeiwang_url的值 --> <regexp pattern="epeiwang_url>(.*)</epeiwang_url" /> <substitution expression="epeiwang_url>${epeiwang_url}</epeiwang_url" /> <fileset dir="" includes="res/xml/hostconfig.xml" /> </replaceregexp> </target> <!-- 打包測試環境命令就用這個 --> <target name="deploytest" > <!-- 傳服務器配置參數到 replace_parameter這個打包target --> <antcall target="replace_parameter" > <param name="server_url" value="${test.server.url}" /> <param name="server_image_url" value="${test.server.image.url}" /> <param name="epeiwang_url" value="${test.epeiwang.url}" /> </antcall> <!-- 執行循環打包target foreach_replacechannel --> <antcall target="foreach_replacechannel" > <!-- apk命名時候用到的參數 --> <param name="deploy_environment" value="${test.tag.name}" /> </antcall> </target> <!-- 打包生產環境命令就用這個 --> <target name="deployrel" > <!-- 傳服務器配置參數到 replace_parameter這個打包target --> <antcall target="replace_parameter" > <param name="server_url" value="${rel.server.url}" /> <param name="server_image_url" value="${rel.server.image.url}" /> <param name="epeiwang_url" value="${rel.epeiwang.url}" /> </antcall> <!-- 執行循環打包target foreach_replacechannel --> <antcall target="foreach_replacechannel" > <!-- apk命名時候用到的參數 --> <param name="deploy_environment" value="${release.tag.name}" /> </antcall> </target> <!-- 循環打包的target --> <target name="foreach_replacechannel" > <!-- 開始循環打包,從market_channels參數中取出一個渠道號用channel標識,而後經過正則修改manifest文件 --> <foreach delimiter="," list="${market_channels}" param="channel" target="modify_manifest" > </foreach> </target> <target name="modify_manifest" > <replaceregexp byline="false" encoding="UTF-8" flags="g" > <!-- 這個是正則表達式匹配manifest中meta,我用的友盟的統計,我 AndroidManifest中的配置爲: <meta-data android:value="360shichang" android:name="UMENG_CHANNEL" --> <regexp pattern="android:value="(.*)" android:name="UMENG_CHANNEL"" /> <substitution expression="android:value="${channel}" android:name="UMENG_CHANNEL"" /> <fileset dir="" includes="AndroidManifest.xml" /> </replaceregexp> <!-- 這裏設置最終生成包的存放目錄以及apk的名稱,注意這裏是文件名稱,因此變量中不容許出現違規字符,不然將沒法生成最終的apk(會出現output is not valid 的錯誤) --> <property name="out.final.file" location="${apk.dir}/${app.name}_${channel}_${deploy_environment}_${pktime}.apk" /> <antcall target="clean" /> <antcall target="release" /> </target> </project>
<?xml version="1.0" encoding="UTF-8"?> <!-- 爲了ant打包時候正則匹配,請不要格式化該文件 --> <hostconfig> <epeiwang_server>1111.1111.1111.1/epeiwang</epeiwang_server> <epeiwang_img_server>1111.1111.1111.1</epeiwang_img_server> <epeiwang_url>www.epeiwang.com</epeiwang_url> </hostconfig>