Android: run sonarRunner from gradle

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

This is my sonarRunner configuration:

 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 the sonar complains that the classes were not found, because libraries cannot be referenced.

How can I reference android dependencies + libraries 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 
+6
source share
2 answers

I have a solution that 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() } } } } 

note that

  • I use the class libraries of the trajectories of the test run because we have an area of ​​the provided compilation area that will not be taken into account in the path to the main runtime.
  • Sonar-runner complains if the library settings contain directories that do not exist (for example, since the module does not have src / main / resources)
  • sonar.java.libraries used because for me it does not work with sonar.libraries .
+1
source

I think you just need to set the library property manually with the correct configuration. Something like that:

 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", "android.sourceSets.main.runtimeClasspath" } } 

Or what your correct dependency configuration is right.

0
source

Source: https://habr.com/ru/post/973358/


All Articles