基於Jenkins + Tomcat 的安卓客戶端可持續化構建及發佈下載(loltube.cn)

一. Jenkins 可持續化構建環境請參考 文章 http://my.oschina.net/long0419/blog/183299html

二. 基於搭好可持續構建環境完成基礎上 ,使用Ant 來編寫安卓持續化構建環境 (未使用gradle 原理同樣)java

一、首先要去官網下載ANT代碼。node

ANT官網下載地址android

二、下載ANT循環打包JAR包。web

ANT循環JAR包下載地址shell

網盤下載地址express

三、解壓獲得其中的ant-contrib-1.0b3.jar文件待用。apache

作完以上3步,ANT多渠道打包所須要的軟件就準備完畢了,接下來就是環境配置了(略去)。android-studio

4. 打包過程 總共須要編輯4個文件,分別爲build.xml、local.properties、customrules.xml、ant.properties. 同時準備好本身的簽名文件:androidkey.keystore。tomcat

一、編輯local.properties文件內容,主要是配置SDK路徑及項目路徑

# 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的路徑地址
sdk.dir=E:\\Android\\android-studio\\sdk  
#項目路徑地址
project.dir=F:\\Android\\sourceCode\\Test

二、編輯ant.properties,主要是配置ANT打包的數據

#項目包名
application.package=com.ecloud.test  
#項目名稱
ant.project.name=test  
#項目編碼,這裏須要注意本身工程的編碼格式,須要保證編碼格式一致(可不使用)
#java.encoding=utf-8   
#編譯中間文件生成目錄
out.absolute.dir=F:/Test/compile  
#最終APK生成文件目錄
gos.path=F:/Test/test-code1-20140523  
#簽名文件全路徑
key.store=F:/Test/Test/android_key.keystore 
#簽名文件密碼
key.store.password=111111  
#別名,這裏要注意若是你簽名文件的別名爲中文,須要和我這個同樣轉成16進制,否則簽名的時候會報錯,轉碼能夠用【UltraEdit工具】(自行百度下載)來作。
key.alias=\u5BB9\u6613\u901B  
#別名密碼
key.alias.password=111111  
#軟件版本號
app_version=1.0.0
#須要打包的渠道名,注意‘,’分格
market_channels=Gfan,3G,360,AndMarket,AnZi


三、編輯custom_rules.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules" >
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" >
        <classpath>
        <!--注意這裏要和你拷貝的那個JAR文件相同-->
            <pathelement location="lib/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>
    <target name="deploy" >
        <foreach
            delimiter=","
            list="${market_channels}"
            param="channel"
            target="modify_manifest" >
        </foreach>
    </target>
    <target name="modify_manifest" >
       <replaceregexp flags="g" byline="false">  
       <regexp pattern="android:value=&quot;(.*)&quot; android:name=&quot;UMENG_CHANNEL&quot;" />  
         <substitution expression="android:value=&quot;{channel}&quot; android:name=&quot;UMENG_CHANNEL&quot;" />  
           <fileset
                dir=""
                includes="AndroidManifest.xml" />
        </replaceregexp>
        <property
            name="out.final.file"
            location="${apk.dir}/${ant.project.name}_${channel}.apk" />
        <antcall target="clean" />
        <antcall target="debug" />
    </target>
</project>


四、編輯最終的build.xml文件,這個是纔是循環打包的重點

<?xml version="1.0" encoding="UTF-8"?> <!-- 項目名稱test,可用全局替換爲當前項目名稱 -->
    <project
        name="test"
        default="deploy" > <!-- 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" />
    <property
        name="manifest.file"
        value="AndroidManifest.xml" >
    </property>
    <property
        name="bin.dir"
        value="bin" >
    </property>
    <property
        name="absolute-out"
        value="${project.dir}/${bin.dir}" >
    </property>
    <property
        name="absolute-file-manifest-out"
        value="${absolute-out}/${manifest.file}" >
    </property>
    <property
        name="absolute-file-manifest-src"
        value="${project.dir}/${manifest.file}" >
    </property> <!-- 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" />
    <fail
        message="sdk.dir is missing. Make sure to generate local.properties using &apos;android update project&apos; or to inject it through an env var"
        unless="sdk.dir" /> <!-- extension targets. Uncomment the ones where you want to do custom work
     in between standard targets -->
    <!-- <target name="-pre-build">
    </target>
    <target name="-pre-compile">
    </target>
   
    <target name="-post-compile">
    </target> -->
    <!-- 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" >
