4

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.

Brian
  • 857
  • 2
  • 12
  • 25
  • Sorry, I was focused on the runtime aspect, so I got rid of my answer (to help encourage other answers). In Eclipse, you'd add it as an external JAR on your build path. If Ant is not picking up `libs/`... I haven't the foggiest how to fix that. If you get no love here, try the `adt-dev` Google Group -- this may just be a missing feature on the Ant script side. – CommonsWare Apr 16 '13 at 00:03
  • @CommonsWare Thanks for the update! You encouraged me to dig into the ant build scripts. While I still don't know what to do exactly, it seems the uitest.xml that is imported into build.xml doesn't include any external jar files during the actual build process - although the reference id's are there. This feels a bit like they copy-pasted from their other parts, and removed external libraries from the compilation. That is speculation mostly. I just need to figure out how to add them back if that is the case :S – Brian Apr 16 '13 at 00:22
  • Any update on this? I seem to have stumbled on the same problem. – aragaer Sep 01 '13 at 16:30
  • Any updates on the same issue ? – AndroidGuy Aug 07 '14 at 09:46

4 Answers4

9

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!

Joe Zhong
  • 91
  • 1
  • 3
2

take a look here [1]. Just copy this file and put the wanted jar in a directory libs, so run ant build, ant install and 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

Wiliam Souza
  • 31
  • 1
  • 3
2

create lib directory in project keep all the required jar there then create new custom_rule.xml and copy below written code, then it will work file Or you can paste this code in the last of build.xml too.

<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>
neeraj t
  • 4,654
  • 2
  • 27
  • 30
0

if you are using eclipse

From the Project Explorer, right-click on the new project that you created, then select Properties > Java Build Path, and do the following: Click Add External JARs... and navigate to the directory and add jar files

p0sitr0n
  • 1
  • 2