uiautomator开发使用到引用外部jar,导致编译失败的解决方案

来源:互联网 发布:内帐软件 编辑:程序博客网 时间:2024/05/18 10:07

首先,感谢CrystalChenxiaoqing同学的整理,转载于:http://blog.csdn.net/cxq234843654/article/details/50350435

最近做安卓的遍历测试,是基于uiautomator做的,二次开发,避免不了使用第三方jar,比如这边我使用到的就是xStream,但是在打包的过程中会出现

classNotFound或者是Build Failed的问题。


这里有两个原因:

1、编译的时候,没有把jar包添加进来,这时候的体现一般是报BUILD FAILED的错误。

2、jar包添加进来了,没有把jar下的class文件的路径对应放到classes.dex文件中,造成类无法找到,这时候一般会报classNotFound的错误。


针对以上两个问题,我们需要对应修改uiBuild.xml文件,这个文件是ant自带的,目录为${sdk.dir}/tools/ant/uibuild.xml


1、将jar包的路径,放到compile下,红色部分为新增内容,意思是将项目的libs文件夹下的所有jar包都加入编译。

 <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}" />
            <compilerarg line="${java.compilerargs}" />
            <classpath>
                <fileset dir="${jar.libs.dir}" includes="*.jar"/>
            </classpath>

        </javac>
    </target>


2、把class的路径加入到classes.dex文件中,红色部分为新增内容。

    <target name="-dex" depends="compile, -post-compile">
        <dex executable="${dx}"
                output="${intermediate.dex.file}"
                nolocals="@{nolocals}"
                verbose="${verbose}">
           <fileset dir="${jar.libs.dir}" includes="*.jar"/>
            <path path="${out.classes.absolute.dir}"/>
        </dex>
    </target>


