3
votes

I'm struggling to set up correctly SonarQube 5.4 for a multi-module project. I've read here, here and here, all of the provided solutions doesn't solve my errors: I got during a Sonar analyze for every class I created (not for libraries I included) a warning:

"Class XYZ is not accessible through the ClassLoader."

and I get no results in the Sonar dashboard (neither on localhost nor on Jenkins), not even the time of the analyze got updated. I'm using Gradle 2.10, here is my root Gradle file:

buildscript {
repositories {
    jcenter()
    mavenCentral()
    maven {
        url "https://plugins.gradle.org/m2/"
    }
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.0.0'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.0-rc1"
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
}
apply from: 'buildsystem/dependencies.gradle'
apply plugin: "org.sonarqube"

subprojects {
sonarqube {
    properties {
        property "sonar.host.url", "http://localhost:9000/"
        property "sonar.sources", "./src/main/java"
        property "sonar.java.binaries", "./build/"
        property "sonar.projectKey", "net.project"
        property "sonar.projectName", "Project Android"
        property "sonar.projectVersion", "0.5"
        property "sonar.sourceEncoding", "UTF-8"
        }
    }
}
allprojects {
ext {
    androidApplicationId = 'net.project'
    androidVersionCode = 1
    androidVersionName = "0.5"
    testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
    testApplicationId = 'com.beatclip.android.presentation.test'
    }

repositories {
    jcenter()
    }
}

task wrapper(type: Wrapper) {
description 'Creates the gradle wrapper.'
gradleVersion '2.10'
}

Could be Dagger2 the problem? I have no SonarQube configurations in the Gradle files for the modules set, since this example from SonarQube doesn't provide any Sonar configs in module Gradle files as well.

Any help or example is appreciated! :)

1

1 Answers

0
votes

While I have no clue what's going on with gradle and how to set it up correctly, I somehow managed to get partially better results in the scan (still some classes fail to load, but mostly external ones, so I hope the scan works at least on internal ones).

I'm using the sonarqube plugin 2.0.1, sonarqube 4.5.7, gradle 2.14.1.

I'm setting this for final application module, so this will not help you directly with "multi-module", but maybe something similar can be put into subprojects part? As I don't understand groovy (1), it's very difficult and painful for me to change anything, took me several hours before I managed at least to get here.

(1) anything I try to write works in the other way than I expected, for example I would expect adding the classFilesTree (file tree instance) directly to property "sonar.java.binaries" would be enough to traverse the files, but I ended up with collect + {}.each to expand it before setting the property (I had several issues at that point, so I'm not sure this was correct step, and now it works, so I'm not going to experiment). I simply don't understand these high level languages, I can never imagine how it lands on the CPU and what executes where with what in memory, so I'm struggling a lot, my mind is trained to work over bytes, not abstractions.

plugins {  //instead of the old way with dependecies + apply plugin
    id "org.sonarqube" version "2.0.1"
}

// path to Android SDK jar file, requires sdk.dir=/path/file in local.properties
def androidSdkJarPath;
afterEvaluate {
    def rootDir = project.rootDir
    def localProperties = new File(rootDir, "local.properties")
    if (localProperties.exists()) {
        Properties properties = new Properties()
        localProperties.withInputStream { instr ->
            properties.load(instr)
        }
        def sdkDir = properties.getProperty('sdk.dir')
        androidSdkJarPath = sdkDir + "/platforms/" + android.compileSdkVersion + "/android.jar"
        logger.info('androidSdkJarPath: ' + androidSdkJarPath)
    }
}

sonarqube {
    properties {
        // ... other module configuration for sonar...

        FileTree classFilesTree = fileTree(dir: 'build/intermediates/classes', include: '**/release/**')
        FileTree jarFilesFromAarsTree = fileTree(dir: 'build/intermediates/exploded-aar', include: '**/jars/*.jar', exclude: ['**/debug/jars/**', '**/snapshot/jars/**'])

        def classFiles = []
        def jarFilesFromAars = []
        classFilesTree.collect { relativePath(it) }.each { classFiles += it }
        jarFilesFromAarsTree.collect { relativePath(it) }.each { jarFilesFromAars += it }
//        logger.warn('classFiles: ' + classFiles )
//        logger.warn('jarFilesFromAars: ' + jarFilesFromAars )
        // Still MIA (on my project): android.support.v4.**, ...

        property "sonar.java.binaries", classFiles
        property "sonar.libraries", [jarFilesFromAars, androidSdkJarPath]
    }
}

subprojects {
    sonarqube {
        properties["sonar.sources"] += "src/main/java"
        properties["sonar.test"] += ["src/androidTest/java", "src/test/java"]
    }
}