軟件環(huán)境
OS:CentOS 6.0 64-bit
Android SDK:adt-bundle-linux-x86_64
Jenkins:1.529
Ant:1.8.2
配置管理
1.Jenkins中配置JDK,,直接配置1.7+以上JDK,。過程略,。
2.把Android工程改造成具有Ant特性
(如果項目本身已經(jīng)ant化了,,略過此步。)
先從源碼庫,,比如SVN檢出源碼,,然后進行工作空間執(zhí)行
# cd /myworkspace
# $ANDROID_HOME/sdk/tools/android update project -p my-android-app
再把生成的文件上傳到SVN。
也可以從頭開始生成一個全新的空白ant特性的android工程,,
# $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=的名稱
不清楚參數(shù)用法使用# $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系統(tǒng)設置ant欄中,,
然后jenkins系統(tǒng)設置全局屬性欄中,,
保存后,進行項目構建配置頁面,,Invoke Ant中,,Ant Version下拉列表選擇ant-1.8.2-external。
執(zhí)行各項任務
利用andorid sdk中ant提供的build.xml腳本(位于%ANDROID_HOME%/sdk/tools/ant目錄)可輕松完成各種日常任務
1.打包APK
直須在Jenkins項目配置頁面,,找到Invoke Ant欄,,作如下設置
2.打包APK后同時對apk進行簽名和zipalign優(yōu)化
如果查看%ANDROID_HOME%/sdk/tools/ant/build.xml文件,會看到ant執(zhí)行release操作的同時會根據(jù)local.properies和ant.properties中配置的屬性,,決定是否執(zhí)行簽名和zipalign,。為些要在Invoke ant中如下設置
這樣ant在打包時會自動根據(jù)密鑰庫、密碼信息對生成的apk進行簽名,,而無需再手工輸入密鑰的密碼,。
3.打包APK,簽名和zipalign優(yōu)化后把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./display/JENKINS/Building+an+Android+app+and+test+project
http:///content/getting-started-building-android-apps-hudson
|