我最近更新了我的 Android 项目,使用 Java 17 而不是 Java 8。在运行 jacocoTestReport Gradle 命令时,我获得了库模块的代码覆盖率,但没有应用程序模块 (app)。
如果我将应用程序模块从应用程序更改为库,我可以在 Jacoco 报告中看到代码覆盖率。我不确定我是否缺少某些配置或者这是 Jacoco 错误。
这是我的示例应用程序 Gradle 设置:
build.gradle(应用程序):
plugins {
id 'com.android.application' // If I change this to 'com.android.library', I get Jacoco coverage.
id 'org.jetbrains.kotlin.android'
id 'jacoco'
}
android {
namespace 'com.example.codecoverageissue'
compileSdk 33
defaultConfig {
applicationId "com.example.codecoverageissue"
minSdk 26
targetSdk 33
versionCode 1
versionName "1.0"
}
buildTypes {
debug {
debuggable true
testCoverageEnabled true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
getIsDefault().set(true)
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses true
jacoco.excludes = ['jdk.internal.*']
}
testOptions {
unitTests.returnDefaultValues = true
unitTests.includeAndroidResources = true
}
testCoverage.jacocoVersion = "$jacocoVer"
}
dependencies {
implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
task jacocoTestReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
def androidExclusion = [
'**/BR.*',
'**/R.*',
'**/R$*.*',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Adapter*.*',
'**/*Fragment*.*',
'**/*ViewHolder*.*',
'**/*ViewModelFactory*.*',
'**/*Layout*.*'
]
reports {
xml.getRequired().set(true)
html.getRequired().set(true)
}
classDirectories.from = files([
fileTree(
// Java generated classes (debug build)
dir: 'build/intermediates/javac/debug/classes',
excludes: androidExclusion
),
fileTree(
// Kotlin generated classes (debug build)
dir: 'build/tmp/kotlin-classes/debug',
excludes: androidExclusion
)
])
sourceDirectories.from = files('src/main/java/')
executionData.from = files('build/outputs/unit_test_code_coverage/debugUnitTest/testDebugUnitTest.exec')
}
build.gradle(根级别):
plugins {
id 'com.android.application' version '8.0.2' apply false
id 'com.android.library' version '8.0.2' apply false
id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
}
ext {
jacocoVer = "0.8.10"
}
我已经检查过此 Gradle 问题论坛 上提供的解决方案,但它没有为我解决。
如有任何帮助,我们将不胜感激。
给你解决方案,我面临着同样的问题。