I'm facing an issue where the Jacoco code coverage results aren't showing up for unit tests that use Robolectric. The tests execute and pass successfully, but the Jacoco coverage seems to ignore files where Robolectric is utilized.
Environment:
- Android Studio: Giraffe | 2022.3.1 Patch 1
- Android Gradle Plugin (AGP): 7.2.0
- Gradle Version: 7.4.2
- Robolectric: 4.10.3
Project-Level build.gradle:
buildscript {
val agp_version by extra("7.2.0")
}
plugins {
id("com.android.application") version "7.2.0" apply false
id("org.jetbrains.kotlin.android") version "1.9.0" apply false}
Module-Level build.gradle:
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
android {
namespace = "com.example.jacocorobolelectric"
compileSdk = 33
defaultConfig {
applicationId = "com.example.jacocorobolelectric"
minSdk = 23
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.9.0")
testImplementation("junit:junit:4.13.2")
testImplementation ("org.robolectric:robolectric:4.10.3")
}
How can I address this issue?
The problem you're facing is a common one among Android developers who use Robolectric and Jacoco together. Jacoco is not always able to capture coverage for Robolectric tests out of the box due to the way Robolectric manipulates bytecode.
Here are steps to address this issue:
Once these configurations are set, re-run your tests, and then generate the Jacoco coverage report. Hopefully, you'll see coverage data for your Robolectric tests.
If you're still facing issues after making these changes, consider checking if there are any specific compatibility issues between the Jacoco version and Robolectric or Android Gradle Plugin versions you're using.