A few months ago, my team came upon an agreement that when leaving a TODO anywhere in our code, we need to always provide several things:
- the person who is expected to address the TODO
- date when the TODO was left
- a comment or explanation on what needs to be done
I created a live template to support adherence to this rule, but why not go one step further and integrate the rule into our daily workflow?
We have previously seen:
Having tests for our custom Lint rule is really important. We do not want Lint to flag errors, uhm, erroneously. It results in frustration and users might just turn off our rule.
Serving up files ๐
Lint checks run on files, so for each of our test cases we need to provide mock files.
Lint provides a TestFile
API that allows us to create these mock files inline.
TestFiles.java(
"""
package test.pkg;
public class TestClass1 {
// In a comment, mentioning "lint" has no effect
}
"""
)
TestFiles.kotlin(
"""
package test.pkg
class TestClass {
// In a comment, mentioning "lint" has no effect
}
"""
I am using raw strings so that I do not have to worry about escaping special characters.
It also gives us very nice syntax highlighting!
Furthermore, if you choose โEdit Kotlin Fragmentโ from the hint, Android Studio will open up a file editor. Any changes you make in this editor will immediately reflect in your TestFile
. Pretty cool!
Letโs get testing ๐ฌ
The gateway to our test cases is TestLintTask. We need to provide it the following:
- files we want to run our checks on
- the issue we are testing against
- expected result
@Test
fun testKotlinFileNormalComment() {
TestLintTask.lint()
.files(
TestFiles.kotlin(
"""
package test.pkg
class TestClass {
// In a comment, mentioning "lint" has no effect
}
"""
)
)
.issues(TodoDetector.ISSUE)
.run()
.expect("No warnings.")
}
Note that we have the option here of using expect("No warnings.")
or expectClean()
.
For the test cases where we expect an error to occur, we need to put in the text that Lint spits out (i.e. similar to what you see in the console when running .gradlew :app:lintDebug
). The trickiest thing about this is that the string has to match exactly, including where the squiggly lines are.
The easiest way to do this is to pass an empty string to expect()
and let the test fail. You can then copy-paste the error message into your test.
Retrieving the message for an error scenario
I wrote a few tests for the detector covering Java and Kotlin files, incorrect date formats, and โTODOโ casing. You can find them all here.
Bringing it all together ๐ค
Now that we have written our tests, itโs finally time to integrate our Lint rule into our app!
First we need to create our IssueRegistry
to let Lint know about our custom rule. We also need to provide an API value; for custom rules we can use the constant defined by the Lint API.
@Suppress("UnstableApiUsage")
class IssueRegistry : IssueRegistry() {
override val issues: List<Issue> = listOf(
TodoDetector.ISSUE
)
override val api = CURRENT_API
}
We then register our IssueRegistry
by creating a file with the fully qualified name of our file under a META-INF/services/
folder:
src/main/resources/META-INF/services/dev.zarah.lint.checks.IssueRegistry
๐ Most posts mention that adding this registry is enough, and I am probably doing something wrong but I found out that for my project, I still have to include my IssueRegistry
in the Manifest by adding this to my build.gradle.kts
file:
tasks {
jar {
manifest {
attributes(
"Lint-Registry-v2" to "dev.zarah.lint.checks.IssueRegistry"
)
}
}
}
We have set up everything and now itโs time to consume our custom lint rule! In the modules where you want the rules applied, add a lintChecks
entry in the dependencies
closure, build your project, and everything should be good to go! ๐โโ๏ธ
dependencies {
lintChecks project(':checks')
}
Seeing it in action ๐ฝ๏ธ
Finally! We have come to the end of our journey! Hereโs our detector in action:
Custom lint rule with quickfix
The source code for this Lint series is available on Github (diff).
๐โโ๏ธ I have learned so many things whilst I was doing this task.
As I mentioned on Twitter, thereโs barely any public documentation at all. The talks Iโve seen are way too advanced for me (for example, I needed to understand what PSI and UAST were before it clicked and most talks barely even define them).
There was a lot of trial and error, and so. much. guesswork. It was incredibly frustrating. Major props to Mike Evans who patiently guided me through the hours and hours of pair programming that we did. If he didnโt help, I wouldnโt even know where to start. Sure I can copy-paste from samples but I want to understand why things are done the way they are โ thatโs how I learn.
I probably have made wrong assumptions whilst writing these posts ๐คทโโ๏ธ but again, no documentation so this is the best I could do. ๐
Anyway, what I want to say is, I usually only see the final output of other peopleโs work and sometimes I cannot help but feel jealous โ how come they find it super easy whilst Iโm out here crying because I donโt understand anything? I needed to remind myself that itโs okay to be frustrated, itโs okay to take your time to learn, and itโs okay to ask for help.
Further reading (and/or watching) ๐
If youโre keen to learn more, here are some resources about Lint and custom Lint rules:
- Coding in Style: Static Analysis with Custom Lint Rules, Android Dev Summit 2019 (Alan Viverette, Rahul Ravikumar)
- Kotlin Static Analysis with Android Lint (Tor Norbye)
- Lint API source code
- AndroidX documentation
- Android Lint Checks demo (Alex Lockwood)
- lint-dev Google group
Top comments (0)