6

I've been unable to get code coverage to work on any of my android projects.

To simplify things I created a new project (selected empty activity). Added a new utility class to the the project in src/main/java.

I then created a unit test for it in src/androidTest/java.

Updated the gradle file to enable coverage.

apply plugin: 'com.android.application' android { compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig { applicationId "com.example.michaelkrussel.coverageapp" minSdkVersion 15 targetSdkVersion 21 versionCode 1 versionName "1.0" } jacoco { version "0.7.1.201405082137" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug { testCoverageEnabled true } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.3' } 

I ran the tests then using ./gradlew createDebugCoverageReport. The unit test shows that the test I created passed, but the coverage report reports zero coverage.

I assume I'm either missing something in the gradle file or not running the correct gradle task, but I cannot figure out what.

2
  • try add In defaultConfig { testApplicationId "your.package.test"} Commented Mar 13, 2015 at 18:13
  • 1
    @anderson_acs Tried to add it, and I still have an empty coverage.ec file and the report says 0 coverage. Commented Mar 13, 2015 at 19:59

1 Answer 1

1
+100

I'm using this configuration in my project and work fine!

apply plugin: 'com.android.application' android { compileSdkVersion 21 buildToolsVersion "21.1.1" defaultConfig { applicationId "br.com.acs.app" minSdkVersion 18 targetSdkVersion 21 versionCode 1 versionName "1.0" testApplicationId "br.com.acs.app.test" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug { testCoverageEnabled true } } packagingOptions { exclude 'LICENSE.txt' } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.3' androidTestCompile 'junit:junit:4.11' androidTestCompile('com.android.support.test:testing-support-lib:0.1') { exclude group: 'junit' } } 
Sign up to request clarification or add additional context in comments.

4 Comments

I see that you're include junit 4. Are you running standard Java unit tests or are you running android instrumented tests?
yeah! I'm running instrumented tests.
Well coping our gradle file still didn't fix my problem, but did tell me that it wasn't a gradle configuration problem. I decided to try running the tests on a different device and everything started working. The device that didn't work was a Nexus S4 mini running Android 4.4.2. I also tried an emulator using Android 4.4.2, and it worked. So the problems seems to be my specific device (not sure what). Thanks for showing me a working example. I also learned about the testing-support-lib which looks cool.
Yes Michael Krussel is right, I had the same problem I did extensive experiment with Samsung Galaxy S4 (4.4.2), It was giving me an empty coverage.ec but when I tried same config with LG-Nexus 5 (5.1.1) it worked fine. Also where are you trying to find *.exec or *.ec files ? look at your project-directory/build/outputs/code-coverage/connected/coverage.ec

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.