Emma does not generate coverage.ec

2020-07-20 03:54发布

问题:

I setup Emma and it used to work for me. Then we had source code changes and now it doesn't generate coverage.ec at all. It does generate coverage.em. Near the end of testing, it has error messages:

[exec] INSTRUMENTATION_CODE: 0
[echo] Downloading coverage file into project directory...
[exec] remote object '/sdcard/coverage.ec' does not exist

BUILD FAILED
/var/lib/jenkins/android-sdk-linux_x86/tools/ant/build.xml:1056: exec returned: 1.

Line 1056 of build.xml is

"{adb}" failonerror="true".

I see that I do have coverage.em on the desktop, which means my code are instrumented.

the command I use under the \test is

ant emma debug install test

This worked for me before. Running code coverage always crashes for me, usually near the end of the unit test, but it'd always get me some coverage. Now it crashes out and doesn't produce coverage.em.

I also tried to access /sdcard/ and it's perfectly accessible and writable.

This has blocked me for days, any input would be much appreciated. I am also new to all this Android, Ant and Emma, so thanks!!

Update: I just cleaned up the environment and ran the command again.Now coverage.em is no longer generated either. Which tells me the source code are not instrumented. But the command I used above should instrument project, its test project, install and start test. I didn't change emma def in build.xml except to change the coverage.ec location to /sdcard/coverage.ec. This is because by default it goes to /data/data, and I don't have permission to access data/data on this phone

I am using r15 of Android SDK, and the default build.xml. I only changed the path to coverage.ec to /sdcard/coverage.ec. To run instrumentation

 Go to main_project
 $andriod update project -p .
 Go to main_prject\test
 $android update project -m ../ -p .
 To start code code
 $ant emma debug install test

It generated main_project-instrumented.apk and test_project-debug.apk. Both are installed and I can see it executes testing.

回答1:

You have to make a test project with the android command line tools first.

Create Project & Test Project

Assuming your project is stored in D:\AndroidProject and your programming against android API level 8. First you use this command to create the project:

android update project --path ./ --name blabla~ --target android-8 --subprojects 

Then create an folder for the test project and navigate into that folder:

mkdir Android_test
cd Android_test

Then create the android test project with the below command

 android create test-project --main ../AndroidProject --path ./

Ant building with emma coverage report (with root)

Execute this command (from jenkins select and build step) to get a build done with emma reporting:

ant emma debug install test

Caution : For this to work you have to connect rooted device or emulator, then execute ant command!

Change Build xml file (so no root is required)

If you don't want to root your device an alternative solution is to alter the location of these coverage reports. For this you should modify the build.xml file.

(you should googling about that for more information, briefly explained here)

Open the build.xml -> find the location where the coverage.ec file is stored. In most cases this will be stored in /data/data/com.example.Android/coverage.ec

The problem here is that the /data/data/~~~ path is protected (hence the required root).

anyway~ you can get a coverage.html file in your test project folder/bin. The next steps explain how to change this to save this file on the /sdcardinstead!

You can open your build.xml file and at the last line ~ you can find the command import ~~~ build.xml which means that your build.xml file will import anoother build.xml file.

The other build.xml file is part of the android SDK and is located at ${Android-sdk}/tools/ant/build.xml.

Required changes for build.xml file

We can't change this file (without getting into trouble) so instead copy the complete file to an alternative location or directly into your projects build.xml file.

Don't forget to adapt or change the import statement in your build.xml file whatever you choose to do.

This is what you need to change in that new build.xml file:

  • Erase the import= ~~build.xml
  • Erase the first line which is xml=ejkwjkw?e jw ""project = "android_rule" ~~ ~blabla)
  • and last line /project
  • update address to /sdcard/coverage.ec

Then, you can get coverage.ec file~



回答2:

I ran into this issue after updating my SDK to r16: Emma code coverage not working in r15 of tools

and this fixed it for me:

ant all clean emma debug install test

But I am unsure if you have the same problem.

I don't have permission to access data/data on this phone

Is it a non-rooted device? Note that the build says:

WARNING: Code Coverage is currently only supported on the emulator and rooted devices.

Maybe you can't work around this limitation by just changing the location of the coverage file. Does it work on a virtual device?