android-lint-checks-demo
This is a simple demo repo that shows how to configure and write custom lint checks in an Android project.
Custom lint checks
Custom lint checks are a great way to prohibit usages of certain classes and resources in a codebase. To demonstrate their power, this project contains the following custom lint checks:
AndroidToastJavaKotlinDetector
- Prohibits usage of the AndroidToast
class in Java and Kotlin code and suggests using aSnackbar
from the support library instead.DeprecatedButtonXmlDetector
- Prohibits usages of a hypotheticalDeprecatedButton
class in layout XML resource files.DeprecatedButtonJavaKotlinDetector
- Prohibits instantiations of a hypotheticalDeprecatedButton
class in Java and Kotlin code.DeprecatedPurpleColorXmlDetector
- Prohibits references to a hypothetical@color/deprecated_purple
resource in layout XML resource files.DeprecatedPurpleColorJavaKotlinDetector
- Prohibits references to a hypotheticalR.color.deprecated_purple
resource in Java and Kotlin code.HardcodedHexColorXmlDetector
- Prohibits hardcoded hex colors in layout XML resource files.OkayStringXmlDetector
- Suggests replacing 'Okay' with 'OK' in string resource XML files.
You can see the lint checks show up as errors if you open this project in Android Studio and look at the MainActivity.kt
and activity_main.xml
files.
You can view the unit tests for each custom lint check here.
Project setup
This project contains the following two modules:
checks/
)
Lint check jar library (This module is the lint check jar library that other Android library modules can consume. It contains the custom lint check implementations listed above, each of which are listed in the IssueRegistry
class.
app/
)
Android app module (This module contains a generic sample Android app. In order to get the custom lint checks running on the code in this module, it depends on the checks
module in the app/build.gradle
file:
dependencies {
lintChecks project(':checks')
}
Additional resources
- KotlinConf 2017 - Kotlin Static Analysis with Android Lint - An incredible 40 minute deep-dive into lint. I highly recommend watching this video.
- Getting the Most Out of Android Lint (Android Dev Summit '18) - A shorter video about Android lint, but also very informative.
- Source code for the Android Studio lint checks - In my opinion this is the best source of information when it comes to writing custom lint checks. You can learn a lot about how to write them by analyzing this source code.
- Lint mailing list - A great place to asking questions about anything related to lint.