如何在eclipse中引用第三方jar包


  在用UiAutomator做手機自動化測試過程中,在UiAutomator的基礎之上進一步封裝了里邊的方法,以使case開發更順手。直接在工程的根目錄下新建了個libs的文件夾,把封裝好的框架打成jar包,放到了libs中,之后在build.xml中加入了其路徑,以便ant build可以成功。

  但之后提交git就出現問題了,每個工程都有一份libs,且其中都是框架的jar包,N多份同樣的東西,可想而知,這樣的代碼,無論從哪個方面來說都是不合格的,於是想着是不是能否處理成像android.jar一樣可以添加進來的library,這樣就不用每次都提交git了,只需要把框架的jar包提交git,每次下載下來之后,放到指定路徑下,在添加到build path里邊即可。在高手的指引下,終於實現了!好開森,美麗的星期一!

  先把你想引用的jar包放到指定路徑下,如圖1所示。

  

  打開eclipse的Windows->Preferences,在Java菜單下,選擇User Libraries,點擊New,添加一個新的user library,暫時命名為Uiautomatorlib,點擊OK,之后點擊右側的Add External JARS,找到你剛才放到指定路徑下的jar包,如圖2所示。

  在你所想引用jar包的工程上名字上單擊右鍵,選擇Build Path->Configure build path,點擊右側的Add Library....,選擇User Library,點擊Finish,如圖3所示。

之后在build path中會看到有了Uiautomatorlib這個library,同時在工程目錄下,也多出來了此library,如圖4所示。

最重要的一步,修改build.xml,如圖5所示。

具體的build.xml文件代碼,如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project name="AutoRunner" default="build">
 3     <property file="local.properties" />
 4     <property file="ant.properties" />
 5     <property environment="env" />
 6     <condition property="sdk.dir" value="${env.ANDROID_HOME}">
 7         <isset property="env.ANDROID_HOME" />
 8     </condition>
 9     <loadproperties srcFile="project.properties" />
10     <fail
11             message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
12             unless="sdk.dir"
13     />
14     <import file="custom_rules.xml" optional="true" />
15 
16     <!-- version-tag: VERSION_TAG -->
17     <import file="${sdk.dir}/tools/ant/uibuild.xml" />
18     <target name="compile" depends="-build-setup, -pre-compile">
19         <javac encoding="${java.encoding}"
20                 source="${java.source}" target="${java.target}"
21                 debug="true" extdirs="" includeantruntime="false"
22                 destdir="${out.classes.absolute.dir}"
23                 bootclasspathref="project.target.class.path"
24                 classpathref="reflibrary"
25                 verbose="${verbose}"
26                 fork="${need.javac.fork}">
27             <src path="${source.absolute.dir}" />
28             <compilerarg line="${java.compilerargs}" />
29         </javac>
30     </target>
31 
32     <path id="reflibrary">
33         <fileset dir="C:\Libs\uiautomator">
34             <include name="*.jar" />
35         </fileset>
36     </path>
37 
38     <target name="-dex" depends="compile, -post-compile">
39         <dex executable="${dx}"
40                 output="${intermediate.dex.file}"
41                 nolocals="@{nolocals}"
42                 verbose="${verbose}">
43             <path path="${out.classes.absolute.dir}"/>
44             <fileset dir="libs" includes="*.jar" erroronmissingdir="false"/>
45         </dex>
46     </target>
47 
48     <target name="prepare" depends="clean,build"></target>
49 </project>

 

接下來就可以在你的工程中使用這個jar包,且提交git時無需提交此jar,只需要從git pull下來腳本和所需要的框架jar包,如上所述,新建user library,加入到build path,修改build.xml文件,就可以ant build啦!

So easy!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM