# Lint Android's [**lint**](https://developer.android.com/tools/help/lint.html) is a static analysis tool that Chromium uses to catch possible issues in Java code. This is a list of [**checks**](http://tools.android.com/tips/lint-checks) that you might encounter. [TOC] ## How Chromium uses lint Chromium only runs lint on apk or bundle targets that explicitly set `enable_lint = true`. Some example targets that have this set are: - `//chrome/android:monochrome_public_bundle` - `//android_webview/support_library/boundary_interfaces:boundary_interface_example_apk` - `//remoting/android:remoting_apk` ## My code has a lint error If lint reports an issue in your code, there are several possible remedies. In descending order of preference: ### Fix it While this isn't always the right response, fixing the lint error or warning should be the default. ### Suppress it locally Java provides an annotation, [`@SuppressWarnings`](https://developer.android.com/reference/java/lang/SuppressWarnings), that tells lint to ignore the annotated element. It can be used on classes, constructors, methods, parameters, fields, or local variables, though usage in Chromium is typically limited to the first three. You do not need to import it since it is in the `java.lang` package. Like many suppression annotations, `@SuppressWarnings` takes a value that tells **lint** what to ignore. It can be a single `String`: ```java @SuppressWarnings("NewApi") public void foo() { a.methodThatRequiresHighSdkLevel(); } ``` It can also be a list of `String`s: ```java @SuppressWarnings({ "NewApi", "UseSparseArrays" }) public Map bar() { Map shouldBeASparseArray = new HashMap(); another.methodThatRequiresHighSdkLevel(shouldBeASparseArray); return shouldBeASparseArray; } ``` For resource xml files you can use `tools:ignore`: ```xml @color/unused ``` The examples above are the recommended ways of suppressing lint warnings. ### Suppress it in a `lint-suppressions.xml` file **lint** can be given a per-target XML configuration file containing warnings or errors that should be ignored. Each target defines its own configuration file via the `lint_suppressions_file` gn variable. It is usually defined near its `enable_lint` gn variable. These suppressions files should only be used for temporarily ignoring warnings that are too hard (or not possible) to suppress locally, and permanently ignoring warnings only for this target. To permanently ignore a warning for all targets, add the warning to the `_DISABLED_ALWAYS` list in [build/android/gyp/lint.py](https://source.chromium.org/chromium/chromium/src/+/main:build/android/gyp/lint.py). Disabling globally makes lint a bit faster. The exception to the above rule is for warnings that affect multiple languages. Feel free to suppress those in lint-suppressions.xml files since it is not practical to suppress them in each language file and it is a lot of extra bloat to list out every language for every violation in lint-baseline.xml files. Here is an example of how to structure a suppressions XML file: ```xml ``` ## What are `lint-baseline.xml` files for? Baseline files are to help us introduce new lint warnings and errors without blocking on fixing all our existing code that violate these new errors. Since they are generated files, they should **not** be used to suppress lint warnings. One of the approaches above should be used instead. Eventually all the errors in baseline files should be either fixed or ignored permanently. Most devs do not need to update baseline files and should not need the script below. Occasionally when making large build configuration changes it may be necessary to update baseline files (e.g. increasing the min_sdk_version). Baseline files are defined via the `lint_baseline_file` gn variable. It is usually defined near a target's `enable_lint` gn variable. To regenerate all baseline files, run: ``` $ third_party/android_build_tools/lint/rebuild_baselines.py ``` This script will also update baseline files in downstream //clank if needed. Since downstream and upstream use separate lint binaries, it is usually safe to simply land the update CLs in any order.