Custom Android Lint for kotlin only (no java)

Viewed 881

I decided to write custom lint checks for Android and I have a strong belief in Android Lint, so I don't want to use detekt or ktlint for checking code (they can't parse xml files, etc and I'd like to use a single tool).

But when I've read docs for Android Lint, I found that they have this new UAST and PSI (com.intellij.psi package) stuff, when detekt uses its child (kotlin only org.jetbrains.kotlin.psi).

I don't have java code in my project and I'd like to write kotlin specific lint rules.

I see, that com.android.tools.lint.detector.api.Detector has a method createPsiVisitor which returns JavaElementVisitor, and this JavaElementVisitor is subclass of com.intellij.psi.PsiElementVisitor. Is there any chance, that it's possible to use org.jetbrains.kotlin.psi.KtVisitor instead?

Or how can I write kotlin specific checks in Detector.UastScanner?

I want (for example) to write rules for

Kotlin hidden costs set of articles (1, 2, 3)

P.S. I succeeded in doing this in detekt, but I'd like to use default Android Lint.

[UPD] I found com.android.tools.lint.checks.InteroperabilityDetector. Probably it's possible to write kinda KotlinVisitor (as JavaVisitor in this class). And this KotlinVisitor will be just wrapper around KtVisitor?

1 Answers

In version 3.4, the Kotlin PSI is available directly to custom lint checks (it's on the classpath). When you're handed a UFile (the UAST abstraction on top of the Java and Kotlin AST's) it has a sourcePsi field which gives you the KtFile.

There's a "lint-dev" Google group you can read/join to discuss lint check development in more detail.

Related