軟件環境
OS:CentOS 6.0 64-bit java
Android SDK:adt-bundle-linux-x86_64 linux
Jenkins:1.529 android
Ant:1.8.2 c++
配置管理
1.Jenkins中配置JDK,直接配置1.7+以上JDK。過程略。
2.把Android工程改形成具備Ant特性
(若是項目自己已經ant化了,略過此步。) apache
先從源碼庫,好比SVN檢出源碼,而後進行工做空間執行 bash
# cd /myworkspace
# $ANDROID_HOME/sdk/tools/android update project -p my-android-app
再把生成的文件上傳到SVN。 服務器
也能夠從頭開始生成一個全新的空白ant特性的android工程, app
# $ANDROID_HOME/sdk/tools/android create project -n myandroidapp -t a
ndroid-19 -p my-android-app -k pragmatic.pkg -a Pragmatic
工具
-n Project name.
-t android target id,必選
-p 工程文件夾目錄位置,必選
-k 安卓包名稱即AndroidManifest.xml中<manifest package=的名稱,必選
-a 安卓Activity名稱,AndroidManifest.xml中<activity android:name=的名稱 優化
不清楚參數用法使用# $ANDROID_HOME/sdk/tools/android create project命令行會告訴你用法。
而後打開ADT,import -> Andorid下面的Existing Android Code Into Workspace。再而後Team -> Share Project到SVN服務器。
3.源碼庫配置
在Jenkins項目配置頁面,找到源碼管理欄,做以下設置。
4.Jenkins中配置Ant
方式一,把ant做爲一個Jenkins內置的工具使用。
新增一個ant安裝
保存後,進行項目構建配置頁面,以下
方式二,引用一個外部已有的ant安裝。
jenkins系統設置ant欄中,
而後jenkins系統設置全局屬性欄中,
保存後,進行項目構建配置頁面,Invoke Ant中,Ant Version下拉列表選擇ant-1.8.2-external。
執行各項任務
利用andorid sdk中ant提供的build.xml腳本(位於%ANDROID_HOME%/sdk/tools/ant目錄)可輕鬆完成各類平常任務
1.打包APK
直須在Jenkins項目配置頁面,找到Invoke Ant欄,做以下設置
2.打包APK後同時對apk進行簽名和zipalign優化
若是查看%ANDROID_HOME%/sdk/tools/ant/build.xml文件,會看到ant執行release操做的同時會根據local.properies和ant.properties中配置的屬性,決定是否執行簽名和zipalign。爲些要在Invoke ant中以下設置
這樣ant在打包時會自動根據密鑰庫、密碼信息對生成的apk進行簽名,而無需再手工輸入密鑰的密碼。
3.打包APK,簽名和zipalign優化後把APK裝到與Jenkins主機相連的手機上。
在Jenkins構建Invoke Ant腳本後,增長一個Shell調用步驟,內容以下:
#!/bin/bash
adb uninstall com.aaa.pragmatic(.apk)
adb install /target/dir/apks/com.aaa.pragmatic.apk
前提是:(1)安裝android usb driver (2)手機與電腦相連 (3)電腦進程中沒有tadb.exe、豌豆夾等運行。(4)若是不須要在設備上進行debug,能夠更新AndroidManifest.xml文件,去掉android:debuggable。
Troubleshootings
1.
Execute failed: java.io.IOException: Cannot run program "/android/sdk/build-tools/android-4.4/aapt": error=2, 沒有那個文件或目錄
-code-gen:
[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...
BUILD FAILED
/android/sdk/tools/ant/build.xml:653: The following error occurred while executing this line:
/android/sdk/tools/ant/build.xml:698: Execute failed: java.io.IOException: Cannot run program "/android/sdk/build-tools/android-4.4/aapt": error=2, 沒有那個文件或目錄
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
at java.lang.Runtime.exec(Runtime.java:615)
at org.apache.tools.ant.taskdefs.Execute$Java13CommandLauncher.exec(Execute.java:862)
at org.apache.tools.ant.taskdefs.Execute.launch(Execute.java:481)
at org.apache.tools.ant.taskdefs.Execute.execute(Execute.java:495)
at org.apache.tools.ant.taskdefs.ExecTask.runExecute(ExecTask.java:631)
at org.apache.tools.ant.taskdefs.ExecTask.runExec(ExecTask.java:672)
at org.apache.tools.ant.taskdefs.ExecTask.execute(ExecTask.java:498)
at com.android.ant.AaptExecTask.execute(AaptExecTask.java:699)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
at com.android.ant.IfElseTask.execute(IfElseTask.java:120)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.taskdefs.MacroInstance.execute(MacroInstance.java:398)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:809)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: java.io.IOException: error=2, 沒有那個文件或目錄
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
... 46 more
緣由:在64位Linux下打包成APK時缺乏x86下C++語言庫。
解決:
# yum install -y compat-libstdc++-296.i686
# yum install -y compat-libstdc++-33.i686
2.
[aapt] /android/sdk/build-tools/android-4.4/aapt: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
-code-gen:
[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] /android/sdk/build-tools/android-4.4/aapt: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
解決:
# yum install -y zlib
3.
[aapt] /android/sdk/build-tools/android-4.4/aapt: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory
-code-gen:
[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] /android/sdk/build-tools/android-4.4/aapt: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory
緣由:在64位Linux下打包成APK時缺乏x86下C++語言庫。
解決:
# yum install -y libstdc++.so.6
參考文獻
https://wiki.jenkins-ci.org/display/JENKINS/Building+an+Android+app+and+test+project
http://jenkins-ci.org/content/getting-started-building-android-apps-hudson