| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 1 | # Adding custom Lint checks |
| 2 | |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 3 | [TOC] |
| 4 | |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 5 | ## Getting started |
| 6 | |
| 7 | Lint is a static analysis tool that checks Android project source files. Lint |
| AndroidX Core Team | ee9c1aa | 2021-04-06 17:29:05 +0000 | [diff] [blame] | 8 | checks come with Android Studio by default, but custom lint checks can be added |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 9 | to specific library modules to help avoid potential bugs and encourage best code |
| 10 | practices. |
| 11 | |
| AndroidX Core Team | ee9c1aa | 2021-04-06 17:29:05 +0000 | [diff] [blame] | 12 | This guide is targeted to developers who would like to quickly get started with |
| 13 | adding lint checks in the AndroidX development workflow. For a complete guide to |
| 14 | writing and running lint checks, see the official |
| 15 | [Android lint documentation](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/docs/README.md.html). |
| 16 | |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 17 | ### Create a module |
| 18 | |
| 19 | If this is the first Lint rule for a library, you will need to create a module |
| 20 | by doing the following: |
| 21 | |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 22 | Include the project in the top-level `settings.gradle` file so that it shows up |
| 23 | in Android Studio's list of modules: |
| 24 | |
| 25 | ``` |
| 26 | includeProject(":mylibrary:mylibrary-lint", "mylibrary/mylibrary-lint") |
| 27 | ``` |
| 28 | |
| 29 | Manually create a new module in `frameworks/support` (preferably in the |
| 30 | directory you are making lint rules for). In the new module, add a `src` folder |
| 31 | and a `build.gradle` file containing the needed dependencies. |
| 32 | |
| AndroidX Core Team | e80aab7 | 2021-09-29 08:44:33 -0700 | [diff] [blame] | 33 | `mylibrary/mylibrary-lint/build.gradle`: |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 34 | |
| 35 | ``` |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 36 | import androidx.build.LibraryGroups |
| AndroidX Core Team | e80aab7 | 2021-09-29 08:44:33 -0700 | [diff] [blame] | 37 | import androidx.build.LibraryType |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 38 | import androidx.build.LibraryVersions |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 39 | |
| 40 | plugins { |
| 41 | id("AndroidXPlugin") |
| 42 | id("kotlin") |
| 43 | } |
| 44 | |
| 45 | dependencies { |
| AndroidX Core Team | e80aab7 | 2021-09-29 08:44:33 -0700 | [diff] [blame] | 46 | compileOnly(libs.androidLintMinApi) |
| 47 | compileOnly(libs.kotlinStdlib) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 48 | |
| AndroidX Core Team | e80aab7 | 2021-09-29 08:44:33 -0700 | [diff] [blame] | 49 | testImplementation(libs.kotlinStdlib) |
| 50 | testImplementation(libs.androidLint) |
| 51 | testImplementation(libs.androidLintTests) |
| 52 | testImplementation(libs.junit) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | androidx { |
| AndroidX Core Team | e80aab7 | 2021-09-29 08:44:33 -0700 | [diff] [blame] | 56 | name = "MyLibrary lint checks" |
| 57 | type = LibraryType.LINT |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 58 | mavenVersion = LibraryVersions.MYLIBRARY |
| 59 | mavenGroup = LibraryGroups.MYLIBRARY |
| 60 | inceptionYear = "2019" |
| AndroidX Core Team | e80aab7 | 2021-09-29 08:44:33 -0700 | [diff] [blame] | 61 | description = "Lint checks for MyLibrary" |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 62 | } |
| 63 | ``` |
| 64 | |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 65 | ### Issue registry |
| 66 | |
| 67 | Your new module will need to have a registry that contains a list of all of the |
| 68 | checks to be performed on the library. There is an |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 69 | [`IssueRegistry`](https://cs.android.com/android/platform/superproject/+/master:tools/base/lint/libs/lint-api/src/main/java/com/android/tools/lint/client/api/IssueRegistry.java;l=47) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 70 | class provided by the tools team. Extend this class into your own |
| 71 | `IssueRegistry` class, and provide it with the issues in the module. |
| 72 | |
| AndroidX Core Team | e80aab7 | 2021-09-29 08:44:33 -0700 | [diff] [blame] | 73 | `MyLibraryIssueRegistry.kt` |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 74 | |
| 75 | ```kotlin |
| 76 | class MyLibraryIssueRegistry : IssueRegistry() { |
| AndroidX Core Team | e80aab7 | 2021-09-29 08:44:33 -0700 | [diff] [blame] | 77 | override val api = 11 |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 78 | override val minApi = CURRENT_API |
| 79 | override val issues get() = listOf(MyLibraryDetector.ISSUE) |
| 80 | } |
| 81 | ``` |
| 82 | |
| AndroidX Core Team | e80aab7 | 2021-09-29 08:44:33 -0700 | [diff] [blame] | 83 | The maximum version this Lint check will will work with is defined by `api = |
| 84 | 11`, where versions `0`-`11` correspond to Lint/Studio versions `3.0`-`3.11`. |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 85 | |
| 86 | `minApi = CURRENT_API` sets the lowest version of Lint that this will work with. |
| 87 | |
| 88 | `CURRENT_API` is defined by the Lint API version against which your project is |
| 89 | compiled, as defined in the module's `build.gradle` file. Jetpack Lint modules |
| AndroidX Core Team | e80aab7 | 2021-09-29 08:44:33 -0700 | [diff] [blame] | 90 | should compile using the Lint API version referenced in |
| AndroidX Core Team | 4cc85fa | 2021-11-23 15:58:34 +0000 | [diff] [blame] | 91 | [Dependencies.kt](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:buildSrc/public/src/main/kotlin/androidx/build/dependencies/Dependencies.kt;l=176). |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 92 | |
| AndroidX Core Team | e80aab7 | 2021-09-29 08:44:33 -0700 | [diff] [blame] | 93 | We guarantee that our Lint checks work with the versions referenced by `minApi` |
| 94 | and `api` by running our tests with both versions. For newer versions of Android |
| 95 | Studio (and consequently, Lint) the API variable will need to be updated. |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 96 | |
| 97 | The `IssueRegistry` requires a list of all of the issues to check. You must |
| 98 | override the `IssueRegistry.getIssues()` method. Here, we override that method |
| 99 | with a Kotlin `get()` property delegate: |
| 100 | |
| AndroidX Core Team | e80aab7 | 2021-09-29 08:44:33 -0700 | [diff] [blame] | 101 | [Example `IssueRegistry` Implementation](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:fragment/fragment-lint/src/main/java/androidx/fragment/lint/FragmentIssueRegistry.kt) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 102 | |
| 103 | There are 4 primary types of Lint checks: |
| 104 | |
| 105 | 1. Code - Applied to source code, ex. `.java` and `.kt` files |
| 106 | 1. XML - Applied to XML resource files |
| 107 | 1. Android Manifest - Applied to `AndroidManifest.xml` |
| 108 | 1. Gradle - Applied to Gradle configuration files, ex. `build.gradle` |
| 109 | |
| 110 | It is also possible to apply Lint checks to compiled bytecode (`.class` files) |
| 111 | or binary resource files like images, but these are less common. |
| 112 | |
| 113 | ## PSI & UAST mapping |
| 114 | |
| 115 | To view the PSI structure of any file in Android Studio, use the |
| 116 | [PSI Viewer](https://www.jetbrains.com/help/idea/psi-viewer.html) located in |
| 117 | `Tools > View PSI Structure`. The PSI Viewer should be enabled by default on the |
| AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 118 | Android Studio configuration loaded by `studiow` in `androidx-main`. If it is |
| 119 | not available under `Tools`, you must enable it by adding the line |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 120 | `idea.is.internal=true` to `idea.properties.` |
| 121 | |
| 122 | <table> |
| 123 | <tr> |
| 124 | <td><strong>PSI</strong> |
| 125 | </td> |
| 126 | <td><strong>UAST</strong> |
| 127 | </td> |
| 128 | </tr> |
| 129 | <tr> |
| 130 | <td>PsiAnnotation |
| 131 | </td> |
| 132 | <td>UAnnotation |
| 133 | </td> |
| 134 | </tr> |
| 135 | <tr> |
| 136 | <td>PsiAnonymousClass |
| 137 | </td> |
| 138 | <td>UAnonymousClass |
| 139 | </td> |
| 140 | </tr> |
| 141 | <tr> |
| 142 | <td>PsiArrayAccessExpression |
| 143 | </td> |
| 144 | <td>UArrayAccessExpression |
| 145 | </td> |
| 146 | </tr> |
| 147 | <tr> |
| 148 | <td>PsiBinaryExpression |
| 149 | </td> |
| 150 | <td>UArrayAccesExpression |
| 151 | </td> |
| 152 | </tr> |
| 153 | <tr> |
| 154 | <td>PsiCallExpression |
| 155 | </td> |
| 156 | <td>UCallExpression |
| 157 | </td> |
| 158 | </tr> |
| 159 | <tr> |
| 160 | <td>PsiCatchSection |
| 161 | </td> |
| 162 | <td>UCatchClause |
| 163 | </td> |
| 164 | </tr> |
| 165 | <tr> |
| 166 | <td>PsiClass |
| 167 | </td> |
| 168 | <td>UClass |
| 169 | </td> |
| 170 | </tr> |
| 171 | <tr> |
| 172 | <td>PsiClassObjectAccessExpression |
| 173 | </td> |
| 174 | <td>UClassLiteralExpression |
| 175 | </td> |
| 176 | </tr> |
| 177 | <tr> |
| 178 | <td>PsiConditionalExpression |
| 179 | </td> |
| 180 | <td>UIfExpression |
| 181 | </td> |
| 182 | </tr> |
| 183 | <tr> |
| 184 | <td>PsiDeclarationStatement |
| 185 | </td> |
| 186 | <td>UDeclarationExpression |
| 187 | </td> |
| 188 | </tr> |
| 189 | <tr> |
| 190 | <td>PsiDoWhileStatement |
| 191 | </td> |
| 192 | <td>UDoWhileExpression |
| 193 | </td> |
| 194 | </tr> |
| 195 | <tr> |
| 196 | <td>PsiElement |
| 197 | </td> |
| 198 | <td>UElement |
| 199 | </td> |
| 200 | </tr> |
| 201 | <tr> |
| 202 | <td>PsiExpression |
| 203 | </td> |
| 204 | <td>UExpression |
| 205 | </td> |
| 206 | </tr> |
| 207 | <tr> |
| 208 | <td>PsiForeachStatement |
| 209 | </td> |
| 210 | <td>UForEachExpression |
| 211 | </td> |
| 212 | </tr> |
| 213 | <tr> |
| 214 | <td>PsiIdentifier |
| 215 | </td> |
| 216 | <td>USimpleNameReferenceExpression |
| 217 | </td> |
| 218 | </tr> |
| 219 | <tr> |
| 220 | <td>PsiLiteral |
| 221 | </td> |
| 222 | <td>ULiteralExpression |
| 223 | </td> |
| 224 | </tr> |
| 225 | <tr> |
| 226 | <td>PsiLocalVariable |
| 227 | </td> |
| 228 | <td>ULocalVariable |
| 229 | </td> |
| 230 | </tr> |
| 231 | <tr> |
| 232 | <td>PsiMethod |
| 233 | </td> |
| 234 | <td>UMethod |
| 235 | </td> |
| 236 | </tr> |
| 237 | <tr> |
| 238 | <td>PsiMethodCallExpression |
| 239 | </td> |
| 240 | <td>UCallExpression |
| 241 | </td> |
| 242 | </tr> |
| 243 | <tr> |
| 244 | <td>PsiParameter |
| 245 | </td> |
| 246 | <td>UParameter |
| 247 | </td> |
| 248 | </tr> |
| 249 | </table> |
| 250 | |
| 251 | ## Code detector |
| 252 | |
| 253 | These are Lint checks that will apply to source code files -- primarily Java and |
| 254 | Kotlin, but can also be used for other similar file types. All code detectors |
| 255 | that analyze Java or Kotlin files should implement the |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 256 | [SourceCodeScanner](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-api/src/main/java/com/android/tools/lint/detector/api/SourceCodeScanner.kt). |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 257 | |
| 258 | ### API surface |
| 259 | |
| 260 | #### Calls to specific methods |
| 261 | |
| 262 | ##### getApplicableMethodNames |
| 263 | |
| 264 | This defines the list of methods where lint will call the visitMethodCall |
| 265 | callback. |
| 266 | |
| 267 | ```kotlin |
| 268 | override fun getApplicableMethodNames(): List<String>? = listOf(METHOD_NAMES) |
| 269 | ``` |
| 270 | |
| 271 | ##### visitMethodCall |
| 272 | |
| 273 | This defines the callback that Lint will call when it encounters a call to an |
| 274 | applicable method. |
| 275 | |
| 276 | ```kotlin |
| 277 | override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {} |
| 278 | ``` |
| 279 | |
| 280 | #### Calls to specific class instantiations |
| 281 | |
| 282 | ##### getApplicableConstructorTypes |
| 283 | |
| 284 | ```kotlin |
| 285 | override fun getApplicableConstructorTypes(): List<String>? = listOf(CLASS_NAMES) |
| 286 | ``` |
| 287 | |
| 288 | ##### visitConstructor |
| 289 | |
| 290 | ```kotlin |
| 291 | override fun visitConstructor(context: JavaContext, node: UCallExpression, method: PsiMethod) {} |
| 292 | ``` |
| 293 | |
| 294 | #### Classes that extend given superclasses |
| 295 | |
| 296 | ##### getApplicableSuperClasses |
| 297 | |
| 298 | ```kotlin |
| 299 | override fun applicableSuperClasses(): List<String>? = listOf(CLASS_NAMES) |
| 300 | ``` |
| 301 | |
| 302 | ##### visitClass |
| 303 | |
| 304 | ```kotlin |
| 305 | override fun visitClass(context: JavaContext, declaration: UClass) {} |
| 306 | ``` |
| 307 | |
| 308 | #### Call graph support |
| 309 | |
| 310 | It is possible to perform analysis on the call graph of a project. However, this |
| 311 | is highly resource intensive since it generates a single call graph of the |
| 312 | entire project and should only be used for whole project analysis. To perform |
| 313 | this analysis you must enable call graph support by overriding the |
| 314 | `isCallGraphRequired` method and access the call graph with the |
| 315 | `analyzeCallGraph(context: Context, callGraph: CallGraphResult)` callback |
| 316 | method. |
| 317 | |
| 318 | For performing less resource intensive, on-the-fly analysis it is best to |
| 319 | recursively analyze method bodies. However, when doing this there should be a |
| 320 | depth limit on the exploration. If possible, lint should also not explore within |
| 321 | files that are currently not open in studio. |
| 322 | |
| 323 | ### Method call analysis |
| 324 | |
| 325 | #### resolve() |
| 326 | |
| 327 | Resolves into a `UCallExpression` or `UMethod` to perform analysis requiring the |
| 328 | method body or containing class. |
| 329 | |
| 330 | #### ReceiverType |
| 331 | |
| 332 | Each `UCallExpression` has a `receiverType` corresponding to the `PsiType` of |
| 333 | the receiver of the method call. |
| 334 | |
| 335 | ```kotlin |
| 336 | public abstract class LiveData<T> { |
| 337 | public void observe() {} |
| 338 | } |
| 339 | |
| 340 | public abstract class MutableLiveData<T> extends LiveData<T> {} |
| 341 | |
| 342 | MutableLiveData<String> liveData = new MutableLiveData<>(); |
| 343 | liveData.observe() // receiverType = PsiType<MutableLiveData> |
| 344 | ``` |
| 345 | |
| 346 | #### Kotlin named parameter mapping |
| 347 | |
| 348 | `JavaEvaluator`contains a helper method `computeArgumentMapping(call: |
| 349 | UCallExpression, method: PsiMethod)` that creates a mapping between method call |
| 350 | parameters and the corresponding resolved method arguments, accounting for |
| 351 | Kotlin named parameters. |
| 352 | |
| 353 | ```kotlin |
| 354 | override fun visitMethodCall(context: JavaContext, node: UCallExpression, |
| 355 | method: PsiMethod) { |
| 356 | val argMap: Map<UExpression, PsiParameter> = context.evaluator.computArgumentMapping(node, psiMethod) |
| 357 | } |
| 358 | ``` |
| 359 | |
| 360 | ### Testing |
| 361 | |
| 362 | Because the `LintDetectorTest` API does not have access to library classes and |
| 363 | methods, you must implement stubs for any necessary classes and include these as |
| 364 | additional files in your test cases. For example, if a lint check involves |
| 365 | Fragment's `getViewLifecycleOwner` and `onViewCreated` methods, then we must |
| 366 | create a stub for this: |
| 367 | |
| 368 | ``` |
| 369 | java(""" |
| 370 | package androidx.fragment.app; |
| 371 | |
| 372 | import androidx.lifecycle.LifecycleOwner; |
| 373 | |
| 374 | public class Fragment { |
| 375 | public LifecycleOwner getViewLifecycleOwner() {} |
| 376 | public void onViewCreated() {} |
| 377 | } |
| 378 | """) |
| 379 | ``` |
| 380 | |
| 381 | Since this class also depends on the `LifecycleOwner` class it is necessary to |
| 382 | create another stub for this. |
| 383 | |
| 384 | ## XML resource detector |
| 385 | |
| 386 | These are Lint rules that will apply to resource files including `anim`, |
| 387 | `layout`, `values`, etc. Lint rules being applied to resource files should |
| 388 | extend |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 389 | [`ResourceXmlDetector`](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-api/src/main/java/com/android/tools/lint/detector/api/ResourceXmlDetector.java). |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 390 | The `Detector` must define the issue it is going to detect, most commonly as a |
| 391 | static variable of the class. |
| 392 | |
| 393 | ```kotlin |
| 394 | companion object { |
| 395 | val ISSUE = Issue.create( |
| 396 | id = "TitleOfMyIssue", |
| 397 | briefDescription = "Short description of issue. This will be what the studio inspection menu shows", |
| 398 | explanation = """Here is where you define the reason that this lint rule exists in detail.""", |
| 399 | category = Category.CORRECTNESS, |
| 400 | severity = Severity.LEVEL, |
| 401 | implementation = Implementation( |
| 402 | MyIssueDetector::class.java, Scope.RESOURCE_FILE_SCOPE |
| 403 | ), |
| 404 | androidSpecific = true |
| 405 | ).addMoreInfo( |
| 406 | "https://linkToMoreInfo.com" |
| 407 | ) |
| 408 | } |
| 409 | ``` |
| 410 | |
| 411 | ### API surface |
| 412 | |
| 413 | The following methods can be overridden: |
| 414 | |
| 415 | ```kotlin |
| 416 | appliesTo(folderType: ResourceFolderType) |
| 417 | getApplicableElements() |
| 418 | visitElement(context: XmlContext, element: Element) |
| 419 | ``` |
| 420 | |
| 421 | #### appliesTo |
| 422 | |
| 423 | This determines the |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 424 | [ResourceFolderType](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:layoutlib-api/src/main/java/com/android/resources/ResourceFolderType.java) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 425 | that the check will run against. |
| 426 | |
| 427 | ```kotlin |
| 428 | override fun appliesTo(folderType: ResourceFolderType): Boolean { |
| 429 | return folderType == ResourceFolderType.TYPE |
| 430 | } |
| 431 | ``` |
| 432 | |
| 433 | #### getApplicableElements |
| 434 | |
| 435 | This defines the list of elements where Lint will call your visitElement |
| 436 | callback method when encountered. |
| 437 | |
| 438 | ```kotlin |
| 439 | override fun getApplicableElements(): Collection<String>? = Collections.singleton(ELEMENT) |
| 440 | ``` |
| 441 | |
| 442 | #### visitElement |
| 443 | |
| 444 | This defines the behavior when an applicable element is found. Here you normally |
| 445 | place the actions you want to take if a violation of the Lint check is found. |
| 446 | |
| 447 | ```kotlin |
| 448 | override fun visitElement(context: XmlContext, element: Element) { |
| AndroidX Core Team | cf94603 | 2022-02-11 15:52:08 -0800 | [diff] [blame^] | 449 | val lintFix = fix().replace() |
| 450 | .text(ELEMENT) |
| 451 | .with(REPLACEMENT TEXT) |
| 452 | .build() |
| 453 | |
| 454 | val incident = Incident(context) |
| 455 | .fix(lintFix) |
| 456 | .issue(ISSUE) |
| 457 | .location(context.getLocation(node)) |
| 458 | .message("My issue message") |
| 459 | .scope(context.getNameLocation(element)) |
| 460 | |
| 461 | context.report(incident) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 462 | } |
| 463 | ``` |
| 464 | |
| 465 | In this instance, the call to `report()` takes the definition of the issue, the |
| 466 | location of the element that has the issue, the message to display on the |
| 467 | element, as well as a quick fix. In this case we replace our element text with |
| 468 | some other text. |
| 469 | |
| AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 470 | [Example Detector Implementation](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:fragment/fragment-lint/src/main/java/androidx/fragment/lint/FragmentTagDetector.kt) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 471 | |
| 472 | ### Testing |
| 473 | |
| 474 | You need tests for two things. First, you must test that the API Lint version is |
| 475 | properly set. That is done with a simple `ApiLintVersionTest` class. It asserts |
| 476 | the api version code set earlier in the `IssueRegistry()` class. This test |
| 477 | intentionally fails in the IDE because different Lint API versions are used in |
| 478 | the studio and command line. |
| 479 | |
| 480 | Example `ApiLintVersionTest`: |
| 481 | |
| 482 | ```kotlin |
| 483 | class ApiLintVersionsTest { |
| 484 | |
| 485 | @Test |
| 486 | fun versionsCheck() { |
| 487 | val registry = MyLibraryIssueRegistry() |
| 488 | assertThat(registry.api).isEqualTo(CURRENT_API) |
| 489 | assertThat(registry.minApi).isEqualTo(3) |
| 490 | } |
| 491 | } |
| 492 | ``` |
| 493 | |
| 494 | Next, you must test the `Detector` class. The Tools team provides a |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 495 | [`LintDetectorTest`](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-tests/src/main/java/com/android/tools/lint/checks/infrastructure/LintDetectorTest.java) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 496 | class that should be extended. Override `getDetector()` to return an instance of |
| 497 | the `Detector` class: |
| 498 | |
| 499 | ```kotlin |
| 500 | override fun getDetector(): Detector = MyLibraryDetector() |
| 501 | ``` |
| 502 | |
| 503 | Override `getIssues()` to return the list of Detector Issues: |
| 504 | |
| 505 | ```kotlin |
| 506 | getIssues(): MutableList<Issue> = mutableListOf(MyLibraryDetector.ISSUE) |
| 507 | ``` |
| 508 | |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 509 | [`LintDetectorTest`](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-tests/src/main/java/com/android/tools/lint/checks/infrastructure/LintDetectorTest.java) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 510 | provides a `lint()` method that returns a |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 511 | [`TestLintTask`](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-tests/src/main/java/com/android/tools/lint/checks/infrastructure/TestLintTask.java). |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 512 | `TestLintTask` is a builder class for setting up lint tests. Call the `files()` |
| 513 | method and provide an `.xml` test file, along with a file stub. After completing |
| 514 | the set up, call `run()` which returns a |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 515 | [`TestLintResult`](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-tests/src/main/java/com/android/tools/lint/checks/infrastructure/TestLintResult.kt). |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 516 | `TestLintResult` provides methods for checking the outcome of the provided |
| 517 | `TestLintTask`. `ExpectClean()` means the output is expected to be clean because |
| 518 | the lint rule was followed. `Expect()` takes a string literal of the expected |
| 519 | output of the `TestLintTask` and compares the actual result to the input string. |
| 520 | If a quick fix was implemented, you can check that the fix is correct by calling |
| 521 | `checkFix()` and providing the expected output file stub. |
| 522 | |
| AndroidX Core Team | 408c27b | 2020-12-15 15:57:00 +0000 | [diff] [blame] | 523 | [TestExample](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:fragment/fragment-lint/src/test/java/androidx/fragment/lint/FragmentTagDetectorTest.kt) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 524 | |
| 525 | ## Android manifest detector |
| 526 | |
| 527 | Lint checks targeting `AndroidManifest.xml` files should implement the |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 528 | [XmlScanner](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-api/src/main/java/com/android/tools/lint/detector/api/XmlScanner.kt) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 529 | and define target scope in issues as `Scope.MANIFEST` |
| 530 | |
| 531 | ## Gradle detector |
| 532 | |
| 533 | Lint checks targeting Gradle configuration files should implement the |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 534 | [GradleScanner](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-api/src/main/java/com/android/tools/lint/detector/api/GradleScanner.kt) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 535 | and define target scope in issues as `Scope.GRADLE_SCOPE` |
| 536 | |
| 537 | ### API surface |
| 538 | |
| 539 | #### checkDslPropertyAssignment |
| 540 | |
| 541 | Analyzes each DSL property assignment, providing the property and value strings. |
| 542 | |
| 543 | ```kotlin |
| 544 | fun checkDslPropertyAssignment( |
| 545 | context: GradleContext, |
| 546 | property: String, |
| 547 | value: String, |
| 548 | parent: String, |
| 549 | parentParent: String?, |
| 550 | propertyCookie: Any, |
| 551 | valueCookie: Any, |
| 552 | statementCookie: Any |
| 553 | ) {} |
| 554 | ``` |
| 555 | |
| 556 | The property, value, and parent string parameters provided by this callback are |
| 557 | the literal values in the gradle file. Any string values in the Gradle file will |
| 558 | be quote enclosed in the value parameter. Any constant values cannot be resolved |
| 559 | to their values. |
| 560 | |
| 561 | The cookie parameters should be used for reporting Lint errors. To report an |
| 562 | issue on the value, use `context.getLocation(statementCookie)`. |
| 563 | |
| 564 | ## Enabling Lint for a library |
| 565 | |
| 566 | Once the Lint module is implemented we need to enable it for the desired |
| 567 | library. This can be done by adding a `lintPublish` rule to the `build.gradle` |
| 568 | of the library the Lint check should apply to. |
| 569 | |
| 570 | ``` |
| 571 | lintPublish(project(':mylibrary:mylibrary-lint')) |
| 572 | ``` |
| 573 | |
| 574 | This adds a `lint.jar` file into the `.aar` bundle of the desired library. |
| 575 | |
| 576 | Then we should add a `com.android.tools.lint.client.api.IssueRegistry` file in |
| 577 | `main > resources > META-INF > services`. The file should contain a single line |
| 578 | that has the `IssueRegistry` class name with the full path. This class can |
| 579 | contain more than one line if the module contains multiple registries. |
| 580 | |
| 581 | ``` |
| 582 | androidx.mylibrary.lint.MyLibraryIssueRegistry |
| 583 | ``` |
| 584 | |
| 585 | ## Advanced topics: |
| 586 | |
| 587 | ### Analyzing multiple different file types |
| 588 | |
| 589 | Sometimes it is necessary to implement multiple different scanners in a Lint |
| 590 | detector. For example, the |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 591 | [Unused Resource](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/UnusedResourceDetector.java) |
| AndroidX Core Team | e80aab7 | 2021-09-29 08:44:33 -0700 | [diff] [blame] | 592 | Lint check implements an XML and SourceCodeScanner in order to determine if |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 593 | resources defined in XML files are ever references in the Java/Kotlin source |
| 594 | code. |
| 595 | |
| 596 | #### File type iteration order |
| 597 | |
| 598 | The Lint system processes files in a predefined order: |
| 599 | |
| 600 | 1. Manifests |
| 601 | 1. Android XML Resources (alphabetical by folder type) |
| 602 | 1. Java & Kotlin |
| 603 | 1. Bytecode |
| 604 | 1. Gradle |
| 605 | |
| 606 | ### Multi-pass analysis |
| 607 | |
| 608 | It is often necessary to process the sources more than once. This can be done by |
| 609 | using `context.driver.requestRepeat(detector, scope)`. |
| 610 | |
| AndroidX Core Team | e31e959 | 2021-12-09 11:27:33 -0800 | [diff] [blame] | 611 | ## Helpful tips {#tips} |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 612 | |
| AndroidX Core Team | e31e959 | 2021-12-09 11:27:33 -0800 | [diff] [blame] | 613 | ### Useful classes/packages |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 614 | |
| AndroidX Core Team | e31e959 | 2021-12-09 11:27:33 -0800 | [diff] [blame] | 615 | [`SdkConstants`](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:common/src/main/java/com/android/SdkConstants.java) - |
| 616 | contains most of the canonical names for Android core library classes, as well |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 617 | as XML tag names. |
| 618 | |
| AndroidX Core Team | e31e959 | 2021-12-09 11:27:33 -0800 | [diff] [blame] | 619 | ### Updating bytecode and checksum in tests |
| 620 | |
| 621 | When updating a file that is used in a lint test, the following error may appear |
| 622 | when running tests: |
| 623 | |
| 624 | ``` |
| 625 | The checksum does not match for java/androidx/sample/deprecated/DeprecatedKotlinClass.kt; |
| 626 | expected 0x1af1856 but was 0x6692f601. |
| 627 | Has the source file been changed without updating the binaries? |
| 628 | Don't just update the checksum -- delete the binary file arguments and re-run the test first! |
| 629 | java.lang.AssertionError: The checksum does not match for java/androidx/sample/deprecated/DeprecatedKotlinClass.kt; |
| 630 | expected 0x1af1856 but was 0x6692f601. |
| 631 | Has the source file been changed without updating the binaries? |
| 632 | Don't just update the checksum -- delete the binary file arguments and re-run the test first! |
| 633 | at org.junit.Assert.fail(Assert.java:89) |
| 634 | ... |
| 635 | ``` |
| 636 | |
| 637 | Here are the steps to fix this: |
| 638 | |
| 639 | 1. Remove the arguments in `compiled()`: |
| 640 | |
| 641 | ``` |
| 642 | // Before |
| 643 | compiled( |
| 644 | "libs/ktlib.jar", |
| 645 | ktSample("androidx.sample.deprecated.DeprecatedKotlinClass"), |
| 646 | 0x6692f601, |
| 647 | """ |
| 648 | META-INF/main.kotlin_module: |
| 649 | H4sIAAAAAAAAAGNgYGBmYGBgBGJWKM2gxKDFAABNj30wGAAAAA== |
| 650 | """, |
| 651 | """ |
| 652 | androidx/sample/deprecated/DeprecatedKotlinClass.class: |
| 653 | H4sIAAAAAAAAAJVSy27TQBQ9YydxcQNNH5SUZyivlkWSpuxAiFIEighBCiit |
| 654 | // rest of bytecode |
| 655 | """ |
| 656 | ) |
| 657 | |
| 658 | // After |
| 659 | compiled( |
| 660 | "libs/ktlib.jar", |
| 661 | ktSample("androidx.sample.deprecated.DeprecatedKotlinClass"), |
| 662 | ) |
| 663 | ``` |
| 664 | |
| 665 | 2. Set `$LINT_TEST_KOTLINC` to the location of `kotlinc` if you haven't |
| 666 | already, and add it to the test run configuration's environment variables. |
| 667 | |
| 668 | Note: The location of `kotlinc` can vary; use your system's file finder to |
| 669 | determine the exact location. For gLinux, search under |
| 670 | `~/.local/share/JetBrains`. For Mac, search under `<your androidx checkout |
| 671 | root>/frameworks/support/studio` |
| 672 | |
| 673 | If it's not set (or set incorrectly), this error message appears when |
| 674 | running tests: |
| 675 | |
| 676 | ``` |
| 677 | Couldn't find kotlinc to update test file java/androidx/sample/deprecated/DeprecatedKotlinClass.kt with. |
| 678 | Point to it with $LINT_TEST_KOTLINC |
| 679 | ``` |
| 680 | |
| 681 | 3. Run the test, which will output the new bytecode and checksum: |
| 682 | |
| 683 | ``` |
| 684 | Update the test source declaration for java/androidx/sample/deprecated/DeprecatedKotlinClass.kt with this list of encodings: |
| 685 | |
| 686 | Kotlin: |
| 687 | compiled( |
| 688 | "libs/ktlib.jar", |
| 689 | kotlin( |
| 690 | """ |
| 691 | package java.androidx.sample.deprecated |
| 692 | |
| 693 | @Deprecated( |
| 694 | // (rest of inlined sample file) |
| 695 | """ |
| 696 | ).indented(), |
| 697 | 0x5ba03e2d, |
| 698 | """ |
| 699 | META-INF/main.kotlin_module: |
| 700 | H4sIAAAAAAAAAGNgYGBmYGBgBGJWKM2gxKDFAABNj30wGAAAAA== |
| 701 | // rest of bytecode |
| 702 | """, |
| 703 | """ |
| 704 | java/androidx/sample/deprecated/DeprecatedKotlinClass.class: |
| 705 | """ |
| 706 | ) |
| 707 | ``` |
| 708 | |
| 709 | Note: the generated replacement code will inline the specified sample file (in |
| 710 | our case, `ktSample("androidx.sample.deprecated.DeprecatedKotlinClass")`). |
| 711 | Replace the inlined code with the sample declaration. |
| 712 | |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 713 | ## Helpful links |
| 714 | |
| AndroidX Core Team | 8a082f9 | 2021-07-01 11:46:10 -0700 | [diff] [blame] | 715 | [Writing Custom Lint Rules](https://googlesamples.github.io/android-custom-lint-rules/) |
| 716 | |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 717 | [Studio Lint Rules](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 718 | |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 719 | [Lint Detectors and Scanners Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-master-dev:lint/libs/lint-api/src/main/java/com/android/tools/lint/detector/api/) |
| Jeremy Woods | feffecaf | 2020-10-15 12:08:38 -0700 | [diff] [blame] | 720 | |
| 721 | [Creating Custom Link Checks (external)](https://twitter.com/alexjlockwood/status/1176675045281693696) |
| 722 | |
| 723 | [Android Custom Lint Rules by Tor](https://github.com/googlesamples/android-custom-lint-rules) |
| 724 | |
| 725 | [Public lint-dev Google Group](https://groups.google.com/forum/#!forum/lint-dev) |
| 726 | |
| 727 | [In-depth Lint Video Presentation by Tor](https://www.youtube.com/watch?v=p8yX5-lPS6o) |
| 728 | (partially out-dated) |
| 729 | ([Slides](https://resources.jetbrains.com/storage/products/kotlinconf2017/slides/KotlinConf+Lint+Slides.pdf)) |
| 730 | |
| 731 | [ADS 19 Presentation by Alan & Rahul](https://www.youtube.com/watch?v=jCmJWOkjbM0) |
| 732 | |
| AndroidX Core Team | 2e416b2 | 2020-12-03 22:58:07 +0000 | [diff] [blame] | 733 | [META-INF vs Manifest](https://groups.google.com/forum/#!msg/lint-dev/z3NYazgEIFQ/hbXDMYp5AwAJ) |