build.gradle(Project)文件中的dependencies区域添加以下代码:
classpath "org.jacoco:org.jacoco.core:0.8.7"
build.gradle(Module)文件添加以下配置:
buildTypes {
        debug {
            testCoverageEnabled = true
        }
    }
dependencies {
    implementation "androidx.test.ext:junit:1.1.2"
    androidTestImplementation "androidx.test.ext:junit:1.1.2"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0"
    debugImplementation 'androidx.fragment:fragment-testing:1.3.4'
    // Required as the Jacoco plugin is being applied to the debug build type only.
    debugImplementation "org.jacoco:org.jacoco.agent:0.8.7"
}
build.gradle文件中的dependencies区域添加以下代码:
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.3"
在build.gradle(Module)文件添加以下配置:
plugins {
    id "org.sonarqube" version "3.3"
}
sonarqube {
    properties {
        property "sonar.projectKey", "your-project-key"
        property "sonar.organization", "your-org"
        property "sonar.host.url", "https://sonarcloud.io"
        property "sonar.branch.name", "your-branch-name"
        property "sonar.coverage.jacoco.xmlReportPaths", "${project.buildDir}/jacoco/testDebugUnitTest.exec"
        property "sonar.junit.reportPaths", "${project.buildDir}/test-results/testDebugUnitTest"
        
    }
}
在终端中输入以下命令:
./gradlew clean jacocoTestReport sonarqube
这条命令将重新编译您的