💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
本文章主要描述UIAutomator项目中引用到第三方Jar包的时候,按照正常的打包方式碰到的各种问题,以及最终解决的思路和办法。 ## 1. 问题起源 在本人的一个示例项目中引用到了单元测试框架hamcrest的jar包,在项目目录下执行ant build的时候出现以下的问题 ![](https://box.kancloud.cn/2016-08-12_57ad6e272fee5.jpg) 源码如下: ~~~ 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)简要介绍](http://blog.csdn.net/zhubaitian/article/details/39520069)》描述,我们在打包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”:![](https://box.kancloud.cn/2016-08-12_57ad6e2768b32.jpg) 打开该文件,里面尽是build相关的配置,所以问题很有可能出现在这里。 找到编译相关的Section,确实没有看到有指定第三方jar包的classpath:![](https://box.kancloud.cn/2016-08-12_57ad6e278cdf8.jpg) ### 2.2 编译问题解决办法 那么很自然,我们应该在这里指定我们第三方jar包的classpath,以便ant在build的时候知道从哪里拿到我们的第三方包。 ![](https://box.kancloud.cn/2016-08-12_57ad6e27b1c3a.jpg) 我这里的例子是把我项目顶层目录下的“libs”文件夹包含的jar包都引用进来,该目录就是我存放第三方jar包的位置。 运行“ant build”,成功! ### 2.3 运行问题分析 build完成后,满心欢喜的把编译好的jar包push到安卓机器上运行,前期运行的没有问题,但一到调用到第三方Jar包相关API的时候Exception就出来了![](https://box.kancloud.cn/2016-08-12_57ad6e27dada6.jpg) 编译没有问题,运行时出现问题,那么很有可能就是刚才解决编译问题的时候只是确保项目在编译的时候能找到第三方jar包,但是并没有在编译后把相应的jar包一并打包到目标jar包里面去。 经过一番google,相信还是build配置的问题,返回”${sdk.dir}/tools/ant/uibuild.xml“, 发现确实打包section没有看到第三方jar包相应的信息:![](https://box.kancloud.cn/2016-08-12_57ad6e280cd74.jpg) ### 2.4 运行问题解决办法 根据google提示,最终修改成如下,问题最终解决! ![](https://box.kancloud.cn/2016-08-12_57ad6e285dbc1.jpg) <table cellspacing="0" cellpadding="0" width="539" class=" " style="margin:0px 0px 10px; padding:0px; border-collapse:collapse; width:668px; max-width:100%; word-wrap:break-word!important"><tbody style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><tr style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><td valign="top" width="112" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important"> </td></tr><tr style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><td valign="top" width="111" height="13" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(190,192,191)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">作者</span></p></td><td valign="top" width="112" height="13" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(190,192,191)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">自主博客</span></p></td><td valign="top" width="111" height="13" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(190,192,191)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">微信</span></p></td><td valign="top" width="112" height="13" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(190,192,191)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important">CSDN</span></p></td></tr><tr style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"><td valign="top" width="111" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important; background-color:rgb(227,228,228)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">天地会珠海分舵</span></p></td><td valign="top" width="112" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; font-size:11px; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important"><a target="_blank" href="http://techgogogo.com/">http://techgogogo.com</a></span><span style="margin:0px; padding:0px; max-width:100%; font-family:Helvetica; font-size:11px; letter-spacing:0px; word-wrap:break-word!important"/></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:14px; white-space:pre-wrap; font-family:Helvetica; word-wrap:break-word!important"><br style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important"/></p></td><td valign="top" width="111" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">服务号</span><span style="margin:0px; padding:0px; max-width:100%; font-size:10px; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important">:TechGoGoGo</span></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; word-wrap:break-word!important">扫描码</span><span style="margin:0px; padding:0px; max-width:100%; font-size:10px; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important">:</span></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:14px; white-space:pre-wrap; font-family:Helvetica; word-wrap:break-word!important"><img src="image/47cf4f9ec59b0ef1f807a6c33ab5ce5f.jpg" alt="" style="max-width:100%; margin:0px; padding:0px; height:auto!important; word-wrap:break-word!important; width:auto!important; visibility:visible!important"/></p></td><td valign="top" width="112" height="39" style="border-style:solid; border-color:rgb(0,0,0); margin:0px; padding:4px; word-break:break-all; max-width:100%; word-wrap:break-word!important"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; max-width:100%; clear:both; min-height:1em; white-space:pre-wrap; color:rgb(62,62,62); font-family:'Helvetica Neue',Helvetica,'Hiragino Sans GB','Microsoft YaHei',΢ÈíÑźÚ,Arial,sans-serif; font-size:18px; line-height:28.7999992370605px; word-wrap:break-word!important"><span style="margin:0px; padding:0px; max-width:100%; color:rgb(0,0,0); font-size:11px; font-family:Helvetica; letter-spacing:0px; word-wrap:break-word!important"><a target="_blank" href="http://blog.csdn.net/zhubaitian">http://blog.csdn.net/zhubaitian</a></span><span style="margin:0px; padding:0px; max-width:100%; color:rgb(0,0,0); font-family:Helvetica; font-size:11px; letter-spacing:0px; line-height:28.7999992370605px; word-wrap:break-word!important"/></p><div><span style="margin:0px; padding:0px; max-width:100%; color:rgb(0,0,0); font-family:Helvetica; font-size:11px; letter-spacing:0px; line-height:28.7999992370605px; word-wrap:break-word!important"><br/></span></div></td></tr></tbody></table>