最后修改后的uibuild.xml文件内容如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project name="android_rules" default="debug">  
  3.   
  4.     <!-- This build file is imported by the project build file. It contains   
  5.         all the targets and tasks necessary to build Android projects, be they regular   
  6.         projects, library projects, or test projects. At the beginning of the file   
  7.         is a list of properties that can be overridden by adding them to your ant.properties   
  8.         (properties are immutable, so their first definition sticks and is never   
  9.         changed). Follows: - custom task definitions, - more properties (do not override   
  10.         those unless the whole build system is modified). - macros used throughout   
  11.         the build, - base build targets, - debug-specific build targets, - release-specific   
  12.         build targets, - instrument-specific build targets, - test project-specific   
  13.         build targets, - install targets, - help target -->  
  14.   
  15.     <!-- ******************************************************* -->  
  16.     <!-- **************** Overridable Properties *************** -->  
  17.     <!-- ******************************************************* -->  
  18.   
  19.     <!-- You can override these values in your build.xml or ant.properties.   
  20.         Overriding any other properties may result in broken build. -->  
  21.   
  22.     <!-- Tells adb which device to target. You can change this from the command   
  23.         line by invoking "ant -Dadb.device.arg=-d" for device "ant -Dadb.device.arg=-e"   
  24.         for the emulator. -->  
  25.     <property name="adb.device.arg" value="" />  
  26.   
  27.     <!-- filename only of the output file. Cannot be a path -->  
  28.     <property name="out.filename" value="${ant.project.name}.jar" />  
  29.   
  30.     <!-- compilation options -->  
  31.     <property name="java.encoding" value="UTF-8" />  
  32.     <property name="java.target" value="1.5" />  
  33.     <property name="java.source" value="1.5" />  
  34.     <property name="java.compilerargs" value="" />  
  35.   
  36.     <!-- Verbosity -->  
  37.     <property name="verbose" value="false" />  
  38.   
  39.     <!-- ******************************************************* -->  
  40.     <!-- ********************* Custom Tasks ******************** -->  
  41.     <!-- ******************************************************* -->  
  42.   
  43.     <!-- jar file from where the tasks are loaded -->  
  44.     <path id="android.antlibs">  
  45.         <pathelement path="${sdk.dir}/tools/lib/ant-tasks.jar" />  
  46.     </path>  
  47.   
  48.     <!-- Custom tasks -->  
  49.     <taskdef resource="anttasks.properties" classpathref="android.antlibs" />  
  50.   
  51.     <!-- Emma configuration -->  
  52.     <property name="emma.dir" value="${sdk.dir}/tools/lib" />  
  53.     <path id="emma.lib">  
  54.         <pathelement location="${emma.dir}/emma.jar" />  
  55.         <pathelement location="${emma.dir}/emma_ant.jar" />  
  56.     </path>  
  57.     <taskdef resource="emma_ant.properties" classpathref="emma.lib" />  
  58.     <!-- End of emma configuration -->  
  59.   
  60.   
  61.     <!-- ******************************************************* -->  
  62.     <!-- ******************* Other Properties ****************** -->  
  63.     <!-- ******************************************************* -->  
  64.     <!-- overriding these properties may break the build unless the whole file   
  65.         is updated -->  
  66.   
  67.     <!-- Input directories -->  
  68.     <property name="source.dir" value="src" />  
  69.     <property name="source.absolute.dir" location="${source.dir}" />  
  70.     <property name="jar.libs.dir" value="libs" />  
  71.     <property name="jar.libs.absolute.dir" location="${jar.libs.dir}" />  
  72.   
  73.     <!-- Output directories -->  
  74.     <property name="out.dir" value="bin" />  
  75.     <property name="out.absolute.dir" location="${out.dir}" />  
  76.     <property name="out.classes.absolute.dir" location="${out.dir}/classes" />  
  77.   
  78.     <property name="out.file" value="${out.absolute.dir}/${out.filename}" />  
  79.   
  80.     <!-- tools location -->  
  81.     <property name="android.tools.dir" location="${sdk.dir}/tools" />  
  82.     <property name="android.platform.tools.dir" location="${sdk.dir}/platform-tools" />  
  83.     <condition property="exe" value=".exe" else="">  
  84.         <os family="windows" />  
  85.     </condition>  
  86.     <condition property="bat" value=".bat" else="">  
  87.         <os family="windows" />  
  88.     </condition>  
  89.     <property name="adb" location="${android.platform.tools.dir}/adb${exe}" />  
  90.   
  91.     <!-- Intermediate files -->  
  92.     <property name="dex.file.name" value="classes.dex" />  
  93.     <property name="intermediate.dex.file" location="${out.absolute.dir}/${dex.file.name}" />  
  94.     <property name="resource.package.file.name" value="${ant.project.name}.ap_" />  
  95.   
  96.     <!-- whether we need to fork javac. This is only needed on Windows when   
  97.         running Java < 7 -->  
  98.     <condition else="false" property="need.javac.fork">  
  99.         <and>  
  100.             <matches pattern="1\.[56]" string="${java.specification.version}" />  
  101.             <not>  
  102.                 <os family="unix" />  
  103.             </not>  
  104.         </and>  
  105.     </condition>  
  106.   
  107.     <macrodef name="run-tests-helper">  
  108.         <attribute name="emma.enabled" default="false" />  
  109.         <element name="extra-instrument-args" optional="yes" />  
  110.         <sequential>  
  111.             <echo level="info">Running tests ...</echo>  
  112.             <exec executable="${adb}" failonerror="true">  
  113.                 <arg line="${adb.device.arg}" />  
  114.                 <arg value="shell" />  
  115.                 <arg value="am" />  
  116.                 <arg value="instrument" />  
  117.                 <arg value="-w" />  
  118.                 <arg value="-e" />  
  119.                 <arg value="coverage" />  
  120.                 <arg value="@{emma.enabled}" />  
  121.                 <extra-instrument-args />  
  122.                 <arg value="${project.app.package}/${test.runner}" />  
  123.             </exec>  
  124.         </sequential>  
  125.     </macrodef>  
  126.   
  127.     <!-- ******************************************************* -->  
  128.     <!-- ******************** Build Targets ******************** -->  
  129.     <!-- ******************************************************* -->  
  130.   
  131.     <!-- Basic Ant + SDK check -->  
  132.     <target name="-check-env">  
  133.         <checkenv />  
  134.     </target>  
  135.   
  136.     <!-- empty default pre-clean target. Create a similar target in your build.xml   
  137.         and it'll be called instead of this one. -->  
  138.     <target name="-pre-clean" />  
  139.   
  140.     <!-- clean target -->  
  141.     <target name="clean" depends="-check-env, -pre-clean"  
  142.         description="Removes output files created by other targets.">  
  143.         <delete dir="${out.absolute.dir}" verbose="${verbose}" />  
  144.     </target>  
  145.   
  146.     <!-- Pre build setup -->  
  147.     <target name="-build-setup" depends="-check-env">  
  148.         <getbuildtools name="android.build.tools.dir" />  
  149.         <property name="dx" location="${android.build.tools.dir}/dx${bat}" />  
  150.   
  151.         <echo level="info">Resolving Build Target for ${ant.project.name}...  
  152.         </echo>  
  153.         <!-- load project properties, resolve Android target, library dependencies   
  154.             and set some properties with the results. All property names are passed as   
  155.             parameters ending in -Out -->  
  156.         <getuitarget compileClassPathOut="project.target.class.path" />  
  157.   
  158.         <echo level="info">----------</echo>  
  159.         <echo level="info">Creating output directories if needed...</echo>  
  160.         <mkdir dir="${out.absolute.dir}" />  
  161.         <mkdir dir="${out.classes.absolute.dir}" />  
  162.   
  163.     </target>  
  164.   
  165.     <!-- empty default pre-compile target. Create a similar target in your build.xml   
  166.         and it'll be called instead of this one. -->  
  167.     <target name="-pre-compile" />  
  168.   
  169.     <!-- Compiles this project's .java files into .class files. -->  
  170.     <target name="compile" depends="-build-setup, -pre-compile">  
  171.         <javac encoding="${java.encoding}" source="${java.source}"  
  172.             target="${java.target}" debug="true" extdirs="" includeantruntime="false"  
  173.             destdir="${out.classes.absolute.dir}" bootclasspathref="project.target.class.path"  
  174.             verbose="${verbose}" fork="${need.javac.fork}">  
  175.             <src path="${source.absolute.dir}" />  
  176.             <compilerarg line="${java.compilerargs}" />  
  177.             <!-- 修改位置1 -->  
  178.             <classpath>  
  179.                 <fileset dir="${jar.libs.dir}" includes="*.jar" />  
  180.             </classpath>  
  181.             <!-- 修改位置1 -->  
  182.         </javac>  
  183.     </target>  
  184.   
  185.     <!-- empty default post-compile target. Create a similar target in your   
  186.         build.xml and it'll be called instead of this one. -->  
  187.     <target name="-post-compile" />  
  188.   
  189.     <!-- Converts this project's .class files into .dex files -->  
  190.     <target name="-dex" depends="compile, -post-compile">  
  191.         <dex executable="${dx}" output="${intermediate.dex.file}"  
  192.             nolocals="@{nolocals}" verbose="${verbose}">  
  193.             <!-- 修改位置2 -->  
  194.             <fileset dir="${jar.libs.dir}" includes="*.jar" />  
  195.             <!-- 修改位置2 -->  
  196.             <path path="${out.classes.absolute.dir}" />  
  197.         </dex>  
  198.     </target>  
  199.   
  200.     <!-- empty default post-dex target. Create a similar target in your build.xml   
  201.         and it'll be called instead of this one. -->  
  202.     <target name="-post-dex" />  
  203.   
  204.     <target name="-jar" depends="-dex, -post-dex">  
  205.         <jar destfile="${out.file}">  
  206.             <fileset file="${intermediate.dex.file}" />  
  207.         </jar>  
  208.     </target>  
  209.   
  210.     <!-- empty default post-jar target. Create a similar target in your build.xml   
  211.         and it'll be called instead of this one. -->  
  212.     <target name="-post-jar" />  
  213.   
  214.     <target name="build" depends="-jar, -post-jar" />  
  215.   
  216.     <target name="install" description="Install the test package">  
  217.         <exec executable="${adb}" failonerror="true">  
  218.             <arg line="${adb.device.arg}" />  
  219.             <arg value="push" />  
  220.             <arg value="${out.file}" />  
  221.             <arg value="/data/local/tmp" />  
  222.         </exec>  
  223.     </target>  
  224.   
  225.     <target name="test" description="Runs tests">  
  226.         <!-- todo: fix this -->  
  227.         <fail message="Launching tests from Ant not supported yet" />  
  228.   
  229.         <exec executable="${adb}" failonerror="true">  
  230.             <arg line="${adb.device.arg}" />  
  231.             <arg value="shell" />  
  232.             <arg value="uiautomator" />  
  233.             <arg value="runtest" />  
  234.             <arg value="${out.filename}" />  
  235.             <arg value="-e" />  
  236.             <arg value="class" />  
  237.             <arg value="com.android.uiautomator.samples.skeleton.DemoTestCase" />  
  238.         </exec>  
  239.     </target>  
  240.   
  241.     <target name="help">  
  242.         <!-- displays starts at col 13 |13 80| -->  
  243.         <echo>Android Ant Build. Available targets:</echo>  
  244.         <echo> help: Displays this help.</echo>  
  245.         <echo> clean: Removes output files created by other targets.</echo>  
  246.         <echo> build: Builds the test library.</echo>  
  247.         <echo> install: Installs the library on a connected device or</echo>  
  248.         <echo> emulator.</echo>  
  249.         <echo> test: Runs the tests.</echo>  
  250.         <echo></echo>  
  251.         <echo>It is possible to mix targets. For instance:</echo>  
  252.         <echo> ant build install test</echo>  
  253.         <echo>This will build, install and run the test in a single command.  
  254.         </echo>  
  255.     </target>  
  256.   
  257. </project>  


大家也可以将以上内容直接复制到现有的uibuild.xml文件中,快速解决打包问题。

0 0
原创粉丝点击