apply plugin: 'jacoco'
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}
configurations {
jacocoAnt
jacocoRuntime
}
jacoco {
toolVersion = "0.8.7"
// toolVersion = "0.8.5"
}
def offline_instrumented_outputDir = "$buildDir.path/intermediates/classes-instrumented/debugCoverage"
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}
def coverageSourceDirs = [
'src/main/java'
]
task jacocoTestReport(type: JacocoReport, dependsOn: "test") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
classDirectories.from = fileTree(
dir: './build/intermediates/javac/debugCoverage/classes/',
excludes: [
'**/R.class',
'**/R$*.class',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
'**/BuildConfig.*',
'**/**Bean.class',
'**/inputmethod/*',
'**/config/*.*',
'**/flex/*.*',
'**/AssetsUtils.class',
'**/databinding/**',
'**/DataBind**',
'**/BtUtil.*',
'**/SystemProperties.*',
'**/SystemPropertyUtil.*',
]
)
getSourceDirectories().setFrom(files(coverageSourceDirs))
getExecutionData().setFrom(files('./build/jacoco/testDebugUnitTest.exec'))
}
jacocoTestReport {
reports {
xml.enabled true
html.enabled true
html.destination file("build/test-results/jacocoHtml")
}
}
task createOfflineTestCoverageReport(dependsOn: ['instrument', 'testDebugCoverageUnitTest']) {
group = "reporting"
doLast {
ant.taskdef(name: 'report',
classname: 'org.jacoco.ant.ReportTask',
classpath: configur3+ations.jacocoAnt.asPath)
ant.report() {
executiondata {
ant.file(file: "$buildDir.path/jacoco/testDebugCoverageUnitTest.exec")
}
structure(name: 'Example') {
classfiles {
fileset(dir: "$project.buildDir/intermediates/javac/debugCoverage")
}
sourcefiles {
fileset(dir: 'src/main/java')
}
}
}
}
}
createOfflineTestCoverageReport.dependsOn(clean)
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(instrument)) {
tasks.withType(Test) {
doFirst {
systemProperty 'jacoco-agent.destfile', buildDir.path + '/jacoco/testDebugCoverageUnitTest.exec'
classpath = files(offline_instrumented_outputDir) + classpath + configurations.jacocoRuntime
}
}
}
}
task instrument(dependsOn:'compileDebugUnitTestSources') {
doLast {
println 'Instrumenting classes'
ant.taskdef(name: 'instrument',
classname: 'org.jacoco.ant.InstrumentTask',
classpath: configurations.jacocoAnt.asPath)
ant.instrument(destdir: offline_instrumented_outputDir) {
fileset(dir: "$buildDir.path/intermediates/javac/debugCoverage")
}
}
}
已经将版本修改到了0.8.7 并使用的是java11
Microsoft Windows [版本 10.0.19045.6093]
(c) Microsoft Corporation。保留所有权利。
E:\work_project\20250714_shentong\Launcher>gradlew testDebugUnitTest --tests "com.st.base.ui.ViewModelScopeTest2"
> Task :base:compileDebugJavaWithJavac
注: 某些输入文件使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
注: 某些输入文件使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
> Task :app:compileDebugJavaWithJavac
注: E:\work_project\20250714_shentong\Launcher\app\src\main\java\com\st\launcher\AppListActivity.java使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
> Task :base:compileDebugUnitTestJavaWithJavac
注: 某些输入文件使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
注: 某些输入文件使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
> Task :app:testDebugUnitTest FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:testDebugUnitTest'.
> No tests found for given includes: [com.st.base.ui.ViewModelScopeTest2](--tests filter)
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
See https://docs.gradle.org/7.3.3/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 12s
63 actionable tasks: 11 executed, 52 up-to-date
如何处理