一. Jenkins 可持续化构建环境请参考 文章 http://my.oschina.net/long0419/blog/183299
二. 基于搭好可持续构建环境完成基础上 ,使用Ant 来编写安卓持续化构建环境 (未使用gradle 原理一样)
1、首先要去官网下载ANT代码。
2、下载ANT循环打包JAR包。
3、解压得到其中的ant-contrib-1.0b3.jar文件待用。
做完以上3步,ANT多渠道打包所需要的软件就准备完毕了,接下来就是环境配置了(略去)。
4. 打包过程 总共需要编辑4个文件,分别为build.xml、local.properties、customrules.xml、ant.properties. 同时准备好自己的签名文件:androidkey.keystore。
1、编辑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
2、编辑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
3、编辑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="(.*)" android:name="UMENG_CHANNEL"" /> <substitution expression="android:value="{channel}" android:name="UMENG_CHANNEL"" /> <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>
4、编辑最终的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 'android update project' 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>
5、把以上4个文件拷贝到项目根目录下,目录结构如下
test-- --src --res --libs --local.properties --custom_rules.xml --ant.properties --build.xml
6、打开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:
7、如果你看到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 :未签名版本