简体   繁体   English

android jacoco覆盖空与gradle

[英]android jacoco coverage empty with gradle

I'm trying to make jacoco create a code coverage report for my android test project. 我正在尝试让jacoco为我的android测试项目创建一个代码覆盖率报告。 I have the following in build.gradle: 我在build.gradle中有以下内容:

apply plugin: 'com.android.application'
apply plugin: 'jacoco'
...
jacoco {
    toolVersion = "0.7.1.201405082137"
}
...
android {
    buildTypes {
        release {
        }
        debug {
            testCoverageEnabled true
        }
    }
}

when I run gradlew -i createDebugCoverageReport, I get a coverage report, but it's empty. 当我运行gradlew -i createDebugCoverageReport时,我得到一个覆盖率报告,但它是空的。 the end of the gradle execution is as follows: gradle执行结束如下:

:androidTest:connectedAndroidTest (Thread[main,5,main]) completed. Took 2 mins 36.951 secs.
:androidTest:createDebugCoverageReport (Thread[main,5,main]) started.
:androidTest:createDebugCoverageReport
Executing task ':androidTest:createDebugCoverageReport' (up-to-date check took 0.006 secs) due to:
  Output file /home/akos/src/androidTest/build/outputs/reports/coverage/debug has changed.
  Output file /home/akos/src/androidTest/build/outputs/reports/coverage/debug/index.html has been removed.
  Output file /home/akos/src/androidTest/build/outputs/reports/coverage/debug/.resources/package.gif has been removed.
[ant:reportWithJacoco] Loading execution data file /home/akos/src/androidTest/build/outputs/code-coverage/connected/coverage.ec
[ant:reportWithJacoco] Writing bundle 'debug' with 3 classes
:androidTest:createDebugCoverageReport (Thread[main,5,main]) completed. Took 0.215 secs.

BUILD SUCCESSFUL

Total time: 4 mins 53.467 secs

and indeed, the coverage.ec file referenced above is empty (of 0 length) 实际上,上面引用的coverage.ec文件为空(长度为0)

in the directory build/intermediates/coverage-instrumented-classes/ , I seem to have the instrumented class files 在目录build / intermediates / coverage-instrumented-classes /中,我似乎有了已检测的类文件

this is with gradle 2.1 这是gradle 2.1

what am I doing wrong? 我究竟做错了什么?

Try to generate the debug coverage report using Nexus device (5 or 5x), it will works perfectly. 尝试使用Nexus device (5或5x)生成调试覆盖率报告,它将完美运行。 I was facing same issue with Samsung devices (coverage.ec is empty) but after that I run it with Nexus 5x and everything was working fine. 我遇到了与Samsung devices相同的问题(coverage.ec是空的),但之后我用Nexus 5x运行它,一切正常。

Jacoco should be inside of the android closure and the jacoco plugin declaration is not necessary: Jacoco应该在android闭包内,并且jacoco插件声明不是必需的:

apply plugin: 'com.android.application'
...
android {
    buildTypes {
        release {
        }
        debug {
            testCoverageEnabled true
        }
    }
    jacoco {
        toolVersion = "0.7.1.201405082137"
    }
}

I use same gradle.build and just enter the command below to terminal. 我使用相同的gradle.build,只需在下面输入命令到终端。 It gave me coverage report and test result as a html page. 它给了我一个html页面的报道和测试结果。

$./gradlew createDebugCoverageReport $。/ gradlew createDebugCoverageReport

Another suggestion is ; 另一个建议是; enter terminal 进入终端

$./gradlew --gui $。/ gradlew --gui

it opens a gui window and you can find the correct command. 它打开一个gui窗口,你可以找到正确的命令。

There's a problem in the Android Gradle Plugin 0.14.0 that generates an empty coverage.ec file. Android Gradle Plugin 0.14.0中存在一个问题,即生成一个空的coverage.ec文件。 It's known and was fixed in a branch but doesn't seem to have made it out to the public yet: https://code.google.com/p/android/issues/detail?id=78556 它已知并已在一个分支机构中修复但似乎尚未向公众发布: https//code.google.com/p/android/issues/detail?id = 78556

On the other hand, a lovely workaround template project for Android Studio can be found here using Jacoco and RoboElectric: https://github.com/nenick/android-gradle-template 另一方面,可以使用Jacoco和RoboElectric找到适用于Android Studio的可爱解决方案模板项目: https//github.com/nenick/android-gradle-template

