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

來源:互聯網
上載者:User

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

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

1. 問題起源

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


源碼如下:

package majcit.com.UIAutomatorDemo;import com.android.uiautomator.core.UiDevice;import com.android.uiautomator.core.UiObject;import com.android.uiautomator.core.UiObjectNotFoundException;import com.android.uiautomator.core.UiScrollable;import com.android.uiautomator.core.UiSelector;import com.android.uiautomator.testrunner.UiAutomatorTestCase;import static org.hamcrest.Matchers.*;import static org.hamcrest.MatcherAssert.assertThat;public class NotePadTest extends UiAutomatorTestCase { public void testDemo() throws UiObjectNotFoundException {          UiDevice device = getUiDevice();        device.pressHome();          // Start Notepad        UiObject appNotes = new UiObject(new UiSelector().text("Notes"));         appNotes.click();          //Sleep 3 seconds till the app get ready        try {              Thread.sleep(3000);          } catch (InterruptedException e1) {              // TODO Auto-generated catch block              e1.printStackTrace();          }                  //Evoke the system menu option        device.pressMenu();        UiObject addNote = new UiObject(new UiSelector().text("Add note"));        addNote.click();                //Add a new note        UiObject noteContent = new UiObject(new UiSelector().className("android.widget.EditText"));        noteContent.clearTextField();        noteContent.setText("Note 1");        device.pressMenu();        UiObject save = new UiObject(new UiSelector().text("Save"));        save.click();                //Find out the new added note entry        UiScrollable noteList = new UiScrollable( new UiSelector().className("android.widget.ListView"));          //UiScrollable noteList = new UiScrollable( new UiSelector().scrollable(true));         UiObject note = null;        if(noteList.exists()) {        note = noteList.getChildByText(new UiSelector().className("android.widget.TextView"), "Note1", true);          //note = noteList.getChildByText(new UiSelector().text("Note1"), "Note1", true);         }        else {        note = new UiObject(new UiSelector().text("Note1"));        }        assertThat(note,notNullValue());                note.longClick();                UiObject delete = new UiObject(new UiSelector().text("Delete"));        delete.click();              }  }

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提示,最終修改成如下,問題最終解決!



Ant 怎打包第三方的jar

android工程A關聯了另一工程B(可以在buildpath-->LinkSource去關聯)用ant的時候,方法與單獨build一個工程師沒多大的區別先把B工程編譯打成jar包(可用Eclipse直接Export出jar包),然後放到A工程下的libs中去再用ant去buildA工程即可~
 
android工程中添加了 preject library第三方包(是一個工程,並非jar),問用ant怎打包?

android工程A關聯了另一工程B(可以在build path -->Link Source去關聯)

用ant的時候,方法與單獨build一個工程師沒多大的區別

先把B工程編譯打成jar包(可用Eclipse直接Export出jar包),然後放到A工程下的libs中去

再用ant去buildA工程即可~
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.