Unable to use sonar into external gradle file

  • Gradle build : com.android.tools.build:gradle:4.0.2
  • Sonar version : org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.1
 FAILURE: Build failed with an exception.
 What went wrong:
com/android/build/gradle/api/BaseVariant > com.android.build.gradle.api.BaseVariant
 
 Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
 
 Exception is:
java.lang.NoClassDefFoundError: com/android/build/gradle/api/BaseVariant
    at org.sonarqube.gradle.SonarQubePlugin.lambda$null$12(SonarQubePlugin.java:130)
    at org.sonarqube.gradle.SonarQubePlugin.lambda$getAndroidCompileTasks$13(SonarQubePlugin.java:145)
    at org.gradle.util.GUtil.uncheckedCall(GUtil.java:425)
Caused by: java.lang.ClassNotFoundException: com.android.build.gradle.api.BaseVariant
    ... 120 more
 
 Get more help at https://help.gradle.org
 
BUILD FAILED in 489ms 
  • Create a file named sonar.gradle.
    Add this :
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.1"
    }
}
apply plugin: org.sonarqube.gradle.SonarQubePlugin
sonarqube {
    properties {
        property "sonar.sourceEncoding", "UTF-8"
        property "sonar.projectKey", "$moduleInfo.name"
        property "sonar.projectName", "$moduleInfo.displayName"
        property "sonar.projectDescription", "$moduleInfo.description"
        property "sonar.coverage.jacoco.xmlReportPaths", "build/reports/jacoco/testEnvprodReleaseUnitTestCoverage/testEnvprodReleaseUnitTestCoverage.xml"
        property "sonar.projectVersion", moduleVersionName
    }
} 

Use code sonarqube into an external gradle file and call it using “apply from” in the app gradle file

apply from: "sonar.gradle" 
  • put everything in the app gradle file

Hi @Alexis.Caruana_SE ,

Welcome to SonarSource Community! :sonarsource:

Did you add the following in your plugins block in build.gradle?

plugins {
  id "org.sonarqube" version "3.1.1"
}

See docs for more info: SonarScanner for Gradle | SonarQube Docs

Joe

Hello,

The objective is to put the whole Sonar gradle block in a external gradle file (named here sonar.gradle) and to only add apply from: "sonar.gradle" to the build.gradle.

The plugin {} syntax cannot be used in external gradle file unfortunately. Gradle documentation states that “Future versions of Gradle will remove this restriction”.

The old syntax has to be used for now. As for why we have to use the fully qualified class name org.sonarqube.gradle.SonarQubePlugin instead of the string id "org.sonarqube", this is also a restriction in external gradle file.

Having tried to put the Sonar gradle block in the build.gradle, it works. The issue comes when we try to move the block in an external file.

Regards,
Guillaume

HI @gweybrechtSE ,

Welcome to SonarSource Community! :sonarsource:

I see what you mean now. Please take a look at my example here for gradle multimodule project with code coverage set up already. I use sonar.gradle in a separate file and reference it in my build.gradle via apply from: "$project.rootDir/sonar.gradle". It’s a PR for now but I plan to merge it eventually: sonar-scanning-examples/sonarqube-scanner-gradle/gradle-multimodule-with-code-coverage at 1d7b02e5a20a11539ca245d5e3b5a034e0614753 · SonarSource/sonar-scanning-examples · GitHub

This project uses modules/subprojects so note that difference as necessary.

Joe