標籤:
本文對如何在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 FAILEDD:\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
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
在Hudson中,使用ant自動對安卓項目編譯打包apk