| 1 | # Android Lint Checks for AOSP |
| 2 | |
| 3 | Custom Android Lint checks are written here to be executed against java modules |
| 4 | in AOSP. These checks are broken down into two subdirectories: |
| 5 | |
| 6 | 1. [Global Checks](#android-global-lint-checker) |
| 7 | 2. [Framework Checks](#android-framework-lint-checker) |
| 8 | |
| 9 | # [Android Global Lint Checker](/global) |
| 10 | Checks written here are executed for the entire tree. The `AndroidGlobalLintChecker` |
| 11 | build target produces a jar file that is included in the overall build output |
| 12 | (`AndroidGlobalLintChecker.jar`). This file is then downloaded as a prebuilt under the |
| 13 | `prebuilts/cmdline-tools` subproject, and included by soong with all invocations of lint. |
| 14 | |
| 15 | ## How to add new global lint checks |
| 16 | 1. Write your detector with its issues and put it into |
| 17 | `global/checks/src/main/java/com/google/android/lint`. |
| 18 | 2. Add your detector's issues into `AndroidGlobalIssueRegistry`'s `issues` |
| 19 | field. |
| 20 | 3. Write unit tests for your detector in one file and put it into |
| 21 | `global/checks/test/java/com/google/android/lint`. |
| 22 | 4. Have your change reviewed and merged. Once your change is merged, |
| 23 | obtain a build number from a successful build that includes your change. |
| 24 | 5. Run `prebuilts/cmdline-tools/update-android-global-lint-checker.sh |
| 25 | <build_number>`. The script will create a commit that you can upload for |
| 26 | approval to the `prebuilts/cmdline-tools` subproject. |
| 27 | 6. Done! Your lint check should be applied in lint report builds across the |
| 28 | entire tree! |
| 29 | |
| 30 | # [Android Framework Lint Checker](/framework) |
| 31 | |
| 32 | Checks written here are going to be executed for modules that opt in to those (e.g. any |
| 33 | `services.XXX` module) and results will be automatically reported on CLs on gerrit. |
| 34 | |
| 35 | ## How to add new framework lint checks |
| 36 | |
| 37 | 1. Write your detector with its issues and put it into |
| 38 | `framework/checks/src/main/java/com/google/android/lint`. |
| 39 | 2. Add your detector's issues into `AndroidFrameworkIssueRegistry`'s `issues` field. |
| 40 | 3. Write unit tests for your detector in one file and put it into |
| 41 | `framework/checks/test/java/com/google/android/lint`. |
| 42 | 4. Done! Your lint checks should be applied in lint report builds for modules that include |
| 43 | `AndroidFrameworkLintChecker`. |
| 44 | |
| 45 | ## How to run lint against your module |
| 46 | |
| 47 | 1. Add the following `lint` attribute to the module definition, e.g. `services.autofill`: |
| 48 | ``` |
| 49 | java_library_static { |
| 50 | name: "services.autofill", |
| 51 | ... |
| 52 | lint: { |
| 53 | extra_check_modules: ["AndroidFrameworkLintChecker"], |
| 54 | }, |
| 55 | } |
| 56 | ``` |
| 57 | 2. Run the following command to verify that the report is being correctly built: |
| 58 | ``` |
| 59 | m out/soong/.intermediates/frameworks/base/services/autofill/services.autofill/android_common/lint/lint-report.html |
| 60 | ``` |
| 61 | (Lint report can be found in the same path, i.e. `out/../lint-report.html`) |
| 62 | |
| 63 | 3. Now lint issues should appear on gerrit! |
| 64 | |
| 65 | **Notes:** |
| 66 | |
| 67 | - Lint report will not be produced if you just build the module, i.e. `m services.autofill` will not |
| 68 | build the lint report. |
| 69 | - If you want to build lint reports for more than 1 module and they include a common module in their |
| 70 | `defaults` field, e.g. `platform_service_defaults`, you can add the `lint` property to that common |
| 71 | module instead of adding it in every module. |
| 72 | - If you want to run a single lint type, use the `ANDROID_LINT_CHECK` |
| 73 | environment variable with the id of the lint. For example: |
| 74 | `ANDROID_LINT_CHECK=UnusedTokenOfOriginalCallingIdentity m out/[...]/lint-report.html` |
| 75 | |
| 76 | # How to apply automatic fixes suggested by lint |
| 77 | |
| 78 | See [lint_fix](fix/README.md) |
| 79 | |
| 80 | # Create or update a baseline |
| 81 | |
| 82 | Baseline files can be used to silence known errors (and warnings) that are deemed to be safe. When |
| 83 | there is a lint-baseline.xml file in the root folder of the java library, soong will |
| 84 | automatically use it. You can override the file using lint properties too. |
| 85 | |
| 86 | ``` |
| 87 | java_library { |
| 88 | lint: { |
| 89 | baseline_filename: "my-baseline.xml", // default: lint-baseline.xml; |
| 90 | } |
| 91 | } |
| 92 | ``` |
| 93 | |
| 94 | When using soong to create a lint report (as described above), it also creates a reference |
| 95 | baseline file. This contains all lint errors and warnings that were found. So the next time |
| 96 | you run lint, if you use this baseline file, there should be 0 findings. |
| 97 | |
| 98 | After the previous invocation, you can find the baseline here: |
| 99 | |
| 100 | ``` |
| 101 | out/soong/.intermediates/frameworks/base/services/autofill/services.autofill/android_common/lint/lint-baseline.xml |
| 102 | ``` |
| 103 | |
| 104 | As noted above, this baseline file contains warnings too, which might be undesirable. For example, |
| 105 | CI tools might surface these warnings in code reviews. In order to create this file without |
| 106 | warnings, we need to pass another flag to lint: `--nowarn`. The easiest way to do this is to |
| 107 | locally change the soong code in |
| 108 | [lint.go](http://cs/aosp-master/build/soong/java/lint.go;l=451;rcl=2e778d5bc4a8d1d77b4f4a3029a4a254ad57db75) |
| 109 | adding `cmd.Flag("--nowarn")` and running lint again. |
| 110 | |
| 111 | # Documentation |
| 112 | |
| 113 | - [Android Lint Docs](https://googlesamples.github.io/android-custom-lint-rules/) |
| 114 | - [Lint Check Unit Testing](https://googlesamples.github.io/android-custom-lint-rules/api-guide/unit-testing.md.html) |
| 115 | - [Android Lint source files](https://source.corp.google.com/studio-main/tools/base/lint/libs/lint-api/src/main/java/com/android/tools/lint/) |
| 116 | - [PSI source files](https://github.com/JetBrains/intellij-community/tree/master/java/java-psi-api/src/com/intellij/psi) |
| 117 | - [UAST source files](https://upsource.jetbrains.com/idea-ce/structure/idea-ce-7b9b8cc138bbd90aec26433f82cd2c6838694003/uast/uast-common/src/org/jetbrains/uast) |
| 118 | - [IntelliJ plugin for viewing PSI tree of files](https://plugins.jetbrains.com/plugin/227-psiviewer) |