<!---->
######注意這裏的JAR包路徑,要修改爲本身的
        <classpath>
            <pathelement location="D:/Ant/lib/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>   
<!-- -->
######這裏就是修改渠道號的代碼,個人渠道值設置的是SETTING_UMENG_CHANNEL_CHANNELID_VALUES你能夠替換成你本身的
       <target name="modify_manifest" > <!-- <copy file="${absolute-file-manifest-src}" tofile="${absolute-file-manifest-out}" overwrite="true"/>
        <replace file="${absolute-file-manifest-out}" token="SETTING_UMENG_CHANNEL_CHANNELID_VALUES" value="${channel}" encoding="UTF8"/> -->
        
        <replaceregexp
            encoding="utf-8"
            file="AndroidManifest.xml"
            match="SETTING_UMENG_CHANNEL_CHANNELID_VALUES"
            replace="${channel}" /> <!-- <replaceregexp
            byline="false"
            flags="g" >
            <regexp pattern="android:name="UMENG_CHANNEL" android:value="(.*)" />
            <substitution expression="android:name="UMENG_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}/test_${channel}.apk" >
            <fileset
                dir="${out.absolute.dir}/"
                includes="test-release.apk" />
        </copy>
        <delete includeEmptyDirs="true" >
            <fileset
                dir="${out.absolute.dir}"
                includes="**/*" />
        </delete>  
<!---->
######這裏要將替換以後的渠道號值改爲默認的值SETTING_UMENG_CHANNEL_CHANNELID_VALUES,否則下一個打包時將不會替換渠道號的值
        <replaceregexp
            encoding="utf-8"
            file="AndroidManifest.xml"
            match="${channel}"
            replace="SETTING_UMENG_CHANNEL_CHANNELID_VALUES" />
        <echo message="===========================" />
    </target>
   </project>


五、把以上4個文件拷貝到項目根目錄下,目錄結構以下

test--
    --src
    --res
    --libs
    --local.properties
    --custom_rules.xml
    --ant.properties
    --build.xml


六、打開CMD,而後CD到項目根路徑下,運行ant deploy便可。注意在運行以前要注意先clean一下項目,否則可能會報錯誤,切記!!!!

BUILD FAILED
F:\Test\Test\build.xml:113: The following error occurred while executing this line:
F:\Test\Test\build.xml:139: The following error occurred while executing this line:

七、若是你看到BUILD SUCCESS,那麼恭喜你,多渠道打包編譯成功了,去輸出目錄查看一下APK文件吧!

錯誤說明:

a.提示以下

Android build failing with build.xml:479: SDK does not have any Build Tools installed

表示當前sdk 版本太低只須要在控制檯中執行下命令行後更新安卓sdk版本便可

android update sdk -u

三 . 當已經構建完成後返回Jenkins 中進行後續配置 截圖以下:








因爲還使用了天天23點定時構建還需作以下配置



而後執行完把構建後的apk所有移動到web容器中 使用shell 腳本執行

cp ./tmp/diandi100-*.apk /home/test/apache-tomcat-7.0.52/webapps/filedownload/files
rm -rf ./tmp/*.*

三 .在web 容器中發佈 而後提供下載

因爲可讓在tomcat 中執行下載操做 首先能夠在eclipse 中隨意創建一個web 工程而後將tomcat conf 目錄中的web.xml中的list 熟悉修改成true 便可 而後訪問web工程目錄以下顯示 :

    

後續只須要根據需求下載對面版本安裝測試

*-release.apk :簽名優化版本

*-release-unaligned.apk :簽名版本

*-release-unsigned.apk :未簽名版本

相關文章
相關標籤/搜索