Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding external jars to an Android UIautomator project

There seem to be a decent number of questions about adding external jars to android projects and ant projects, but I'm not finding out a solution that works in this instance. I'm not too familiar with Ant, which probably exasperates the problem.

Problem is: I'm trying to add JSch libraries to my uiautomator project. I put the jsch.jar file into the /libs folder in hopes it would be found by 'android create uitest-project'. However, not the case - so it seems I need to modify build.xml/ant.properties/build.properties or something to get ant to find the jar

Specific error is:

[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

The build.xml is created by the android script, and Ant is used out of the box - so I think my scrub-knowledge of ant is the issue :P.

like image 940
Brian Avatar asked Dec 12 '22 15:12

Brian


1 Answers

Hi I meet the same problem.And i fix it follow below steps:

  • 1 create "libs" dirtory in project dirtory ,place all extrnal jar file in libs dirtory.
  • 2 add a custom_rules.xml file in project dirtory with below content.

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 select "build.xml" run as "Ant Build" every is ok.

  • 4 will generate tow jar file, the extra jar file is named "bundle.jar"

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

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

it works!

like image 63
Joe Zhong Avatar answered Dec 21 '22 10:12

Joe Zhong