Skip to content

Instantly share code, notes, and snippets.

@cesarioputera
Created November 4, 2019 10:19
Show Gist options
  • Save cesarioputera/859456a3846776ca82dd1904180ce964 to your computer and use it in GitHub Desktop.
Save cesarioputera/859456a3846776ca82dd1904180ce964 to your computer and use it in GitHub Desktop.
apply from: "../jacoco.gradle"
def debugTree = fileTree(dir: "${buildDir}/intermediates/packaged-classes/flavorUnsigned", includes: includeFileFilter, excludes: excludeFileFilter)
def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/flavorUnsignedDebug", includes: includeFileFilter, excludes: excludeFileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
task jacocoTestReport(type: JacocoReport, dependsOn: ['testFlavorUnsignedDebugUnitTest', 'assembleDebug']) {
reports {
xml.enabled = true
html.enabled = true
}
getSourceDirectories().setFrom(files([mainSrc]))
getClassDirectories().setFrom(files([debugTree], [kotlinDebugTree]))
getExecutionData().setFrom(fileTree(dir: "$buildDir", includes: [
"jacoco/testFlavorUnsignedDebugUnitTest.exec",
]))
}
check.dependsOn jacocoTestReport
apply plugin: "jacoco"
ext.excludeFileFilter = [
//ExcludeFiles
]
jacoco {
toolVersion = "0.8.2"
}
ext {
limits = [
'instruction': 50
]
}
ext.includeFileFilter = [
//IncludedFiles
]
android {
testOptions {
unitTests.all {
jacoco {
includeNoLocationClasses = true
}
}
unitTests.returnDefaultValues = true
}
}
apply from: '../sonarqube.gradle'
sonarqube {
//read the flavor from properties.
def flavorName = "flavorUnsigned"
def variant = "debug"
androidVariant flavorName + variant.capitalize()
properties {
def libraries = project.android.sdkDirectory.getPath() + "/platforms/android-28/android.jar" + ",build/intermediates/packaged-classes/**/classes.jar"
property "sonar.projectVersion", app_version
property 'sonar.binaries', "build/intermediates/packaged-classes/$flavorName,build/tmp/kotlin-classes/$androidVariant"
property "sonar.libraries", libraries
property 'sonar.java.binaries', "build/intermediates/packaged-classes/$flavorName,build/tmp/kotlin-classes/$androidVariant"
property "sonar.java.libraries", libraries
property 'sonar.java.test.binaries', "build/intermediates/packaged-classes/$flavorName,build/tmp/kotlin-classes/$androidVariant"
property "sonar.java.test.libraries", libraries
property 'sonar.junit.reportsPaths', "build/test-results/test${androidVariant.capitalize()}UnitTest"
property 'sonar.jacoco.reportPaths', "build/jacoco/test${androidVariant.capitalize()}UnitTest.exec"
}
}
apply plugin: 'org.sonarqube'
sonarqube {
properties {
property "sonar.projectVersion", app_version
property "sonar.sourceEncoding", "UTF-8"
property "sonar.sources", "src/main/java"
property "sonar.tests", "src/test/java"
property "sonar.dynamicAnalysis", "reuseReports"
property 'sonar.java.coveragePlugin', 'jacoco'
property 'sonar.coverage.exclusions', excludeFileFilter.join(',')
property "sonar.scm.provider", "git"
}
}
@esafirm
Copy link

esafirm commented Nov 4, 2019

oh one more thing, how about the sonar's setting? In my case did the same thing like jacoco's setting, create 1 sonar-lib.gradle and include it in each build.gradle

I haven't tried it yet, but i think the principle is the same.
Also, you could configure it to apply your gradle files to all your subprojects

// in your root gradle
subProjects {
   apply from : sonarqube.gradle
}

@cesarioputera
Copy link
Author

Hi @anton46 @esafirm
The issue is resolved now
Thank you for the suggestion
After did the changes (implement the suggested approach), the problem is the path and there are some deprecated properties in sonar

sonarqube {
    //read the flavor from properties.
    def flavorName = "flavorUnsigned"
    def variant = "debug"
    androidVariant flavorName + variant.capitalize() --------> //outside

    properties {
        ...
        property 'sonar.binaries', "build/tmp/kotlin-classes/$androidVariant" --------> //inside
        property "sonar.libraries", libraries
        ...
        property 'sonar.junit.reportsPaths', "build/test-results/test${androidVariant.capitalize()}UnitTest"
        property 'sonar.jacoco.reportPaths', "build/jacoco/test${androidVariant.capitalize()}UnitTest.exec"
    }
}
  1. Path issue: the androidVariant inside the propery{} is different with the outside androidVariant
  2. Deprecated property: sonar.jacoco.reportPaths has been deprecated and changed to sonar.coverage.jacoco.xmlReportPaths

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment