【问题标题】:Adding external jars to an Android UIautomator project将外部 jars 添加到 Android UIautomator 项目
【发布时间】:2014-02-21 17:22:32
【问题描述】:

似乎有很多关于向 android 项目和 ant 项目添加外部 jar 的问题,但我没有找到适用于这种情况的解决方案。我对 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 是开箱即用的 - 所以我认为我对 ant 的清理知识是问题所在:P。

【问题讨论】:

  • 对不起,我专注于运行时方面,所以我放弃了我的答案(以帮助鼓励其他答案)。在 Eclipse 中,您可以将其作为外部 JAR 添加到构建路径中。如果 Ant 没有接听 libs/... 我还没有最模糊的解决方法。如果您在这里不受欢迎,请尝试adt-dev Google Group -- 这可能只是 Ant 脚本方面缺少的功能。
  • @CommonsWare 感谢您的更新!您鼓励我深入研究 ant 构建脚本。虽然我仍然不知道具体要做什么,但在实际构建过程中,导入 build.xml 的 uitest.xml 似乎不包含任何外部 jar 文件 - 尽管参考 ID 在那里。这感觉有点像他们从其他部分复制粘贴,并从编译中删除了外部库。那主要是猜测。如果是这种情况,我只需要弄清楚如何将它们添加回来:S
  • 这有什么更新吗?我似乎偶然发现了同样的问题。
  • 关于同一问题的任何更新?

标签: android jsch android-uiautomator


【解决方案1】:

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

  • 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”,每次都可以。

  • 4会生成tow jar文件,额外的jar文件命名为“bundle.jar”

  • 5 将 bundle.jar 推送到 /data/local/tmp/

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

有效!

【讨论】:

    【解决方案2】:

    看看这里 [1]。只需复制此文件并将想要的 jar 放在目录 libs 中,所以 运行 ant build、ant install 和 finally::

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        如果你使用的是 eclipse

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-03
          • 2011-02-06
          • 1970-01-01
          • 1970-01-01
          • 2013-09-02
          • 2013-07-02
          • 2011-08-07
          相关资源
          最近更新 更多