本文对如何在hudson中配置ant编译打包apk进行说明,主要包括build.xml文件的编写、环境的配置、在Hudson中创建任务。
一、为安卓项目创建build.xml文件
1、打开cmd进入sdk目录下的tools目录,输入: android.bat list target 来查询我们现有的版本list有哪些。如下图:
途中用红框圈出的 id 与蓝框圈出的版本号对应关系,下面需要用到。
2、打开安卓项目工程下的 project.properties 文件,查看target 版本号,如下图:
3、使用命令 android update project -n WebviewSession -t 19 -p E:\Lenovocw\▲技术解决方案\安卓Webview的Session传递\WebviewSession 在工程下生成build.xml文件,如下图所示:
-n 对应的是项目名称
-t 就是我们之前查询的SDK版本对应的ID,大家根据自己的项目版本做出选择即可,我这个是android 4.4.2 所以用ID 19
-p 就是生成的路径
此时,项目中自动生成了 build.xml 和 local.properties 2个文件
查看在 build.xml 文件可以发现,build.xml 文件读取了local.properties 和 ant.properties 2个配置文件,最后引用了 sdk 下面的 build.xml 文件。
如果我们需要在订制自己的target,需要用到一些配置参数,我们把它放到 local.properties 中即可,生成的 local.properties 文件中已自动指定了我们的sdk目录。
4、配置签名证书的信息,让 ant 为我们自动打包并使用我们 配置的证书进行签名
在项目工程下添加 ant.properties 文件,配置我们的证书信息,如下图所示:
至此,我们可以对android 项目进行ant 打包了。
如果我们项目引用了外部jar包,使用Ant Build构建项目时出现的"crunch" 错误,如下:
-code-gen: [mergemanifest] Found Deleted Target File [mergemanifest] Merging AndroidManifest files into one. [mergemanifest] Manifest merger disabled. Using project manifest only. [echo] Handling aidl files... [aidl] No AIDL files to compile. [echo] ---------- [echo] Handling RenderScript files... [echo] ---------- [echo] Handling Resources... [aapt] Generating resource IDs... [aapt] invalid resource directory name: F:\workspace\Zlib\bin\res/crunch BUILD FAILED D:\Android\sdk\tools\ant\build.xml:601: The following error occurred while executing this line: D:\Android\sdk\tools\ant\build.xml:653: The following error occurred while executing this line: D:\Android\sdk\tools\ant\build.xml:698: null returned: 1
出现以上编译错误,解决办法如下:
方法1:在系统tool/ant/build.xml文件中赋值<property name="aapt.ignore.assets" value="crunch" />
方法2:在自己项目build文件中添加<property name="aapt.ignore.assets" value="!.svn:!.git:\x3Cdir\x3E_*:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~:crunch" />
不想去修改sdk内部的build.xml 文件,所以我在项目中生成的 build.xml 文件使用了方法2做了配置。
下面是我的项目下 build.xml 的代码,提供参考。
<?xml version="1.0" encoding="UTF-8"?> <project name="CGVMovie_2015" 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" /> <condition property="sdk.dir" value="${window.sdk.dir}" else="${linux.sdk.dir}"><os family="windows" /></condition> <condition property="deploy.dir.apk" value="${window.deploy.dir.apk}" else="${linux.deploy.dir.apk}"><os family="windows" /></condition> <!-- 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> <!--解决在Ant Build构建项目时出现的"crunch"错误 --> <property name="aapt.ignore.assets" value="!.svn:!.git:\x3Cdir\x3E_*:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~:crunch" /> <!-- 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 --> <import file="${sdk.dir}/tools/ant/build.xml" /> <!-- 编译并安装一个签名的apk --> <target name="installSignedApk" depends="deploy"> <antcall target="installr" /> </target> <!--输出apk文件 --> <target name="deploy"> <!-- 先clean再release --> <antcall target="clean" /> <antcall target="release" /> <!--复制肯定还要涉及到同名覆盖的问题,ant在copy类的API中说明:Files are only copied if the source file is newer than the destination file,这里的newer是指文件的修改时间,即使你在修改时文件内容没有任何变化,只是导致修改时间变了,ant同样会覆盖同名文件,也就是说,ant不会检查文件内容。--> <copy file="${out.absolute.dir}/${ant.project.name}-release.apk" tofile="${deploy.dir.apk}/${ant.project.name}.apk" /> </target> <!--输出apk文件(STG环境) --> <target name="deploy-stg"> <!--备份文件--> <copy file="${basedir}/src/com/cgv/cn/movie/common/Urls.java" tofile="${basedir}/src/com/cgv/cn/movie/common/Urls.java.bak" overwrite="true"/> <!--替换内容--> <replace file="${basedir}/src/com/cgv/cn/movie/common/Urls.java" encoding="UTF-8"> <replacefilter token="${token1}" value="${value1}"/> <replacefilter token="${token2}" value="${value2}"/> </replace> <!-- 先clean再release --> <antcall target="clean" /> <antcall target="release" /> <!--复制肯定还要涉及到同名覆盖的问题,ant在copy类的API中说明:Files are only copied if the source file is newer than the destination file,这里的newer是指文件的修改时间,即使你在修改时文件内容没有任何变化,只是导致修改时间变了,ant同样会覆盖同名文件,也就是说,ant不会检查文件内容。--> <copy file="${out.absolute.dir}/${ant.project.name}-release.apk" tofile="${linux.deploy.dir.apk.stg}/${ant.project.name}.apk" /> <!--还原文件--> <copy file="${basedir}/src/com/cgv/cn/movie/common/Urls.java.bak" tofile="${basedir}/src/com/cgv/cn/movie/common/Urls.java" overwrite="true"/> <delete file="${basedir}/src/com/cgv/cn/movie/common/Urls.java.bak" /> </target> </project>
二、在服务器上配置android-sdk环境(我用的是linux系统,window是开发环境不再单独说明,使用是一样的)
下载 Linux 版本的 android-sdk ,上传到服务器上,解压缩包后的目录结构如下所示:
android-sdk-linux
- tools
- android
- ant
- apps
- ddms
- draw9patch
- emulator
- emulator64-arm
- emulator64-mips
- emulator64-x86
- emulator-arm
- emulator-mips
- emulator-x86
- hierarchyviewer
- jobb
- lib
- lint
- mksdcard
- monitor
- monkeyrunner
- NOTICE.txt
- proguard
- screenshot2
- source.properties
- support
- templates
- traceview
- uiautomatorviewer
- platform-tools
- adb
- api
- dmtracedump
- etc1tool
- fastboot
- hprof-conv
- NOTICE.txt
- source.properties
- sqlite3
- systrace
- platforms
- android-19
- build-tools
- 20.0.0
三、在Hudson中创建任务(至于怎么安装hudson,这里就不做赘述了)
1、在hudson中新建一个任务,输入ProjectName,一般我们勾选“Use custom workspace”使用自己的工作空间目录,如下图:
2、配置SVN信息,hudson会根据我们配置的SVN信息帮我们从svn中更新代码,如下图所示:
3、我们可以根据需要勾选Build perodically Schedule,这个是交给hudson自动帮我们在指定的时间内执行这个任务,如下图所示:
关于表达式的配置可以点击后面的问号查看简单的说明,输入的内容可以是多行的。
4、配置ant
选择已经安装的ant,指定build.xml中需要执行的targets,指定build.xml 文件(. 开头是相对路径,也可以使用绝对路径),然后可以在Properties中输入我们需要在build.xml文件中需要的变量,用key=value的格式编写,每行写一个键值对。
5、点击底部的保存按钮保存任务。
--------------------------------------------
备注:
如果在Linux上使用ant编译打包apk的时候,出现下面的错误:
/usr/local/android-sdk-linux/tools/ant/build.xml:698: Execute failed: java.io.IOException: Cannot run program "/usr/local/android-sdk-linux/build-tools/22.0.0/aapt": error=2, No such file or directory
解决方案详见:http://blog.csdn.net/catoop/article/details/47157631