Improve SonarQube plugin to support Android projects

Hi guys,

I’m working on adding support for Android projects in the SonarQube plugin. Basically when the current project is an Android project (ie have the com.android.application or com.android.library plugin applied) I would like to retrieve:

  • source set locations (in order to set sonar.sources property)
  • test files/directories (in order to set sonar.tests property)
  • classpath (for sonar.java.libraries)
  • build output dir (for sonar.java.binaries)
  • and also the path to the AndroidLint report

I tried playing with Android plugin API but:

  • I only managed to get source and test folders, but using a methode annotated @VisibleForTesting (so not an API)
  • I’m a bit concerned about adding a dependency on Android plugin in SonarQube plugin. Do you need a way to have an optional dependency (ie only access those methods when the current project is an Android project?)

Here is work in progress:
https://github.com/SonarCommunity/sonar-gradle/blob/android_support/src/main/java/org/sonarqube/gradle/SonarQubePlugin.java#L222

Any help would be much appreciated.

You could potentially use the notion of optional dependencies for this. You could then use plugins.withId() instead of withType() to key off the existence of the Android plugin without actually referencing the classes. It should then be safe to reference Android classes within the Action seeing as it would only be executed in the context of an Android project.

The other option would be to separate the Android specific logic into a separate plugin which would be applied only to Android projects. My preference is somewhat leaning towards the latter option as it’s more explicit.

Thanks Mark. This would indeed help to solve the dependency issue.

Still I’m not able to find how to retrieve build classpath (.class output folder and libraries) that is needed to have proper analysis of .java files by SonarQube. Does someone knows the internals of Android plugin?

I’m not sure there’s a public API for that. You might want to ask on the adt-dev mailing list.

I am reverting to sonar-runner as I cannot setup sonar.java.library correctly in my clients android studio project. I am not sure how to vote for this issue, but it is very important to get it fixed before gradle 3.0 comes out. I hope I am not crashing into you discussion but wanted to add my two cents approving of the work you are doing.

I fail to see how using sonar-runner will help… you still have too manually provide the classpath.

Sorry for resurrecting this old thread but we worked last week on supporting Android projects out of the box. Feel free to test version 2.1-rc1 of the plugin and provide feedback on:
https://groups.google.com/forum/#!topic/sonarqube/ZLaC2ickLSM