Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonar: How do I use gradle to inspect an android project

I've been trying for a while now, to get sonar to inspect and analyze my android project. But no luck so far. The project i'm trying to implement in sonar is a test project with 2 classes with some test methods. All stuffed in the src directory of the project.

So this is what I've got so far.

A build.gradle file with the following settings:

buildscript {
repositories {
    mavenCentral()
}

dependencies {
    classpath 'com.android.tools.build:gradle:0.6.+'
}
}

apply plugin: 'android'
apply plugin: 'sonar'
apply plugin: "sonar-runner"

android {  

buildToolsVersion "18.0.1"
compileSdkVersion 18

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 16
}

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['com.example.GradleAndroidTest']
        resources.srcDirs = ['src']
        renderscript.srcDirs = ['src']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
    }
    instrumentTest.setRoot('test')
}


}

sonar {
server {
    url = "http://sonar.someserver.int"
}
database {
    url = "jdbc:mysql://sonar.someserver.int:3306/sonar"
    driverClassName = "com.mysql.jdbc.Driver"
    username = "*****"
    password = "*****"
}
}

sonarRunner {
sonarProperties {
    property "sonar.host.url", "http://sonar.someserver.int"
    property "sonar.jdbc.url", "jdbc:mysql://sonar.someserver.int:3306/sonar"
    property "sonar.jdbc.driverClassName", "com.mysql.jdbc.Driver"
    property "sonar.jdbc.username", "*****"
    property "sonar.jdbc.password", "*****"
}
}

To test this project I go the root directory of the project in the command prompt and type in:

gradle sonarRunner

After this the project is showed on sonarQube but there are no statisctics shown. No lines of code, nothing. My question to you is. Am I forgetting something? Am I doing something wrong.

I don't have much experience with gradle and sonar so now I seek help from people that do. Hope you can help!

like image 323
Kevin Groen Avatar asked Nov 21 '13 09:11

Kevin Groen


2 Answers

I found out what I did wrong. I forgot to add a couple of mandatory properties.

sonarProperties {
    property "sonar.host.url", "http://sonar.someserver.int"
    property "sonar.jdbc.url", "jdbc:mysql://sonar.someserver.int:3306/sonar"
    property "sonar.jdbc.driverClassName", "com.mysql.jdbc.Driver"
    property "sonar.jdbc.username", "*****"
    property "sonar.jdbc.password", "*****"

    //I added these properties to my gradle.build
    property "sonar.projectKey", "GradleAndroidTest"
    property "sonar.projectName", "GradleAndroidTest"
    property "sonar.projectVersion", "V1.0"
    property "sonar.language", "java"
    property "sonar.sources", "src"
    property "sonar.binaries", "build"
}

As @Peter Niederwieser said, the plugin couldn't preconfigure these things so I had to add them manually.

I also deleted the sonar plugin and used the sonar-runner plugin instead.

like image 161
Kevin Groen Avatar answered Oct 24 '22 04:10

Kevin Groen


I think one problem is that the Android plugin currently uses its own source set model. The sonarRunner plugin isn't aware of that model and hence can't preconfigure things like it usually does. However, you should be able to supply this information yourself, by explicitly configuring the necessary Sonar properties.

To find out which Sonar properties are preconfigured by the sonarRunner plugin, check out the Groovydoc for the SonarRunnerPlugin class. A list of all existing Sonar properties is available on the Sonar website.

PS: You should only apply one of the sonar and sonarRunner plugins. I recommend to use the latter (which is the successor of the former) even though it's still incubating.

like image 29
Peter Niederwieser Avatar answered Oct 24 '22 02:10

Peter Niederwieser