blob: 67e2f8bf3eacdcde933ca2ea040a1c4b05c1a915 [file] [log] [blame] [view]
jbudorick8d4d9742016-04-26 04:47:101# Lint
2
Peter Wen24b16742020-11-12 22:39:273Android's [**lint**](https://developer.android.com/tools/help/lint.html) is a
4static analysis tool that Chromium uses to catch possible issues in Java code.
5
6This is a list of [**checks**](http://tools.android.com/tips/lint-checks) that
7you might encounter.
jbudorick8d4d9742016-04-26 04:47:108
9[TOC]
10
11## How Chromium uses lint
12
Peter Wen24b16742020-11-12 22:39:2713Chromium only runs lint on apk or bundle targets that explicitly set
14`enable_lint = true`. Some example targets that have this set are:
jbudorick8d4d9742016-04-26 04:47:1015
Peter Wen24b16742020-11-12 22:39:2716 - `//chrome/android:monochrome_public_bundle`
17 - `//android_webview/support_library/boundary_interfaces:boundary_interface_example_apk`
18 - `//remoting/android:remoting_apk`
jbudorick8d4d9742016-04-26 04:47:1019
20## My code has a lint error
21
22If lint reports an issue in your code, there are several possible remedies.
23In descending order of preference:
24
25### Fix it
26
27While this isn't always the right response, fixing the lint error or warning
28should be the default.
29
Peter Wen24b16742020-11-12 22:39:2730### Suppress it locally
jbudorick8d4d9742016-04-26 04:47:1031
Peter Wen24b16742020-11-12 22:39:2732Java provides an annotation,
33[`@SuppressWarnings`](https://developer.android.com/reference/java/lang/SuppressWarnings),
jbudorick8d4d9742016-04-26 04:47:1034that tells lint to ignore the annotated element. It can be used on classes,
Peter Wen24b16742020-11-12 22:39:2735constructors, methods, parameters, fields, or local variables, though usage in
36Chromium is typically limited to the first three. You do not need to import it
37since it is in the `java.lang` package.
jbudorick8d4d9742016-04-26 04:47:1038
Peter Wen24b16742020-11-12 22:39:2739Like many suppression annotations, `@SuppressWarnings` takes a value that tells
40**lint** what to ignore. It can be a single `String`:
jbudorick8d4d9742016-04-26 04:47:1041
42```java
Peter Wen24b16742020-11-12 22:39:2743@SuppressWarnings("NewApi")
jbudorick8d4d9742016-04-26 04:47:1044public void foo() {
45 a.methodThatRequiresHighSdkLevel();
46}
47```
48
49It can also be a list of `String`s:
50
51```java
Peter Wen24b16742020-11-12 22:39:2752@SuppressWarnings({
jbudorick8d4d9742016-04-26 04:47:1053 "NewApi",
54 "UseSparseArrays"
55 })
56public Map<Integer, FakeObject> bar() {
57 Map<Integer, FakeObject> shouldBeASparseArray = new HashMap<Integer, FakeObject>();
58 another.methodThatRequiresHighSdkLevel(shouldBeASparseArray);
59 return shouldBeASparseArray;
60}
61```
62
Peter Wen24b16742020-11-12 22:39:2763For resource xml files you can use `tools:ignore`:
jbudorick8d4d9742016-04-26 04:47:1064
Peter Wen24b16742020-11-12 22:39:2765```xml
66<?xml version="1.0" encoding="utf-8"?>
67<resources xmlns:tools="http://schemas.android.com/tools">
68 <!-- TODO(crbug/###): remove tools:ignore once these colors are used -->
69 <color name="hi" tools:ignore="NewApi,UnusedResources">@color/unused</color>
70</resources>
jbudorick8d4d9742016-04-26 04:47:1071```
72
Peter Wen24b16742020-11-12 22:39:2773The examples above are the recommended ways of suppressing lint warnings.
jbudorick8d4d9742016-04-26 04:47:1074
Peter Wen24b16742020-11-12 22:39:2775### Suppress it in a `lint-suppressions.xml` file
76
77**lint** can be given a per-target XML configuration file containing warnings or
78errors that should be ignored. Each target defines its own configuration file
79via the `lint_suppressions_file` gn variable. It is usually defined near its
80`enable_lint` gn variable.
81
82These suppressions files should only be used for temporarily ignoring warnings
83that are too hard (or not possible) to suppress locally, and permanently
84ignoring warnings only for this target. To permanently ignore a warning for all
85targets, add the warning to the `_DISABLED_ALWAYS` list in
John Palmer58f5d9f2021-05-19 18:58:2986[build/android/gyp/lint.py](https://source.chromium.org/chromium/chromium/src/+/main:build/android/gyp/lint.py).
Peter Wen24b16742020-11-12 22:39:2787Disabling globally makes lint a bit faster.
88
Peter Wen397a12c22020-11-21 01:52:0889The exception to the above rule is for warnings that affect multiple languages.
90Feel free to suppress those in lint-suppressions.xml files since it is not
91practical to suppress them in each language file and it is a lot of extra bloat
92to list out every language for every violation in lint-baseline.xml files.
93
Peter Wen24b16742020-11-12 22:39:2794Here is an example of how to structure a suppressions XML file:
95
96```xml
97<?xml version="1.0" encoding="utf-8" ?>
98<lint>
99 <!-- Chrome is a system app. -->
100 <issue id="ProtectedPermissions" severity="ignore"/>
101 <issue id="UnusedResources">
102 <!-- 1 raw resources are accessed by URL in various places. -->
103 <ignore regexp="gen/remoting/android/.*/res/raw/credits.*"/>
104 <!-- TODO(crbug.com/###): Remove the following line. -->
105 <ignore regexp="The resource `R.string.soon_to_be_used` appears to be unused"/>
106 </issue>
107</lint>
jbudorick8d4d9742016-04-26 04:47:10108```
109
Peter Wen24b16742020-11-12 22:39:27110## What are `lint-baseline.xml` files for?
jbudorick8d4d9742016-04-26 04:47:10111
Peter Wen24b16742020-11-12 22:39:27112Baseline files are to help us introduce new lint warnings and errors without
113blocking on fixing all our existing code that violate these new errors. Since
114they are generated files, they should **not** be used to suppress lint warnings.
115One of the approaches above should be used instead. Eventually all the errors in
116baseline files should be either fixed or ignored permanently.
117
118The following are some common scenarios where you may need to update baseline
119files.
120
121### I updated `cmdline-tools` and now there are tons of new errors!
122
123This happens every time lint is updated, since lint is provided by
124`cmdline-tools`.
125
126Baseline files are defined via the `lint_baseline_file` gn variable. It is
127usually defined near a target's `enable_lint` gn variable. To regenerate the
128baseline file, delete it and re-run the lint target. The command will fail, but
129the baseline file will have been generated.
130
131This may need to be repeated for all targets that have set `enable_lint = true`,
132including downstream targets. Downstream baseline files should be updated and
133first to avoid build breakages. Each target has its own `lint_baseline_file`
134defined and so all these files can be removed and regenerated as needed.
135
136### I updated `library X` and now there are tons of new errors!
137
138This is usually because `library X`'s aar contains custom lint checks and/or
139custom annotation definition. Follow the same procedure as updates to
140`cmdline-tools`.