Generating android code coverage though changes in build.xml and ant.properties

一笑奈何 提交于 2019-12-06 14:08:24

I am also working on the same from quite some time. Refer to code coverage reports for library projects

External jars coverage support is in ADT-r20. I can only point to the direction I am working. I am working on modifying build.xml inside ant :

 <!-- This is macro that enable passing variable list of external jar files to ApkBuilder
     Example of use:
     <package-helper>
         <extra-jars>
            <jarfolder path="my_jars" />
            <jarfile path="foo/bar.jar" />
            <jarfolder path="your_jars" />
         </extra-jars>
     </package-helper> -->
<macrodef name="package-helper">
    <element name="extra-jars" optional="yes" />
    <sequential>
        <apkbuilder
                outfolder="${out.absolute.dir}"
                resourcefile="${resource.package.file.name}"
                apkfilepath="${out.packaged.file}"
                debugpackaging="${build.is.packaging.debug}"
                debugsigning="${build.is.signing.debug}"
                verbose="${verbose}"
                hascode="${manifest.hasCode}"
                previousBuildType="${build.last.is.packaging.debug}/${build.last.is.signing.debug}"
                buildType="${build.is.packaging.debug}/${build.is.signing.debug}">
            <dex path="${intermediate.dex.file}"/>
            <sourcefolder path="${source.absolute.dir}"/>
            <jarfile refid="project.all.jars.path" />
            <nativefolder path="${native.libs.absolute.dir}" />
            <nativefolder refid="project.library.native.folder.path" />
            <extra-jars/>
        </apkbuilder>
    </sequential>
</macrodef>

I have no luck till yet.

After lots of searching and trying , finally got the package of my External jar in my Main project. Just entered tested.android.library.source.dir in ant.properties and build.xml

ant.properties: tested.android.library.source.dir="Path to libs folder of main project where jars are present" Build.xml : Under emma tag in report tag add this variable seperated by colon.

Now go to command prompt and run

In Main Project :android update project -p . In Test Project: android update test-project -m "Path of main project" -p .

Now copy test target from build.xml (sdk/tools/ant/build.xml) and paste it in build.xml of test project above the line

 <import file="${sdk.dir}/tools/ant/build.xml" />

Dont Forget to Change Version Tag to

Now Again open command prompt and run:

In Main Project: ant emma debug install In Test Project: ant emma debug install test

Your Code coverage report generated will contain the package of the external jar

I struggled with this for 2 days and eventually figured it out. The code snippet in the first post only generates instrument report from emma metadatafile and runtime coverage file but does not perform instrumentation on jar. To instrument the code you will have to manipulate the byte code, like how android ant build.xml target does it. Take a look at the element nested in <-compile> and you will see comment like it is only instrumenting class files. To instrument the jars add a classpath like element to the jars along with path to class.

Emma Documentation: http://emma.sourceforge.net/reference/ch02s03s02.html

<emma enabled="${emma.enabled}" >
  <instr mode="fullcopy"
         outdir="${out.instr.dir}"
         merge="no"
         filter="${emma.filter}">
    <instrpath>
      <fileset dir="${out.dir}" includes="**/*.jar" />
    </instrpath>
  </instr>
</emma>

In Android's build.xml. One just have to delete instrpath attribute and make it look like the example above.

 756                     <emma enabled="true">
 757                         <instr verbosity="${verbosity}"
 758                                mode="overwrite"
 759                                instrpath="${out.absolute.dir}/classes"
 760                                outdir="${out.absolute.dir}/classes"
 761                                metadatafile="${emma.coverage.absolute.file}">
 762                             <filter excludes="${emma.default.filter}" />
 763                             <filter value="${emma.filter}" />
 764                         </instr>
 765                     </emma>

So something like this: (pardon the line#s)

 756                     <emma enabled="true">
 757                         <instr verbosity="${verbosity}"
 758                                mode="overwrite"
 760                                outdir="${out.absolute.dir}/classes"
 761                                metadatafile="${emma.coverage.absolute.file}">
 759                                <instrpath>
 759                                     <pathelement path="${out.absolute.dir}/classes"/>
 759                                     <fileset dir="${you-class-path}"/>
 759                                          <include name="**/*.jar"/>
 759                                     </fileset>
 759                                </instrpath>
 762                             <filter excludes="${emma.default.filter}" />
 763                             <filter value="${emma.filter}" />
 764                         </instr>
 765                     </emma>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!