• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Android Framework Lint Checker
2
3Custom lint checks written here are going to be executed for modules that opt in to those (e.g. any
4`services.XXX` module) and results will be automatically reported on CLs on gerrit.
5
6## How to add new lint checks
7
81. Write your detector with its issues and put it into
9   `checks/src/main/java/com/google/android/lint`.
102. Add your detector's issues into `AndroidFrameworkIssueRegistry`'s `issues` field.
113. Write unit tests for your detector in one file and put it into
12   `checks/test/java/com/google/android/lint`.
134. Done! Your lint checks should be applied in lint report builds for modules that include
14   `AndroidFrameworkLintChecker`.
15
16## How to run lint against your module
17
181. Add the following `lint` attribute to the module definition, e.g. `services.autofill`:
19```
20java_library_static {
21    name: "services.autofill",
22    ...
23    lint: {
24        extra_check_modules: ["AndroidFrameworkLintChecker"],
25    },
26}
27```
282. Run the following command to verify that the report is being correctly built:
29```
30m out/soong/.intermediates/frameworks/base/services/autofill/services.autofill/android_common/lint/lint-report.html
31```
32   (Lint report can be found in the same path, i.e. `out/../lint-report.html`)
33
343. Now lint issues should appear on gerrit!
35
36**Notes:**
37
38- Lint report will not be produced if you just build the module, i.e. `m services.autofill` will not
39  build the lint report.
40- If you want to build lint reports for more than 1 module and they include a common module in their
41  `defaults` field, e.g. `platform_service_defaults`, you can add the `lint` property to that common
42  module instead of adding it in every module.
43- If you want to run a single lint type, use the `ANDROID_LINT_CHECK`
44  environment variable with the id of the lint. For example:
45  `ANDROID_LINT_CHECK=UnusedTokenOfOriginalCallingIdentity m out/[...]/lint-report.html`
46
47## Create or update a baseline
48
49Baseline files can be used to silence known errors (and warnings) that are deemed to be safe. When
50there is a lint-baseline.xml file in the root folder of the java library, soong will
51automatically use it. You can override the file using lint properties too.
52
53```
54java_library {
55    lint: {
56        baseline_filename: "my-baseline.xml", // default: lint-baseline.xml;
57    }
58}
59```
60
61When using soong to create a lint report (as described above), it also creates a reference
62baseline file. This contains all lint errors and warnings that were found. So the next time
63you run lint, if you use this baseline file, there should be 0 findings.
64
65After the previous invocation, you can find the baseline here:
66
67```
68out/soong/.intermediates/frameworks/base/services/autofill/services.autofill/android_common/lint/lint-baseline.xml
69```
70
71As noted above, this baseline file contains warnings too, which might be undesirable. For example,
72CI tools might surface these warnings in code reviews. In order to create this file without
73warnings, we need to pass another flag to lint: `--nowarn`. The easiest way to do this is to
74locally change the soong code in
75[lint.go](http://cs/aosp-master/build/soong/java/lint.go;l=451;rcl=2e778d5bc4a8d1d77b4f4a3029a4a254ad57db75)
76adding `cmd.Flag("--nowarn")` and running lint again.
77
78## Documentation
79
80- [Android Lint Docs](https://googlesamples.github.io/android-custom-lint-rules/)
81- [Android Lint source files](https://source.corp.google.com/studio-main/tools/base/lint/libs/lint-api/src/main/java/com/android/tools/lint/)
82- [PSI source files](https://github.com/JetBrains/intellij-community/tree/master/java/java-psi-api/src/com/intellij/psi)
83- [UAST source files](https://upsource.jetbrains.com/idea-ce/structure/idea-ce-7b9b8cc138bbd90aec26433f82cd2c6838694003/uast/uast-common/src/org/jetbrains/uast)
84- [IntelliJ plugin for viewing PSI tree of files](https://plugins.jetbrains.com/plugin/227-psiviewer)
85