Sonar: Create new rule for Android Lint

2019-08-09 20:28发布

问题:

I am using SonarQube 5.1 in order to collect all Java and Lint errors for Android apps.

Almost every important issue is already caught by Sonar using Lint plugin, but I have found at least one issue not included:

<issue
    id="GradleOverrides"
    severity="Warning"
    message="This `minSdkVersion` value (`14`) is not used; it is always overridden by the value specified in the Gradle build script (`15`)"
    category="Correctness"
    priority="4"
    summary="Value overridden by Gradle build script"
    explanation="The value of (for example) `minSdkVersion` is only used if it is not specified in the `build.gradle` build scripts. When specified in the Gradle build scripts, the manifest value is ignored and can be misleading, so should be removed to avoid ambiguity."
    errorLine1="    &lt;uses-sdk android:minSdkVersion=&quot;14&quot; />"
    errorLine2="              ~~~~~~~~~~~~~~~~~~~~~~~~~~">
    <location
        file="C:\Code\Android\TestApp\app\src\main\AndroidManifest.xml"
        line="5"
        column="15"/>
</issue>

This issue is related to AndroidManifest.xml (legacy way to set Android minSdkVersion attribute) and build.gradle (current way to set minSdkVersion element).

I have read the documentation about creating new rules in Sonar, but I am still confused. I have downloaded the SSLR for XML and executed it to find the XPath string.

My XML file is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.juanacaja.testapp" >

    <uses-sdk android:minSdkVersion="14" />

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

...and the XPath to look for minSdkVersion element is:

//manifest/uses-sdk/@android:minSdkVersion

I am not sure what I must do now. Moreover, the SonarQube 5.1 web interface has changed and I don't see how to add the XPath string to the new rule.

Any help will be very appreciated!

回答1:

Accidentally I have found how to do this. Steps:

  1. Log in as administrator or other user with sufficient rights.
  2. Look for XPath rule for the language, XML in this case.
  3. Press Create button near "Custom Rules" text and fill form.

Do not forget to activate this custom rule in your profile.

PS: IMHO, the underlying problem is that documentation is not updated, and the vast majority of URLs are currently broken, due CodeHaus termination. Until old broken links will be corrected, it would be great if automatic redirection be performed.