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