将外部jar添加到Android UIautomator项目

时间:2013-04-15 23:33:37

标签: android jsch uiautomator

似乎有很多关于将外部jar添加到android项目和ant项目的问题,但我找不到适合这种情况的解决方案。我对Ant不太熟悉,这可能会加剧这个问题。

问题是:我正在尝试将JSch库添加到我的uiautomator项目中。我将jsch.jar文件放入/ libs文件夹中,希望它可以通过'android create uitest-project'找到。但事实并非如此 - 所以我似乎需要修改build.xml / ant.properties / build.properties或者其他东西来获取ant来找到jar

具体错误是:

[javac] Compiling 5 source files to /Users/.../KeyEvents/bin/classes
[javac] /Users/.../KeyEvents/src/com/proj/automation/core/coreSSH.java:9: error: package com.jcraft.jsch does not exist

build.xml是由android脚本创建的,Ant是开箱即用的 - 所以我认为我对蚂蚁的磨砂知识就是问题:P。

4 个答案:

答案 0 :(得分:9)

您好我遇到了同样的问题。我按照以下步骤修复:

  • 1在项目中创建“libs”文件,将所有extrnal jar文件放在libs中。
  • 2在项目内容中添加一个custom_rules.xml文件,其中包含以下内容。

https://github.com/xiaocong/android-uiautomator-jsonrpcserver/blob/master/custom_rules.xml

<!-- Include external libs -->
<property name="jar.libs.dir" value="libs" />
<property name="jar.libs.absolute.dir" location="${jar.libs.dir}" />
<path id="classpath">
    <fileset dir="${jar.libs.absolute.dir}">
        <include name="**/*.jar"/>
    </fileset>
</path>

<property name="dex.file.name" value="classes.dex" />
<property name="out.dir" value="bin" />
<property name="out.absolute.dir" location="${out.dir}" />
<property name="out.absolute.bundle.dir" location="${out.absolute.dir}/bundle" />
<property name="intermediate.dex.bundle.file" location="${out.absolute.bundle.dir}/${dex.file.name}" />

<property name="out.bundle.file" value="${out.absolute.dir}/bundle.jar" />

<target name="-pre-compile">
    <mkdir dir="${out.absolute.bundle.dir}" />
</target>

<!-- overwrite the compile target in uibuild.xml to include to external jars -->
<target name="compile" depends="-build-setup, -pre-compile">
    <javac encoding="${java.encoding}"
            source="${java.source}" target="${java.target}"
            debug="true" extdirs="" includeantruntime="false"
            destdir="${out.classes.absolute.dir}"
            bootclasspathref="project.target.class.path"
            verbose="${verbose}"
            fork="${need.javac.fork}">
        <src path="${source.absolute.dir}" />
        <classpath refid="classpath"/>
        <compilerarg line="${java.compilerargs}" />
    </javac>
</target>

<!-- empty default post-dex target. Create a similar target in
     your build.xml and it'll be called instead of this one. -->
<target name="-post-dex">
    <dex executable="${dx}"
            output="${intermediate.dex.bundle.file}"
            nolocals="@{nolocals}"
            verbose="${verbose}">
        <fileset dir="${jar.libs.absolute.dir}">
             <include name="**/*.jar"/>
        </fileset>
    </dex>
</target>

<!-- empty default post-jar target. Create a similar target in
     your build.xml and it'll be called instead of this one. -->
<target name="-post-jar">
    <jar destfile="${out.bundle.file}">
        <fileset file="${intermediate.dex.bundle.file}" />
    </jar>
</target>

<target name="install" description="Install the test package">
     <exec executable="${adb}" failonerror="true">
        <arg line="${adb.device.arg}" />
        <arg value="push" />
        <arg value="${out.file}" />
        <arg value="/data/local/tmp" />
    </exec>
     <exec executable="${adb}" failonerror="true">
        <arg line="${adb.device.arg}" />
        <arg value="push" />
        <arg value="${out.bundle.file}" />
        <arg value="/data/local/tmp" />
    </exec>
</target>

  • 3选择“build.xml”作为“Ant Build”运行,每次都可以。

  • 4将生成两个jar文件,额外的jar文件名为“bundle.jar”

  • 5推送/ data / local / tmp /

  • 中的bundle.jar
  • 6 adb shell uiautomator xxxTestCast.jar bunder.jar -c com.xxx.MainClass

它有效!

答案 1 :(得分:2)

看看这里[1]。只需复制此文件并将所需的jar放在libs目录中即可 运行ant build,ant install,最后运行::

adb shell uiautomator runtest YourTest.jar bundle.jar -c your.test

[1] - https://github.com/xiaocong/android-uiautomator-jsonrpcserver/blob/master/custom_rules.xml

答案 2 :(得分:2)

在项目中创建lib目录,然后在那里创建所有必需的jar然后创建新的custom_rule.xml并复制下面编写的代码,然后它将工作文件或者您也可以将此代码粘贴到build.xml的最后一行。

<property name="jar.libs.dir" value="libs" />
<property name="jar.libs.absolute.dir" location="${jar.libs.dir}" />
<path id="classpath">
    <fileset dir="${jar.libs.absolute.dir}">
        <include name="your-helping-version.jar" />
        <include name="gson-2.2.2.jar" />
    </fileset>
</path>

<!-- version-tag: VERSION_TAG -->
<import file="${sdk.dir}/tools/ant/uibuild.xml" />


<!-- overwrite the compile target in uibuild.xml to include to external 
    jars -->
<target name="compile" depends="-build-setup, -pre-compile">
    <javac encoding="${java.encoding}" source="${java.source}"
        target="${java.target}" debug="true" extdirs="" includeantruntime="false"
        destdir="${out.classes.absolute.dir}" bootclasspathref="project.target.class.path"
        verbose="${verbose}" fork="${need.javac.fork}">
        <src path="${source.absolute.dir}" />
        <classpath refid="classpath" />
        <compilerarg line="${java.compilerargs}" />
    </javac>
</target>

<!-- overwrite the -dex target in uibuild.xml to include external jar files 
    into the target dex file. -->
<target name="-dex" depends="compile, -post-compile">
    <dex executable="${dx}" output="${intermediate.dex.file}"
        nolocals="@{nolocals}" verbose="${verbose}">
        <fileset dir="${jar.libs.absolute.dir}">
            <include name="your-helping-version.jar" />
            <include name="gson-2.2.2.jar" /> 
        </fileset>
        <path path="${out.classes.absolute.dir}" />
    </dex>
</target>

答案 3 :(得分:0)

如果你正在使用eclipse

在Project Explorer中,右键单击您创建的新项目,然后选择Properties&gt; Java Build Path,并执行以下操作: 单击Add External JARs ...并导航到该目录并添加jar文件