在Hudson中,使用ant自动对安卓项目编译打包apk

本文对如何在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="WebviewSession" 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="deploy">
		<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 -->
	<target name="installSignedApk" depends="deploy">
		<antcall target="installr" />
	</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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-24 21:14:38

在Hudson中,使用ant自动对安卓项目编译打包apk的相关文章

用Ant给Unity3D导出Eclipse工程打包APK

我们经常需要出完apk后,告诉我们改版本号,或者包名什么的,但是每次打包时间又很长.索性我们就出一个eclipse工程,然后用ant自动打包. 1.设置环境变量 2.生成build.xml文件 android update project -t android-21 -p ./ 3.修改签名文件 创建文本文件 ant.properties key.store=user.keystore key.store.password=123123 key.alias=yingyongbao key.ali

你的安卓项目编译要花 10 分钟,如何缩短到 1 分钟?

痛点如果项目的代码库较大,例如大型的安卓开发项目,在构建的时候耗时较长,达到数十分钟甚至更长,分析其原因,其中一部分时间是花在构建上.在大规模开发团队中,例如上百人的开发团队,如果每个人构建一次需要花费数十分钟,那么团队每天浪费的时间是非常惊人的.除了构建时间,执行 Gradle Build 的时候很大一部分时间是花在单元测试用例的执行上,这样的问题也困扰着大规模 Gradle 的用户. 方案为了让构建提升速度,Gradle 4.0 以上版本提供了Build Cache 的功能,也就是构建缓存.

eclipse中集成svn maven开发手册---maven编译打包

使用eclipse中maven进行打包. 打好的包在相应的workspace下面会有一个release的文件夹,如图

Ant管理安卓项目的自动化打包命令

Ant管理安卓项目的自动化打包命令 一.配置ant 1.登陆ant下载网页:http://ant.apache.org/bindownload.cgi,点击红框内容进行ant下载. 2.把ant下载包解压放进指定目录下. 3.配置ant的系统环境,如下 4.在dos环境中输入ant -version 测试ant环境配置是否成功. 二.使用ant命令给安卓项目生成build.xml和local.properties配置文件 1.在dos环境下输入命令: android update project

Ant自动化构建Andriod项目详解

因工作需要,最近一直在用ant实现各android工程的自动化构建.在编写自动化构建脚本过程中遇到了各种各样的坑,在逐一跳坑的过程中,也对ant的自动化构建过程也有了较为深入的了解.本文将着重介绍android平台下如何使用ant自动化编译打包apk, 及在使用过程中的遇到的问题和解决方式,希望能够对大家有所帮助. 1.      简单了解下Ant 1.1.    什么是 Ant: Apache Ant,是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于 Java 环境中

ANT自动打包U3D安卓项目研究笔记

概述 因项目使用Atlassian Stash作为项目源码管理端,且其支持Ant命令自动编译,可使其根据最新的代码自动打包,故产生该研究Ant打包的任务.在此将研究过程及一些相关知识整理记录在此. 本文部分内容可以说就是Android和Ant自动打包的配置,只是在其基础上扩充了Unity3D的部分. 阅读本文需要:知道Android开发环境大概需要哪些东西,知道Unity3D编辑器的基本使用方法. 本文并不是系统学习Ant以及Android自动打包的文章,只是笔者通过网络搜索一系列文章,然后通过

整理的Unity导出安卓工程利用ANT进行多渠道批量打包APK

Unity导出的安卓工程利用ant进行多渠道循环批量打包 一:设置JAVA环境变量 做android开发的配置这个是基础. win7 下配置java环境变量,下面是链接 http://www.cnblogs.com/zhj5chengfeng/archive/2013/01/01/2841253.html 二:配置Android的SDK环境变量 除了需要Java的环境变量,我们还需要配置Android的sdk的位置,名字是ANDROID_HOME,值就是你的android的sdk的位置,比如我的

Android开发中Ant命令编译和APK签名的一些心得

本文章麦子学院跟小伙伴们详细的分享一下关于Android Ant命令行编译和APK签名详解一些实现方法,这是一个朋友在自己做安卓开发时写的,希望对大家会有所帮助呀. 最近在做Android开发时,需要引用第三方的代码进项目,一般情况下,直接在Eclipse下设置需要导入的代码的编译输出为library即可,但是很多代码在Eclipse下编译会出现很多莫名其妙的错误.因而只能使用命令行方式对代码进行编译.具体方法如下: 1.安装编译用的Java,安装Android Platform-tools,安

Ant自动打包

Ant使用 在ant的官网http://ant.apache.org进行下载后apache-ant-1.8.2包 解压(存放的路径不要有中文字符) 把ant里的lib设置到环境变量:E:\Android\apache-ant-1.8.2\lib 这样在命令行里就可以直接使用ant 学习资料:1.  ant使用指南.rar 2. 文档:apache-ant-1.8.2/docs/manual/index.html下的Ant Tasks à List of Tasks Ant的For循环 在ant的