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

时间:2024-04-17 16:48:19

  在用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!!!