如何验证并使用Gradle中JacocoTask生成Android单元测试覆盖率报告?
Jacoco Coverage Report Setup for Android Unit Tests
Great question! Let's break down your current configuration and get it fully working to generate accurate coverage reports.
Is your existing config on the right track?
The snippet you shared has solid foundations but is incomplete and needs a couple tweaks for modern Android Gradle Plugin (AGP) versions:
- The
dependsOn: "testDebugUnitTest"is spot-on—it makes sure your debug unit tests run before the report is generated. - Enabling both XML and HTML reports is smart (XML plays nice with CI tools like SonarQube, while HTML gives you a readable, browser-friendly breakdown).
- Your exclusion list for
classDirectoriesmakes total sense—we don't want to waste time measuring coverage on auto-generated classes like R, BuildConfig, or Manifest files. - That said, the
additionalSourceDirsline is cut off, and the class directory path might be outdated if you're using AGP 7.0 or newer.
Complete, Updated Jacoco Task Configuration
Here's the full, polished version that works with recent AGP releases:
// First, make sure you apply the Jacoco plugin at the top of your app-level build.gradle (or .kts) plugins { id 'com.android.application' id 'jacoco' } // Now define the coverage report task task jacocoTestReport(type: JacocoReport, dependsOn: "testDebugUnitTest") { group = "Verification" description = "Generate Jacoco coverage reports after running tests." reports { xml.enabled = true // Essential if you're using CI tools or code quality platforms html.enabled = true // The human-readable report you'll want to browse csv.enabled = false // Disable this unless you specifically need CSV output } // For AGP 7.0+, class files live in javac/debug/classes instead of the old intermediates/classes path classDirectories = fileTree( dir: "${project.buildDir}/intermediates/javac/debug/classes", excludes: [ '**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', // Don't count coverage for your test classes themselves 'android/**/*.*' ] ) // Point Jacoco to your main app source code directories additionalSourceDirs = files(android.sourceSets.main.java.srcDirs) sourceDirectories = files(android.sourceSets.main.java.srcDirs) // Path to the execution data file generated when you run unit tests executionData = files("${project.buildDir}/jacoco/testDebugUnitTest.exec") }
How to Generate the Coverage Report
Once the task is set up, you can run it in two easy ways:
1. Command Line
Open your terminal, go to your project's root folder, and run:
./gradlew jacocoTestReport
This will automatically run all your debug unit tests first, then generate the coverage report right after.
2. Android Studio GUI
- Open the Gradle panel on the right side of the IDE (if you don't see it, go to View → Tool Windows → Gradle).
- Navigate to your app module → Verification → double-click the
jacocoTestReporttask.
Where to Find Your Report
After the task finishes successfully:
- The HTML report (the one you'll want to look at) is at
app/build/reports/jacoco/jacocoTestReport/html/index.html—just open this file in any browser to see line-by-line coverage stats. - The XML report (for CI or code quality tools) lives at
app/build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml.
内容的提问来源于stack exchange,提问作者Hey You
相关产品推荐
相关产品推荐

