简体   繁体   English

Android Gradle - SonarCloud 与 Jacoco 插件集成,覆盖率始终为 0.0%

[英]Android Gradle - SonarCloud Integration with Jacoco Plugin, coverage is always 0.0%

I am trying to sync my locally generated Coverage Report from Jacoco to SonarCloud, even though there are no errors, the Coverage is always 0.0%.我正在尝试将本地生成的覆盖率报告从 Jacoco 同步到 SonarCloud,即使没有错误,覆盖率始终为 0.0%。 I am able to generate a JacocTestReport.xml with an overall coverage of 86%.我能够生成一个总体覆盖率为 86% 的 JacocTestReport.xml。

Below are the code snippets for build.gradle file.下面是 build.gradle 文件的代码片段。 Please guide me how to sync the coverage report with SonarCloud.请指导我如何将覆盖率报告与 SonarCloud 同步。

plugins {
    id "jacoco"
    id "org.sonarqube"
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.google.firebase.firebase-perf'

jacoco {
    toolVersion = "0.8.8"
    reportsDirectory.set(layout.projectDirectory.dir("src/jacoco"))
}

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']
}

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebugUnitTest") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports"

    reports {
        xml.required = true
        html.required = true
    }

    def fileFilter = ['**/AndroidManifest.xml', '**/strings.xml', '**/*.xml', '**/*.json']
    def mainSrc = "${project.projectDir}/src/main/java"
    def kotlinTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/debugUnitTest", excludes: fileFilter)
    def kotlinTreeModels = fileTree(dir: "${project.projectDir}/src/main/java/com/<package_dir>/model", excludes: [])

    sourceDirectories.setFrom(files([mainSrc]))
    classDirectories.setFrom(files([kotlinTree, kotlinTreeModels]))
    executionData.setFrom(fileTree(dir: "$project.projectDir",
            includes: ['**/*.exec', '**/*.ec']
    ))
}

sonarqube {
    properties {
        property "sonar.organization", "<org_key>"
        property "sonar.projectKey", "<project_key>"
        property "sonar.projectName", "<project_name>"
        property "sonar.projectVersion", "1.0"
        property "sonar.exclusions", ['**/com/skydoves/powerspinner/*', 'robospice/**/*']
        property "sonar.host.url", "https://sonarcloud.io"

        property "sonar.sources", "src/main/java"
        property "sonar.tests", "src/test/java"
        property "sonar.sourceEncoding", "UTF-8"

        property "sonar.test.inclusions", ["**/*Test*/**"]
        property "sonar.login", "<SONAR_TOKEN>"
        property "sonar.branch.name", "release-develop"
        property "sonar.branch.target", "debug"

        def unit = fileTree(dir: project.projectDir, includes: ['**/*.exec']).files;
        def ui = fileTree(dir: project.projectDir, includes: ['**/*.ec']).files;
        unit.addAll(ui);
        def files = unit.join(", ");
        property "sonar.jacoco.reportPaths", files

        def reportXML = "${project.rootDir}/app/src/jacoco/jacocoTestReport/jacocoTestReport.xml"
        property "sonar.coverage.jacoco.xmlReportPaths", ["src/jacoco/jacocoTestReport/jacocoTestReport.xml", "**/jacocoTestReport/jacocoTestReport.xml", reportXML]
        
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.android.lint.report", "build/reports/lint-results.xml"
        property "sonar.junit.reportsPath", "build/test-results/testDebugUnitTest"
    }
}

android {
    compileSdkVersion 32
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "<package_name>"
        minSdkVersion 23
        targetSdkVersion 32
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
        resConfigs 'en', 'fr-rCA'
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    
    buildTypes {
        debug {
            testCoverageEnabled true
        }
    }
   

    testOptions {
        unitTests.includeAndroidResources = true
        unitTests.returnDefaultValues = true
        unitTests.all {
            jacoco {
                includeNoLocationClasses = true
            }
        }
    }
    lint {
        abortOnError false
        checkReleaseBuilds false
    }
}


configurations.all {
    resolutionStrategy {
        eachDependency { details ->
            if ('org.jacoco' == details.requested.group) {
                details.useVersion "0.8.8"
            }
        }
    }
}

Below are the dependencies for project build.gradle :以下是项目 build.gradle 的依赖项:

dependencies {
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.4.0.2513"
        classpath "org.jacoco:org.jacoco.core:0.8.8"
}

Below is the gradle command, which I executed in Terminal :下面是我在终端中执行的 gradle 命令:

.\gradlew clean connectedAndroidTest test createDebugCoverageReport jacocoTestReport sonarqube

Thank you.谢谢你。

I found the solution.我找到了解决方案。 Had to change paths for 'sonar.sources' and 'sonar.tests' properties.必须更改“sonar.sources”和“sonar.tests”属性的路径。

        property "sonar.sources", ["src/test", "src/main/java"]
        property "sonar.tests", "src/test"
        property "sonar.sourceEncoding", "UTF-8"
        property "sonar.test.inclusions", ["src/test/java"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM