How do you implement a custom lint rule in Android?
Instruction: Describe the steps to create and apply a custom lint rule for enforcing specific coding standards or practices.
Updated
Official answer
I would define one check precisely, including permitted cases and a useful diagnostic. For example, a project-specific resource-ID rule must distinguish a new ID declaration from a reference and should not flag unrelated XML text.
Create a separate JVM checks module with the compatible lint API as a compile-only dependency and lint test support for tests. Implement an Issue with a stable ID, severity, explanation and implementation scope, plus a Detector using the appropriate XML or source scanner. Inspect structured XML/UAST rather than search whole files for a word.
Expose the Issue through an IssueRegistry and package service-loader metadata under META-INF/services using the fully qualified IssueRegistry type name as the filename and the registry implementation name as its content. Declare the lint API compatibility information for versions actually tested; do not confuse it with Android minSdk.
Use the app module's lintChecks dependency configuration for local checks. Use lintPublish when an Android library must include its check JAR in the published AAR for consumers. A normal runtime implementation dependency is not the same integration.
Test violations and valid cases, suppression, diagnostic location, aliases or XML namespace differences where relevant, and any proposed fix. Run the project's actual lint task and verify the check is loaded there as well as in unit tests. Pin compatible tooling and retest upgrades; lint APIs can change.