将外部jar添加到Android UIautomator项目中



关于将外部jar添加到android项目和ant项目中似乎有相当数量的问题,但我没有找到在这种情况下有效的解决方案。我对Ant不太熟悉,这可能会使问题更加严重。

问题是:我试图将JSch库添加到我的uiautomator项目。我将jsch.jar文件放入/libs文件夹中,希望通过"android create uitest-project"可以找到它。然而,情况并非如此-所以我似乎需要修改build.xml/ant.properties/build。属性之类的让蚂蚁找到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是开箱即用的——所以我认为我对Ant的初步了解是问题所在:p

你好,我遇到了同样的问题。我按照下面的步骤修复它:

    1在项目目录中创建"libs"目录,将所有外部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"每一个都是ok的

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

  • 5 push bundle.jar in/data/local/tmp/

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

它工作!

看这里[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

在项目中创建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>

如果您使用的是eclipse

在项目资源管理器中,右键单击您创建的新项目,然后选择Properties> Java Build Path,并执行以下操作:单击Add External jar…然后导航到目录并添加jar文件

最新更新