So you can wait for Google to fix it or use someone else's build library repository, but Android Studio 1.0.0's gradle plugin is useless. 因此,您可以等待谷歌修复它或使用其他人的构建库存储库,但Android Studio 1.0.0的gradle插件是无用的。

I found a solution to same problem from Jacoco code coverage in Android Studio with flavors . 我从Android Studio中的Jacoco代码覆盖中找到了解决同样问题的解决方案。 It has example what to do in case coverage.ec is generated instead of testDebug.exec . 它有一个例子,如果生成coverage.ec而不是testDebug.exec ,该怎么做。

  1. I created the jacocoTestReport task. 我创建了jacocoTestReport任务。

  2. I executed $./gradlew createDebugCoverageReport which generates the coverage.ec file 我执行了$./gradlew createDebugCoverageReport ,它生成了coverage.ec文件

  3. After that I executed $./gradlew jacocoTestReport and that creates the HTML. 之后我执行$./gradlew jacocoTestReport并创建HTML。

Sometimes due to some security restrictions on the underlying hardware you are unable to get coverage reports. 有时由于底层硬件的某些安全限制,您无法获得覆盖率报告。 In my case I was using android:sharedUserId="android.uid.system" and due to this it was not picking up any coverage and was giving me an empty file. 在我的情况下,我使用android:sharedUserId =“android.uid.system”,由于这个原因,它没有拿起任何报道,并给了我一个空文件。

So I agree naran that you should try running it on different devices and make sure your app is not using android:sharedUserId="android.uid.system". 所以我同意naran你应该尝试在不同的设备上运行它并确保你的应用程序不使用android:sharedUserId =“android.uid.system”。

I made an interesting observation. 我做了一个有趣的观察。 I had the same problem, with the coverage.ec File having a size of 0 bytes. 我有同样的问题, coverage.ec文件的大小为0字节。 I tried all the answers here as well as these ones: 我在这里尝试了所有答案以及这些答案:

https://code.google.com/p/android/issues/detail?id=170607 https://code.google.com/p/android/issues/detail?id=170607

It didn't change anything. 它没有改变任何东西。 My build.gradle file has only these settings to let jacoco work: 我的build.gradle文件只有这些设置让jacoco工作:

apply plugin: 'com.android.application'
apply plugin: 'jacoco'

android {
    ...
buildTypes {
   ...
    debug {
        testCoverageEnabled true
    }
}

...
}

My Android Studio Version is 1.2.2. 我的Android Studio版本是1.2.2。 I have the following gradle settings in "File -> Project Structure": 我在“文件 - >项目结构”中有以下gradle设置:

  • Gradle version: 2.2.1 Gradle版本:2.2.1
  • Android Plugin Version: 1.2.3 Android插件版本:1.2.3

I tried to run the instrumentation tests with gradlew cC on an Motorola Moto G 4G LTE - Android 4.4.4 with correctly ran the instrumentation tests but with a coverage.ec file having 0 bytes. 我尝试在Motorola Moto G 4G LTE - Android 4.4.4上使用gradlew cC运行仪器测试,正确运行了仪器测试,但是带有0字节的coverage.ec文件。 Today I tried another device: LG Spirit 4G LTE - Android 5.0 and now code coverage works!!! 今天我尝试了另一种设备: LG Spirit 4G LTE - Android 5.0 ,现在代码覆盖正常! The coverage.ec file has 322 bytes and I see correct html coverage reports. coverage.ec文件有322个字节,我看到正确的html覆盖率报告。

What I think is that sometimes running gradle with gradlew cC --debug shows that coverage.ec cannot be found on the device and sometimes it shows the file being pulled. 我认为有时候使用gradlew cC --debug运行gradle会显示在设备上找不到coverage.ec ,有时它会显示文件被拉出。 But maybe in both situations it cannot be found on the device. 但也许在这两种情况下都无法在设备上找到它。 So choosing another device can fix the problem. 因此,选择其他设备可以解决问题。 Good luck! 祝好运!

Now (Oct 2015) you can use it as android team has fixed the bug . 现在(2015年10月)你可以使用它,因为android团队修复了这个bug

android {
    ...
    buildTypes {
        debug {
            testCoverageEnabled true
        }
    }
    ...
    dependencies{
        androidTestCompile 'com.android.support.test:runner:0.4.1'
        // Set this dependency to use JUnit 4 rules
        androidTestCompile 'com.android.support.test:rules:0.4.1'
        // Set this dependency to build and run Espresso tests
        androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
        // Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource
        androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
    }
}

Then just use ./gradlew createDebugCoverageReport . 然后使用./gradlew createDebugCoverageReport

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM