jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 1 | # Lint |
| 2 | |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 3 | Android's [**lint**](https://developer.android.com/tools/help/lint.html) is a |
| 4 | static analysis tool that Chromium uses to catch possible issues in Java code. |
| 5 | |
| 6 | This is a list of [**checks**](http://tools.android.com/tips/lint-checks) that |
| 7 | you might encounter. |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 8 | |
| 9 | [TOC] |
| 10 | |
| 11 | ## How Chromium uses lint |
| 12 | |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 13 | Chromium only runs lint on apk or bundle targets that explicitly set |
| 14 | `enable_lint = true`. Some example targets that have this set are: |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 15 | |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 16 | - `//chrome/android:monochrome_public_bundle` |
| 17 | - `//android_webview/support_library/boundary_interfaces:boundary_interface_example_apk` |
| 18 | - `//remoting/android:remoting_apk` |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 19 | |
| 20 | ## My code has a lint error |
| 21 | |
| 22 | If lint reports an issue in your code, there are several possible remedies. |
| 23 | In descending order of preference: |
| 24 | |
| 25 | ### Fix it |
| 26 | |
| 27 | While this isn't always the right response, fixing the lint error or warning |
| 28 | should be the default. |
| 29 | |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 30 | ### Suppress it locally |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 31 | |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 32 | Java provides an annotation, |
| 33 | [`@SuppressWarnings`](https://developer.android.com/reference/java/lang/SuppressWarnings), |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 34 | that tells lint to ignore the annotated element. It can be used on classes, |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 35 | constructors, methods, parameters, fields, or local variables, though usage in |
| 36 | Chromium is typically limited to the first three. You do not need to import it |
| 37 | since it is in the `java.lang` package. |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 38 | |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 39 | Like many suppression annotations, `@SuppressWarnings` takes a value that tells |
| 40 | **lint** what to ignore. It can be a single `String`: |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 41 | |
| 42 | ```java |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 43 | @SuppressWarnings("NewApi") |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 44 | public void foo() { |
| 45 | a.methodThatRequiresHighSdkLevel(); |
| 46 | } |
| 47 | ``` |
| 48 | |
| 49 | It can also be a list of `String`s: |
| 50 | |
| 51 | ```java |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 52 | @SuppressWarnings({ |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 53 | "NewApi", |
| 54 | "UseSparseArrays" |
| 55 | }) |
| 56 | public Map<Integer, FakeObject> bar() { |
| 57 | Map<Integer, FakeObject> shouldBeASparseArray = new HashMap<Integer, FakeObject>(); |
| 58 | another.methodThatRequiresHighSdkLevel(shouldBeASparseArray); |
| 59 | return shouldBeASparseArray; |
| 60 | } |
| 61 | ``` |
| 62 | |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 63 | For resource xml files you can use `tools:ignore`: |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 64 | |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 65 | ```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> |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 71 | ``` |
| 72 | |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 73 | The examples above are the recommended ways of suppressing lint warnings. |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 74 | |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 75 | ### Suppress it in a `lint-suppressions.xml` file |
| 76 | |
| 77 | **lint** can be given a per-target XML configuration file containing warnings or |
| 78 | errors that should be ignored. Each target defines its own configuration file |
| 79 | via the `lint_suppressions_file` gn variable. It is usually defined near its |
| 80 | `enable_lint` gn variable. |
| 81 | |
| 82 | These suppressions files should only be used for temporarily ignoring warnings |
| 83 | that are too hard (or not possible) to suppress locally, and permanently |
| 84 | ignoring warnings only for this target. To permanently ignore a warning for all |
| 85 | targets, add the warning to the `_DISABLED_ALWAYS` list in |
John Palmer | 58f5d9f | 2021-05-19 18:58:29 | [diff] [blame] | 86 | [build/android/gyp/lint.py](https://source.chromium.org/chromium/chromium/src/+/main:build/android/gyp/lint.py). |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 87 | Disabling globally makes lint a bit faster. |
| 88 | |
Peter Wen | 397a12c2 | 2020-11-21 01:52:08 | [diff] [blame] | 89 | The exception to the above rule is for warnings that affect multiple languages. |
| 90 | Feel free to suppress those in lint-suppressions.xml files since it is not |
| 91 | practical to suppress them in each language file and it is a lot of extra bloat |
| 92 | to list out every language for every violation in lint-baseline.xml files. |
| 93 | |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 94 | Here 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> |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 108 | ``` |
| 109 | |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 110 | ## What are `lint-baseline.xml` files for? |
jbudorick | 8d4d974 | 2016-04-26 04:47:10 | [diff] [blame] | 111 | |
Peter Wen | 24b1674 | 2020-11-12 22:39:27 | [diff] [blame] | 112 | Baseline files are to help us introduce new lint warnings and errors without |
| 113 | blocking on fixing all our existing code that violate these new errors. Since |
| 114 | they are generated files, they should **not** be used to suppress lint warnings. |
| 115 | One of the approaches above should be used instead. Eventually all the errors in |
| 116 | baseline files should be either fixed or ignored permanently. |
| 117 | |
| 118 | The following are some common scenarios where you may need to update baseline |
| 119 | files. |
| 120 | |
| 121 | ### I updated `cmdline-tools` and now there are tons of new errors! |
| 122 | |
| 123 | This happens every time lint is updated, since lint is provided by |
| 124 | `cmdline-tools`. |
| 125 | |
| 126 | Baseline files are defined via the `lint_baseline_file` gn variable. It is |
| 127 | usually defined near a target's `enable_lint` gn variable. To regenerate the |
| 128 | baseline file, delete it and re-run the lint target. The command will fail, but |
| 129 | the baseline file will have been generated. |
| 130 | |
| 131 | This may need to be repeated for all targets that have set `enable_lint = true`, |
| 132 | including downstream targets. Downstream baseline files should be updated and |
| 133 | first to avoid build breakages. Each target has its own `lint_baseline_file` |
| 134 | defined 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 | |
| 138 | This is usually because `library X`'s aar contains custom lint checks and/or |
| 139 | custom annotation definition. Follow the same procedure as updates to |
| 140 | `cmdline-tools`. |