I'm new to EMMA, I don't know how to use this for android system.
Can anyone please give a sample for using this with android.
Thanks a lot.
I've only been able to get emma working using ant. If you've got an ant build set up then you can run:
ant emma debug install
ant emma debug install test
The first is run in your project directory, the second from your test directory. See the docs for more details: http://developer.android.com/guide/developing/building/building-cmdline.html
If you don't have an ant build.xml file already you can see how to generate one from your current project here: http://developer.android.com/guide/developing/projects/projects-cmdline.html
The sad part is this will only work on the emulator or a rooted device. This is because the coverage file gets generated in a folder that requires root. I also needed to modify the android-sdk/tools/ant/build.xml file to copy the file on my rooted device elsewhere so I could pull it off. I modified the emma block of the xml to be the following:
<if condition="${emma.enabled}">
<then>
<echo>WARNING: Code Coverage is currently only supported on the emulator and rooted devices.</echo>
<run-tests-helper emma.enabled="true">
<extra-instrument-args>
<arg value="-e" />
<arg value="coverageFile" />
<arg value="${emma.dump.file}" />
</extra-instrument-args>
</run-tests-helper>
<echo>Copying coverage to readable directory...</echo>
<exec executable="${adb}" failonerror="true">
<arg line="${adb.device.arg}" />
<arg value="shell" />
<arg value="echo cp ${emma.dump.file} /sdcard/coverage.ec | su" />
</exec>
<echo>Downloading coverage file into project directory...</echo>
<exec executable="${adb}" failonerror="true">
<arg line="${adb.device.arg}" />
<arg value="pull" />
<arg value="/sdcard/coverage.ec" />
<arg value="coverage.ec" />
</exec>
<echo>Extracting coverage report...</echo>
<emma>
<report sourcepath="${tested.project.absolute.dir}/${source.dir}"
verbosity="${verbosity}">
<!-- TODO: report.dir or something like should be introduced if necessary -->
<infileset dir=".">
<include name="coverage.ec" />
<include name="coverage.em" />
</infileset>
<!-- TODO: reports in other, indicated by user formats -->
<html outfile="coverage.html" />
</report>
</emma>
<echo>Cleaning up temporary files...</echo>
<delete file="coverage.ec" />
<delete file="coverage.em" />
<echo>Saving the report file in ${basedir}/coverage/coverage.html</echo>
</then>
<else>
<run-tests-helper />
</else>
</if>
Apparently EMMA isn't supported for the SDK Yet. It's only supported for Apps and Tests that are part of the source tree. Which seems weird because they don't mention this in the dev-guide where they tell you about the EMMA instrumentation options.
http://developer.android.com/guide/developing/testing/testing_otheride.html#AMOptionsSyntax
You can read the response from this guy (who appears to be on the dev team? I dunno)
http://groups.google.com/group/android-developers/msg/a542afd318832371
If you want to download and build the Android Source it sounds feasible to get EMMA working for your own App. However, it sounds kind of tricky and possibly a little unstable. See the full thread for more info:
http://groups.google.com/group/android-developers/browse_thread/thread/43cf8a8ca5662f85/9b68eec3e7b625f3?lnk=gst&q=Emma#
If you are using maven for your project is fairly simple with rooted devices
check this wiki :
http://code.google.com/p/maven-android-plugin/wiki/EmmaMaven
it was easy to implement the solution apart the report generation
but if you check the comments of the wiki you will find the solution
Related
I am trying to use custom build file in order to display the current git version in my Android application. I haven't used ant before and I have not really an idea how to use it. I read plenty of topics in SO and searched quite a lot in Google but I cannot figure it out. I don't really have the time to learn everything about ant but I need this thing running. At the bottom, you can find the code.
Current status
The file custom_rules.xml is imported in the build.xml created by Eclipse. The macrodef part is invoked but the targets not. I tried to change the External Tools Configurations, tab Targets but whenever I check a target (no matter in which ant file), I get a message:
Unknown argument: -pre-build
for example (when I put checkmark on -pre-build). I tried adding this line:
<import file="${sdk.dir}/tools/ant/build.xml" />
and defining sdk.dir but that doesn't change anything. What am I missing? As I said, I have no idea about ant and the only tutorial that helped me was this one.
Current code (custom_rules.xml)
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse.ant.import ?>
<project name="Custom build">
<macrodef name="git" taskname="#{taskname}">
<attribute name="command" />
<attribute name="dir" default="" />
<attribute name="property" default="" />
<attribute name="taskname" default="" />
<attribute name="failonerror" default="on" />
<element name="args" optional="true" />
<sequential>
<exec executable="git" dir="#{dir}" outputproperty="#{property}"
failifexecutionfails="#{failonerror}" failonerror="#{failonerror}">
<arg value="#{command}" />
<args/>
</exec>
</sequential>
</macrodef>
<target name="-pre-build">
<git command="rev-list" property="versioning.code" taskname="versioning">
<args>
<arg value="master" />
<arg value="--first-parent" />
<arg value="--count" />
</args>
</git>
<git command="describe" property="versioning.name" taskname="versioning">
<args>
<arg value="--always" />
</args>
</git>
<echo level="info" taskname="versioning">${versioning.code}, ${versioning.name}</echo>
<replaceregexp file="AndroidManifest.xml" match='android:versionCode=".*"' replace='android:versionCode="${versioning.code}"' />
<replaceregexp file="AndroidManifest.xml" match='android:versionName=".*"' replace='android:versionName="${versioning.name}"' />
</target>
<target name="-post-build" >
<replaceregexp file="AndroidManifest.xml" match='android:versionCode=".*"' replace='android:versionCode="0"' />
<replaceregexp file="AndroidManifest.xml" match='android:versionName=".*"' replace='android:versionName="0"' />
</target>
</project>
I just figured it out.
I renamed the targets to pre-build and post-build without -.
I went to External Tools Configurations and selected build.xml to the left. On the right, I went to the tab Targets, checked my two targets (in addition to the build target which already had a checkmark) and set the order to be pre-build, build, post-build.
I went to the project properties and I selected Builders on the left. I created a new builder using the build.xml file and having the three targets from the previous bullet point, in the same order. I placed this builder before the Java builder.
I removed the post-build target as it puts the version back to 0 and seems to do that earlier than I would like.
I am still not sure that this is the optimal solution. Also, the solution fails when there is no git versioning. I tried using this code for solving the issue but it didn't worked. Nevertheless, it is the best I could do and it helps me getting the info I need in the app.
Use Git executable with Argument --version and catch the output in a property for further usage, f.e. :
<project>
<exec executable="git" outputproperty="gitversion">
<arg value="--version"/>
</exec>
<echo>$${gitversion} => ${gitversion}</echo>
</project>
output :
[echo] ${gitversion} => git version 1.8.3.msysgit.0
I have a directory full of jars (felix bundles). I want to iterate through all of these jars and create dex'd versions. My intent is to deploy each of these dex'd jars as standalone apk's since they are bundles. Feel free to straighten me out if I am approaching this from the wrong direction.
This first part is just to try and create a corresponding .dex file for each jar. However when I run this I am getting a "no resources specified" error coming out of Ant.
Is this the right approach, or is there a simpler approach to just input a jar and output a dex'd version of that jar? The ${file} is valid as it is spitting out the name of the file in the echo command.
<target name="dexBundles" description="Run dex on all the bundles">
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="${basedir}/libs/ant-contrib.jar" />
<echo>Starting</echo>
<for param="file">
<path>
<fileset dir="${pre.dex.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<sequential>
<echo message="#{file}" />
<echo>Converting jar file #{file} into ${post.dex.dir}/#{file}.class...</echo>
<apply executable="${dx}" failonerror="true" parallel="true" verbose="true">
<arg value="--dex" />
<arg value="--output=${post.dex.dir}/${file}.dex" />
<arg path="#{file}" />
</apply>
</sequential>
</for>
<echo>Finished</echo>
</target>
Give this a go:
<target name="dexBundles" description="Run dex on all the bundles">
<apply executable="${exec.dx}" dest="${post.dex.dir}/" parallel="false">
<arg value="--dex"/>
<arg value="--output="/>
<targetfile/>
<srcfile/>
<fileset dir="${pre.dex.dir}" includes="**/*.jar"/>
<mapper type="glob" from="*.jar" to="*.dex"/>
</apply>
</target>
It looks like the ant apply task allows you to iterate over a file set without need for ant-contrib (specifically that page has an example that looks for *.c in a directory, compiles them, and renames them to *.o in a specified directory that should be directly applicable). Unfortunately, it looks like you'll lose the traceability provided by your echo messages.
For the record, I believe the error message is actually being generated by dx.bat not ant directly, but I am not certain, and I don't know why.
Hope that helps.
see the document of the "Ant apply task",it's said:“At least one fileset or filelist is required”
so,this line "no resources specified" is printed to notify u that to write some fileset or filelist.....
use "exec" instead.
I've searched google and SO for solutions, but could not find any. I'm developing a mobile AIR app for android, and i use Jenkins as a local CI system. My project compiles fine, however, during the ADT packaging something goes wrong. I've copied the ADT packaging targets from the following examples:
http://blog.terrenceryan.com/using-ant-to-package-the-same-air-app-to-multiple-devices/
and
https://gist.github.com/630170
However, i am getting this output in Jenkins: http://d.pr/i/y2gJ
This is the packaging part in my build.xml file (with important property names and values used):
...
...
<property name="APP_NAME" value="Hightide"/>
<property name="ANDROID_HOME" value="${user.home}/../../../Supermaggel/SDKS/android-sdk-macosx" />
<property name="APP_DESCRIPTOR" value="${SOURCE_DIR}/${APP_NAME}-app.xml" />
<property name="SWF_FILE" value="${APP_NAME}.swf" />
<property name="OUTPUT_LOCATION_ANDROID" location="${BUILD_DIR}/android" />
<property name="OUTPUT_SWF_ANDROID" location="${OUTPUT_LOCATION_ANDROID}/${SWF_FILE}" />
<property name="OUTPUT_APK_ANDROID" value="OUTPUT_LOCATION_ANDROID/${APP_NAME}.apk" />
...
...
<!-- PACKAGE ANDROID -->
<target name="package-android">
<echo message="Packaging for Android"/>
<exec executable="${ADT}" dir="${OUTPUT_LOCATION_ANDROID}">
<arg line="-package"/>
<arg line="-target apk"/>
<arg line="-storetype pkcs12"/>
<arg line="-keystore ${KEYSTORE_ANDROID}" />
<arg line="-storepass ${STOREPASS_ANDROID}" />
<arg line="${APP_NAME}"/> <!-- output .APK -->
<arg line="${APP_DESCRIPTOR}"/> <!-- app descriptor location -->
<arg line="${OUTPUT_SWF_ANDROID}"/> <!-- output -->
</exec>
</target>
I am using Jenkins ver. 1.486, Flash Builder 4.6, AIR 3.3.
Can anyone point out what is going wrong during the packaging? any arguments missing or interpreted wrong?
I found out what caused it. The ADT cli tool gives exit code 2, which means something is wrong with the parameters... i spent hours looking at it, trying to figure out what was wrong. for ADT you need to pass in relative paths, not 'absolute' ones (or, relative from the project root.) so instead of ${PROJECT_ROOT}/packagedir/${APPNAME}.apk just use packagedir/${APPNAME}.apk or something similar, for all paths.
How to convert class file to dex file in android?
Is there any way?
Invoke "dx" command with "--dex" option like the following.
dx --dex --output=<output-file> <input-file>
"dx" command is contained in Android SDK. The location of the command varies. Try to search:
<SDK-HOME>/platform-tools/
<SDK-HOME>/platforms/<platform>/tools/
"dx --dex" can accept some options. For example, "--no-strict" option will skip checking whether path names of input class files match the declared package/class names, so you'll get a different result for WebService.class if you invoke dx with this option. Probably, "--keep-classes" option is useful if you want to use jar files on both Android and Java SE. Without "--keep-classes" option, dx command replaces all .class files in an input jar file with one file named "classes.dex", but with "--keep-classes" option, input .class files also go into the ouput file as well as classes.dex.
ex:
dx --dex --keep-classes --output=output.jar input.jar
I read the source code of dx and listed up the command line options with some explanation at the following page.
Usage of dx --dex:
http://darutk-oboegaki.blogspot.com/2011/03/usage-of-dx-dex-dx-dex.html
Use dx.
The dalvik docs area includes a trivial example.
Edit: Link is dead. You can find the source tree copy here, and a formatted version in a mirror here.
For ANT automatic building:
<property name="dx" value="${android_sdk}/build_tools/18.1.0/dx" />
<target name="convert_jar_to_dex">
<apply executable="${dx}" failonerror="true" parallel="true">
<arg value="-JXms1024m" />
<arg value="-JXss1024k" />
<arg value="--dex" />
<arg value="--verbose" />
<arg value="--no-strict" />
<arg value="--num-threads=4" />
<arg value="--output=${basedir}/bin/output.dex" />
<fileset file="${basedir}/input.jar"/>
</apply>
</target>
This is my experience. May it be helpful.
adding more info for darutk answer:
dx command is contained in Android SDK
example: D:\Android\AndroidSDK\build-tools\23.0.1\
with 23.0.1 is android version
Failing JUnit tests, not breaking my Ant script like I expect?
My continuous integration server runs an Ant script, which calls something like:
/tests/ant run-tests
My JUnit tests run, but with errors:
run-tests:
[echo] run-tests-helper.
[echo] Running tests ...
[exec]
[exec] com.zedray.stuff.FooBarTest:....
[exec] com.zedray.stuff.FooBarTest:.....INSTRUMENTATION_RESULT: shortMsg=Some error in your code.
[exec] INSTRUMENTATION_RESULT: longMsg=java.security.InvalidParameterException: Some error in your code
[exec] INSTRUMENTATION_CODE: 0
The errors are OK, but my build script keeps going (eventually publishing my broken app to my testers - bad!). What I would expect is for the instrimentaiton to throw a build error, so my continuous integration server (TeamCity in this case) realises that something has gone wrong and reports a broken build. The "failonerror" is already set in the relevant macrodef, so I'm not sure what else I can do?
/tests/build.xml
Running tests ...
Any ideas/suggestions on how to fix this?
Regards
Mark
I did it another way, because I am using the ant test target that is in the Android build.xml file. This target prints to the standard out, so I captured stndout into a file then queried the file, using this result to fail my task.
<target name="run-acceptance-tests" depends="clean, debug, install" >
<property name="log.file" value="acceptance_tests_standard_out.txt" />
<!-- because we don't have control over the 'test' target (to check for passes an fails) this prints to standard out
we capture standard out into a file and query this to see if we have any test failures, using this to pass/fail our task -->
<record name="${log.file}" action="start" />
<antcall target="test" />
<record name="${log.file}" action="stop" />
<!-- do other stuff -->
<loadfile property="tests.output" srcFile="${log.file}" />
<echo>Checking for failures</echo>
<fail message="acceptance tests failed!" >
<condition>
<contains string="${tests.output}" substring="FAILURES" />
</condition>
</fail>
<echo>acceptance tests passed!</echo>
</target>
I had the same problem, and I ened up customize the "run-tests" target in my own build.xml like this, and there is no need to change the original android sdk test_rules.xml
<target name="run-tests" depends="-install-tested-project, install"
description="Runs tests from the package defined in test.package property">
<echo>Running tests ...</echo>
<exec executable="${adb}" failonerror="true" outputproperty="tests.output">
<arg value="shell" />
<arg value="am" />
<arg value="instrument" />
<arg value="-w" />
<arg value="-e" />
<arg value="coverage" />
<arg value="#{emma.enabled}" />
<arg value="${manifest.package}/${test.runner}" />
</exec>
<echo message="${tests.output}"/>
<fail message="Tests failed!!!">
<condition>
<contains string="${tests.output}" substring="FAILURES" />
</condition>
</fail>
</target>
Also was looking for some kind of standard solution for this. I wonder how do android guys develop, or they dont use teamcity and continuous integration? heard hudson has some plugin for android but I dont like hudson. anyway here is quick and dirty solution
replace contents in android-sdk-windows\tools\ant\test_rules.xml with:
<attribute name="emma.enabled" default="false" />
<element name="extra-instrument-args" optional="yes" />
<sequential>
<echo>Running tests ...</echo>
<exec executable="${adb}" failonerror="true" outputproperty="tests.output">
<arg line="${adb.device.arg}" />
<arg value="shell" />
<arg value="am" />
<arg value="instrument" />
<arg value="-w" />
<arg value="-e" />
<arg value="coverage" />
<arg value="#{emma.enabled}" />
<extra-instrument-args />
<arg value="${manifest.package}/${test.runner}" />
</exec>
<echo message="${tests.output}"/>
<fail message="Tests failed!!!">
<condition>
<contains string="${tests.output}" substring="FAILURES" />
</condition>
</fail>
</sequential>
there are two drawbacks
1) you dont see test output while tests are running until they failed (and the output is crippled somewhow)
2) its better to override this macro in your project
another option would of course be to ditch Ant in favor of Maven or Gradle. Both have Android plug-ins that properly fail the build when there are test failures.
Maven:
http://code.google.com/p/maven-android-plugin/
Gradle:
http://code.google.com/p/gradle-android-plugin/
running instrumentation tests has just been added to the Gradle Android plug-in, and is waiting to be merged back into the master repository, so there should be another release soon.
The ant JUnit task defaults to running all the tests. There are two solutions to this.
Easiest solution is to set the haltonerror property to true and the build will fail at the first test failure.
Slightly more involved (and my preference) is to set the failureProperty so that all the tests run. This lets you know how many tests fail instead of only the first test that fails. This requires more ant work because you need to add a line after your junit tests like this:
<fail message="tests failed" if="failureProperty"/>