One easy way to implement Custom Rules in Android Projects is by using the Regex-based linting tool AnyLint. It's written in Swift though and therefore it only works on macOS and Linux at the moment (Windows support is in the works). But you don't really need Swift knowledge to use it, just follow it's documentation.
It's the easiest and fastest way to implement custom rules for any language, basically and supports example validation and even autocorrection.
For example, you could write a custom rule to prevent multiple empty lines like so (supports autocorrect):
#!/usr/local/bin/swift-sh
import AnyLint // @Flinesoft ~> 0.6.0
try Lint.logSummaryAndExit(arguments: CommandLine.arguments) {
// MARK: - Variables
let kotlinFiles: Regex = #"^app/src/main/kotlin/.*\.kt$"#
let javaFiles: Regex = #"^app/src/main/java/.*\.java$"#
let xmlFiles: Regex = #"^app/src/main/res/.*\.xml$"#
let gradleFiles: Regex = #"^.*\.gradle$"#
// MARK: MultilineWhitespaces
try Lint.checkFileContents(
checkInfo: "MultilineWhitespaces: Restrict whitespace lines to a maximum of one.",
regex: #"\n( *\n){2,}"#,
matchingExamples: ["}\n \n \n\nclass", "}\n\n\nvoid"],
nonMatchingExamples: ["}\n \n class"],
includeFilters: [kotlinFiles, javaFiles, xmlFiles, gradleFiles],
autoCorrectReplacement: "\n\n",
autoCorrectExamples: [
["before": "}\n \n \n\n class", "after": "}\n\n class"],
["before": "}\n\n\nvoid", "after": "}\n\nvoid"],
]
)
}
I hope this helps.