Ant如何打包UIAutomator項目用到的第三方JAR包

本文章主要描述UIAutomator項目中引用到第三方Jar包的時候,按照正常的打包方式碰到的各種問題,以及最終解決的思路和辦法。

1. 問題起源

在本人的一個示例項目中引用到了單元測試框架hamcrest的jar包,在項目目錄下執行ant build的時候出現以下的問題

源碼如下:

[java] view plain copy
  1. package majcit.com.UIAutomatorDemo;  
  2.   
  3. import com.android.uiautomator.core.UiDevice;  
  4. import com.android.uiautomator.core.UiObject;  
  5. import com.android.uiautomator.core.UiObjectNotFoundException;  
  6. import com.android.uiautomator.core.UiScrollable;  
  7. import com.android.uiautomator.core.UiSelector;  
  8. import com.android.uiautomator.testrunner.UiAutomatorTestCase;  
  9. import static org.hamcrest.Matchers.*;  
  10. import static org.hamcrest.MatcherAssert.assertThat;  
  11.   
  12. public class NotePadTest extends UiAutomatorTestCase {  
  13.       
  14.      public void testDemo() throws UiObjectNotFoundException {    
  15.             UiDevice device = getUiDevice();  
  16.             device.pressHome();    
  17.             // Start Notepad  
  18.             UiObject appNotes = new UiObject(new UiSelector().text("Notes"));   
  19.             appNotes.click();    
  20.             //Sleep 3 seconds till the app get ready  
  21.             try {    
  22.                 Thread.sleep(3000);    
  23.             } catch (InterruptedException e1) {    
  24.                 // TODO Auto-generated catch block    
  25.                 e1.printStackTrace();    
  26.             }    
  27.               
  28.             //Evoke the system menu option  
  29.             device.pressMenu();  
  30.             UiObject addNote = new UiObject(new UiSelector().text("Add note"));  
  31.             addNote.click();  
  32.               
  33.             //Add a new note  
  34.             UiObject noteContent = new UiObject(new UiSelector().className("android.widget.EditText"));  
  35.             noteContent.clearTextField();  
  36.             noteContent.setText("Note 1");  
  37.             device.pressMenu();  
  38.             UiObject save = new UiObject(new UiSelector().text("Save"));  
  39.             save.click();  
  40.               
  41.             //Find out the new added note entry  
  42.             UiScrollable noteList = new UiScrollable( new UiSelector().className("android.widget.ListView"));    
  43.             //UiScrollable noteList = new UiScrollable( new UiSelector().scrollable(true));   
  44.             UiObject note = null;  
  45.             if(noteList.exists()) {  
  46.                 note = noteList.getChildByText(new UiSelector().className("android.widget.TextView"), "Note1"true);    
  47.                 //note = noteList.getChildByText(new UiSelector().text("Note1"), "Note1", true);   
  48.             }  
  49.             else {  
  50.                 note = new UiObject(new UiSelector().text("Note1"));  
  51.             }  
  52.             assertThat(note,notNullValue());  
  53.               
  54.             note.longClick();  
  55.               
  56.             UiObject delete = new UiObject(new UiSelector().text("Delete"));  
  57.             delete.click();  
  58.                 
  59.         }    
  60.   
  61. }  

2. 問題分析解決

2.1 編譯問題分析

根據上圖的錯誤log,很明顯我們在實行ant build的時候ant並沒有把需要的第三方jar包加入進去進行編譯。

根據上一篇文章《Android自動化測試(UiAutomator)簡要介紹》描述,我們在打包UIAutomator項目時會執行一個命令“Android create uitest-project -n <name> -t <android-sdk-ID> -p <path>” 來在項目頂層目錄上生成一個build.xml文件,這個文件就ant用來build我們的UIAutomator項目需要用到的配置描述文件。那麼很自然我們就會想到去該文件下看是否有把我們需要的jar包給包含進來。

打開該文件查看時,發覺相當精簡,並沒有太多的東西可看,但是注意到文件末尾引用了我們Android SDK下面的一個文件“${sdk.dir}/tools/ant/uibuild.xml”:


打開該文件,裏面盡是build相關的配置,所以問題很有可能出現在這裏。

找到編譯相關的Section,確實沒有看到有指定第三方jar包的classpath:


2.2 編譯問題解決辦法

那麼很自然,我們應該在這裏指定我們第三方jar包的classpath,以便ant在build的時候知道從哪裏拿到我們的第三方包。


我這裏的例子是把我項目頂層目錄下的“libs”文件夾包含的jar包都引用進來,該目錄就是我存放第三方jar包的位置。

運行“ant build”,成功!

2.3 運行問題分析

build完成後,滿心歡喜的把編譯好的jar包push到安卓機器上運行,前期運行的沒有問題,但一到調用到第三方Jar包相關API的時候Exception就出來了


編譯沒有問題,運行時出現問題,那麼很有可能就是剛纔解決編譯問題的時候只是確保項目在編譯的時候能找到第三方jar包,但是並沒有在編譯後把相應的jar包一併打包到目標jar包裏面去。

經過一番google,相信還是build配置的問題,返回”${sdk.dir}/tools/ant/uibuild.xml“, 發現確實打包section沒有看到第三方jar包相應的信息:


2.4 運行問題解決辦法

根據google提示,最終修改成如下,問題最終解決!


轉載:http://blog.csdn.net/zhubaitian/article/details/39520007
發佈了25 篇原創文章 · 獲贊 17 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章