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