How to configure multi-module Gradle project to avoid "can’t be indexed twice" issue?

Continuing the discussion from Sonar plugin 4.3.0.3225 error:

I have updated the Gradle SonarQube plugin from 4.0.0.2929 to 4.4.0.3356 and stumbled on the “can’t be indexed twice” problem for my build.gradle.kts files.

I should add that I don’t find any helpful details on this matter from the samples at GitHub - SonarSource/sonar-scanning-examples: Shows how to use the Scanners

My layout looks like this:

"Project root folder"
|   |-- Module A
|   |   src
|   |   build.gradle.kts
|   `-- Module B
|   |   src
|   |   build.gradle.kts
build.gradle.kts

Obviously I want SonarQube to analyze my build.gradle.kts files but not include them in code coverage calculations

Currently my config looks something like this (some details omitted for clarity):

Root project:

sonar {

    properties {
        property("sonar.projectKey", "com.mycompany:myproduct")
        property("sonar.sourceEncoding", "UTF-8")
        property("sonar.qualitygate.wait", "true")
    }

}

Subprojects:

 sonar {
        properties {
            property("sonar.exclusions", "**/BuildConfig.class,**/R.java,**/R\$*.java,src/main/gen/**/*")
            property("sonar.sources", "src/main,build.gradle.kts")
            property("sonar.tests", "src/test")
            property("sonar.coverage.exclusions", "**/testapp/**/*")
            property(
                "sonar.coverage.jacoco.xmlReportPaths",
                "${buildDir}/reports/jacoco/jacocoTestReport/jacocoTestReport.xml"
            )
            property("sonar.junit.reportPaths", "${buildDir}/test-results/")
            property("sonar.androidLint.reportPaths", "${buildDir}/reports/lint-results-debug.xml")
        }
    }

Hello @Alix,

When you’re using scanner for Gradle, usually you don’t need to override sonar.sources properties it should be populated automatically.

In version 4.4 all the .gradle.kts files are added to the root project sources. When you add these files to the submodules, you force them to be indexed twice.

FYI, it was done on purpose, as analyzing rules for Gradle Kotlin DSL requires a connection to Gradle API and it is very slow, so the decision was done to putt all the files in the root module and connect to Gradle API once.

So, in your case, if you remove sonar.sources property from the submodules, the issue should disappear. Let me know, if you the issue is not resolved after this fix.

Best,
Margarita

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.