uiautomator開發使用到引用外部jar,導致編譯失敗的解決方案

首先,感謝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文件中,快速解決打包問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章