Custom lint rule added for a XML Layout tag, doesn't report the issue in layout file

68 Views Asked by At

So, I have created a custom lint rule for FlowLayout tag (a 3rd party library) to replace it with ConstraintLayout's Flow feature, here is the code:

class FlowLayoutDetector : LayoutDetector() {

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

    override fun getApplicableElements(): Collection<String>? {
        return listOf("com.nex3z.flowlayout.FlowLayout")
    }

    override fun visitElement(context: XmlContext, element: Element) {
        context.report(FLOW_LAYOUT_ISSUE, context.getElementLocation(element), briefMessage)
    }

    companion object {
        private const val briefMessage =
            "Please use ConstraintLayout's flow feature, it provides same behaviour as FlowLayout and help us to remove FlowLayout dependency." +
                    "Visit: https://developer.android.com/reference/androidx/constraintlayout/helper/widget/Flow"
        private val IMPLEMENTATION =
            Implementation(
                FlowLayoutDetector::class.java,
                EnumSet.of(Scope.RESOURCE_FILE, Scope.TEST_SOURCES)
            )
        val FLOW_LAYOUT_ISSUE = Issue.create(
            id = "FlowLayoutDetector",
            briefDescription = briefMessage,
            explanation = briefMessage,
            category = Category.CORRECTNESS,
            priority = 10,
            severity = ERROR,
            androidSpecific = true,
            implementation = IMPLEMENTATION,
            enabledByDefault = true
        )
    }
}

Added FLOW_LAYOUT_ISSUE into issues list of IssueRegistry. called ./gradlew lint command, as a result, in the {module-name}-lint-result.xml, it has reported the error:

lint result

But in the layout file, no error is reported (unexpected):

layout file

Is there any way to get the affect of the custom lint in layout file as well? What am I missing?

1

There are 1 best solutions below

1
Claudio Galvão On

I don't know if I arrived too late, but try to delete the content from the baseline file and rerun the command to see if it shows the error on the terminal. When you run the command and there is no baseline file created, it will create the baseline file and will ignore the error.