Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: run sonarRunner from gradle

I'm stuck trying to get sonar reporting to run with an gradle Android project. Because most of the important sonar properties are only applied by the sonarRunner gradle plugin when the project is a java project I'm having trouble applying them for the com.android.application project.

This is my sonarRunner config:

sonarRunner {
    sonarProperties {
        property "sonar.sourceEncoding", "UTF-8"
        property "sonar.profile", "Android Lint"
        property "sonar.sources", android.sourceSets.main.java.srcDirs
        property "sonar.binaries", file("${project.buildDir}/intermediates/classes/app") 
        property "sonar.libraries", "" // what to put here?
    }
}

The problem is, that sonar is complaining about classes not being found because the libraries could not be referenced.

How can the dependencies + android libraries being referenced in my sonarRunner configuration?


Example Error output:

INFO  - Load batch settings
INFO  - User cache: C:\Users\mannaz\.sonar\cache
INFO  - Install plugins
INFO  - Install JDBC driver
INFO  - Create JDBC datasource for jdbc:postgresql://sonar.local/sonar?useUnicode=true&characterEncoding=utf8
INFO  - Initializing Hibernate
INFO  - Load project settings
INFO  - Apply project exclusions
WARN  - 'sonar.dynamicAnalysis' is deprecated since version 4.3 and should no longer be used.
INFO  - -------------  Scan app
INFO  - Load module settings
INFO  - Loading technical debt model...
INFO  - Loading technical debt model done: 20 ms
INFO  - Loading rules...
INFO  - Loading rules done: 584 ms
INFO  - Configure Maven plugins
INFO  - Compare to previous analysis (2014-08-05)
INFO  - Compare over 30 days (2014-07-06, analysis of 2014-07-07 11:33:19.0)
INFO  - Compare to previous version (2014-07-21)
INFO  - No quality gate is configured.
INFO  - Base dir: C:\Users\mannaz\workspace\project\app
INFO  - Working dir: C:\Users\mannaz\workspace\project\app\build\sonar
INFO  - Source dirs: C:\Users\mannaz\workspace\project\app\src\main\java
INFO  - Binary dirs: C:\Users\mannaz\workspace\project\app\build\intermediates\classes\app
INFO  - Source encoding: UTF-8, default locale: de_AT
INFO  - Index files
INFO  - 197 files indexed
INFO  - Quality profile for java: Android Lint
INFO  - Sensor JavaSquidSensor...
INFO  - Java Main Files AST scan...
INFO  - 197 source files to be analyzed
ERROR - Class not found: android.widget.RelativeLayout
ERROR - Class not found: android.os.Handler
ERROR - Class not found: android.content.Context
ERROR - Class not found: android.app.Activity
ERROR - Class not found: android.util.AttributeSet
ERROR - Class not found: android.view.View
ERROR - Class not found: com.nostra13.universalimageloader.core.DisplayImageOptions
ERROR - Class not found: com.google.gson.Gson
like image 475
whlk Avatar asked Aug 05 '14 09:08

whlk


People also ask

How to run SonarQube in Gradle?

login property in your command line or you configure it as part of your gradle. properties file. Execute gradle sonarqube -Dsonar. login=yourAuthenticationToken and wait until the build has completed, then open the web page indicated at the bottom of the console output.

What are Gradle plugins?

A plugin is simply any class that implements the Plugin interface. Gradle provides the core plugins (e.g. JavaPlugin ) as part of its distribution which means they are automatically resolved. However, non-core binary plugins need to be resolved before they can be applied.

What is Sonar Java libraries?

sonar.java.binaries (required) Comma-separated paths to directories containing the compiled bytecode files corresponding to your source files. sonar.java.libraries. Comma-separated paths to files with third-party libraries (JAR or Zip files) used by your project.


1 Answers

I have a solution which works for my multi-project Gradle build:

subprojects.each { p ->
    sonarRunner {
        sonarProperties {
            // ... some settings omitted ...
            property p.name + '.sonar.java.binaries', p.sourceSets.main.output.classesDir
            property p.name + '.sonar.java.libraries', p.sourceSets.test.runtimeClasspath.filter { File f -> f.exists() }
        }
    }
}

Please note that

  1. I use the test runtime classpath libraries, because we have a providedCompile scope which would not be considered in the main runtime classpath.
  2. The sonar-runner complains if the libraries setting contains directories which don't exist (e.g. because a module doesn't have src/main/resources)
  3. sonar.java.libraries is used, because it doesn't work with sonar.libraries for me.
like image 191
Michael Schmeißer Avatar answered Sep 28 '22 03:09

Michael Schmeißer