blob: 37f35502e5f3991c445989018ba546c89aeb6cfe [file] [log] [blame] [view]
jbudorick8d4d9742016-04-26 04:47:101# Lint
2
3Android's [**lint**](http://developer.android.com/tools/help/lint.html) is a static
4analysis tool that Chromium uses to catch possible issues in Java code.
5
6[TOC]
7
8## How Chromium uses lint
9
10Chromium runs lint on a per-target basis for all targets using any of the
11following templates if they are marked as Chromium code (i.e.,
12`chromium_code = true`):
13
14 - `android_apk`
15 - `android_library`
16 - `instrumentation_test_apk`
17 - `unittest_apk`
18
19Chromium also runs lint on a per-target basis for all targets using any of the
20following templates if they are marked as Chromium code and they support
21Android (i.e., `supports_android = true`):
22
23 - `java_library`
24
25This is implemented in the
26[`android_lint`](https://code.google.com/p/chromium/codesearch#chromium/src/build/config/android/internal_rules.gni&q=android_lint%20file:internal_rules%5C.gni)
27gn template.
28
29## My code has a lint error
30
31If lint reports an issue in your code, there are several possible remedies.
32In descending order of preference:
33
34### Fix it
35
36While this isn't always the right response, fixing the lint error or warning
37should be the default.
38
39### Suppress it in code
40
41Android provides an annotation,
42[`@SuppressLint`](http://developer.android.com/reference/android/annotation/SuppressLint.html),
43that tells lint to ignore the annotated element. It can be used on classes,
44constructors, methods, parameters, fields, or local variables, though usage
45in Chromium is typically limited to the first three.
46
47Like many suppression annotations, `@SuppressLint` takes a value that tells **lint**
48what to ignore. It can be a single `String`:
49
50```java
51@SuppressLint("NewApi")
52public void foo() {
53 a.methodThatRequiresHighSdkLevel();
54}
55```
56
57It can also be a list of `String`s:
58
59```java
60@SuppressLint({
61 "NewApi",
62 "UseSparseArrays"
63 })
64public Map<Integer, FakeObject> bar() {
65 Map<Integer, FakeObject> shouldBeASparseArray = new HashMap<Integer, FakeObject>();
66 another.methodThatRequiresHighSdkLevel(shouldBeASparseArray);
67 return shouldBeASparseArray;
68}
69```
70
71This is the preferred way of suppressing warnings in a limited scope.
72
73### Suppress it in the suppressions XML file
74
75**lint** can be given an XML configuration containing warnings or errors that
76should be ignored. Chromium's lint suppression XML file can be found in
77[`build/android/lint/suppressions.xml`](https://chromium.googlesource.com/chromium/src/+/master/build/android/lint/suppressions.xml).
78It can be updated to suppress current warnings by running:
79
80```bash
81$ python build/android/lint/suppress.py <result.xml file>
82```
83
84e.g., to suppress lint errors found in `media_java`:
85
86```bash
87$ python build/android/lint/suppress.py out/Debug/gen/media/base/android/media_java__lint/result.xml
88```
89
90**This mechanism should only be used for disabling warnings across the entire code base; class-specific lint warnings should be disabled inline.**
91