How to register AndroidLintInspectionBase in IntellijIdea Plugin

Answered

I'm creating a costum Android Lint Inspection and I need to register the inspection, to be run. Where do I need to register it?

I've already tried to register the inspection which provides the inspection inside plugin.xml file.

The actual inspection:

class HardcodedDimensionsInspection : AndroidLintInspectionBase("Hardcoded dimensions", HardcodedDimensDetector.ISSUE) {

    override fun getShortName(): String {
        return "AndroidLintHardcodedDimension"
    }
}

The entry in plugin.xml file

<extensions defaultExtensionNs="com.intellij">
        <!-- Add your extensions here -->

<!--        <inspectionToolProvider implementation="JavaInspectionProvider"/>-->


        <globalInspection shortName="AndroidLintHardcodedDimension" displayName="Hardcoded dimensions"
                          enabledByDefault="true" level="WARNING"
                          implementationClass="HardcodedDimensionsInspection"/>
    </extensions>

The actual detector

class HardcodedDimensDetector : LayoutDetector() {
    override fun getApplicableAttributes(): Collection<String>? {
        return Arrays.asList(
                // Layouts
                ATTR_TEXT
        )
    }

    override fun appliesTo(folderType: ResourceFolderType): Boolean {
        return (folderType == ResourceFolderType.LAYOUT ||
                folderType == ResourceFolderType.MENU ||
                folderType == ResourceFolderType.XML)
    }

    override fun visitAttribute(context: XmlContext, attribute: Attr) {
        val value = attribute.value
    }

    companion object {

        /** The main issue discovered by this detector  */
        @JvmField
        val ISSUE = Issue.create(
                id = "HardcodedDimension",
                briefDescription = "Hardcoded dimens",
                explanation = """
                Brief
                """,
                category = Category.I18N,
                priority = 5,
                severity = Severity.ERROR,
                implementation = Implementation(
                        HardcodedDimensDetector::class.java,
                        Scope.RESOURCE_FILE_SCOPE
                )
        )
    }
}

I've expected to hit the breakpoints in any of the functions for Detector but the code is never called. Seems like my detector is not registered. Can you please point me to the missing part, is there a class where I should register my Detector?

Thank you.

 

The link to the full project: https://github.com/magicbytes/Android-Lint-Inspection

3 comments
Comment actions Permalink

Maybe the problem is that you use default package for your classes. Try to provide non-default package.

0
Comment actions Permalink

Thank you Dmitry, let me change the structure of the project and see if that will help. For now I have a workaround, but not sure it's the official one.

Workaround: 

Android Lint has a registry with all the Issueclasses (built-in), the class is called LintIdeIssueRegistry. When it runs the Android Lint, it's looking in this registry for Issue processors. Since the list is hardcoded, we need to inject ours in the list. I'm using the following code for that:

val registry = LintIdeIssueRegistry()
val myIssue = registry.getIssue(HardcodedDimensDetector.ISSUE.id)
if (myIssue == null) {
   val list = registry.issues as MutableList<Issue>
   list.add(HardcodedDimensDetector.ISSUE)
}

Hopefully in future we will have a method called addIssueinside the LintIdeIssueRegistry.

0
Comment actions Permalink

It seems you are right, you need to add issue manually.

0

Please sign in to leave a comment.