Running Sonar on iOS (Objective-C) Projects with Gradle

September 8, 2014 3 By Tad Reeves

After getting the basics out of the way on setting up Sonarqube on Mac OSX, one has to invoke it somehow.   We settled on building our iOS project with Gradle, and as such, are using the sonarRunner plugin for Gradle to fire up the Sonar analysis.

Making the Sonar Gradle Plugin Work

Getting the SonarQube plugin for Gradle to work was pretty straightforward.

In your build.gradle file, first insert:

apply plugin: "sonar-runner"

Then, define Sonar properties for Objective-C analysis as follows:

// Sonar Properties for Objective-C
sonarRunner {
    sonarProperties {
        property "", ""
        property "sonar.jdbc.url", "jdbc:mysql://"
        property "sonar.jdbc.driverClassName", "com.mysql.jdbc.Driver"
        property "sonar.jdbc.username", "sonar"
        property "sonar.jdbc.password", "sonar"
        property "sonar.language", "objc"
        property "sonar.projectKey", "org.codehaus.sonar:superdandymobile-objc-project"
        property "sonar.projectName", "SuperDandy Mobile"
        property "sonar.sources", "src"
        property "sonar.cfamily.gcov.reportsPath", "output"

This will then give you a target when invoking the Gradle wrapper of “sonarRunner”, such that you can just execute:

macmini-superdandy04:trunk administrator$ ./gradlew sonarRunner --info

And this will go about downloading the needful dependencies to make the SonarQube plugin work, and will then insert the analysis data into Sonar.

Making GCov Test Coverage Reports Work

Now, the above analysis worked pretty trouble free for me.  However, as noted in the docs for the SonarSource Objective-C plugin, the plugin will not, out of the box, give you test coverage / code coverage reports.

You’ll first have to get the xcode project to generate the gcno/gcda code coverage reports as covered here.

Then, execute something like the following to get the gcov files generated from that:

macmini-superdandy04:output administrator$ find . -type f | xargs -I {} xcrun gcov {}

That final line in the sonar properties is meant to then tell it where to look for the gcov reports:

property "sonar.cfamily.gcov.reportsPath", "output"

EDIT:  Just found out that despite code coverage and ingest of gcov files being mentioned in their documentation, the C++/Objective-C plugin for SonarQube presently does not do code/test coverage reporting for Objective-C.  This is actively under development, meant to release in October 2014.