@@ -2,25 +2,82 @@ Custom Lint Rules
2
2
=================
3
3
4
4
The [ Android ` lint ` tool] ( http://developer.android.com/tools/help/lint.html ) is a static code
5
- analysis tool that checks your Android project source files for potential bugs and optimization
6
- improvements for correctness, security, performance, usability, accessibility, and
7
- internationalization. Lint comes with over 200 checks, however it can be extended with additional
8
- custom rules.
5
+ analysis tool that checks your project source files for potential bugs and optimization
6
+ improvements for correctness, security, performance, usability, accessibility, and
7
+ internationalization. Lint comes with around 400 built-in checks, but it can be extended with
8
+ additional custom checks. This sample project shows how those sample checks can be built
9
+ and packaged.
10
+
11
+ Note that while Android Lint has the name "Android" in it, it is no longer an Android-specific
12
+ static analysis tool; it's a general static analysis tool, and inside Google for example it is
13
+ run to analyze server-side Java and Kotlin code.
9
14
10
15
** NOTE: The lint API is not a final API; if you rely on this be prepared
11
16
to adjust your code for the next tools release.**
12
17
13
18
Introduction
14
19
------------
15
20
16
- The Android Lint API allows users to create custom lint rules . For example, if you are the author of
17
- a library project, and your library project has certain usage requirements, you can write
18
- additional lint rules to check that your library is used correctly, and then you can distribute
19
- those extra lint rules for users of the library. Similarly, you may have company-local rules you'd
20
- like to enforce.
21
+ The Android Lint API allows users to create custom lint checks . For example, if you are the author of
22
+ an Android library project, and your library project has certain usage requirements, you can write
23
+ additional lint rules to check that your library is used correctly, and then you can distribute
24
+ those extra lint rules for users of the library. Similarly, you may have company-local rules you'd
25
+ like to enforce.
21
26
22
27
This sample demonstrates how to create a custom lint checks and corresponding tests for those rules.
23
28
29
+
30
+ # Sample Lint Checks
31
+
32
+ This project shows how Android Studio as well as the Android Gradle plugin handles packaging of lint
33
+ rules.
34
+
35
+ ## Lint Check Jar Library
36
+
37
+ First, there's the lint check implementation itself. That's done in the
38
+ "checks" project, which just applies the Gradle "java" or "kotlin" plugins, and
39
+ that project produces a jar. Note that the dependencies for the lint
40
+ check project (other than its testing dependencies) must all be "compileOnly":
41
+
42
+ dependencies {
43
+ compileOnly "com.android.tools.lint:lint-api:$lintVersion"
44
+ compileOnly "com.android.tools.lint:lint-checks:$lintVersion"
45
+ ...
46
+
47
+ ## Lint Check AAR Library
48
+
49
+ Next, there's a separate Android library project, called "library". This
50
+ library doesn't have any code on its own (though it could). However,
51
+ in its build.gradle, it specifies this:
52
+
53
+ dependencies {
54
+ lintPublish project(':checks')
55
+ }
56
+
57
+ This tells the Gradle plugin to take the output from the "checks" project
58
+ and package that as a "lint.jar" payload inside this library's AAR file.
59
+ When that's done, any other projects that depends on this library will
60
+ automatically be using the lint checks.
61
+
62
+ ## App Modules
63
+
64
+ Note that you don't have to go through the extra "library indirection"
65
+ if you have a lint check that you only want to apply to one or more
66
+ app modules. You can simply include the ` lintChecks ` dependency as shown
67
+ above there as well, and then lint will include these rules when analyzing
68
+ the project.
69
+
70
+ ## Lint Version
71
+
72
+ The lint version of the libraries (specified in this project as the
73
+ ` lintVersion ` variable in build.gradle) should be the same version
74
+ that is used by the Gradle plugin.
75
+
76
+ If the Gradle plugin version is * X* .* Y* .* Z* , then the Lint library
77
+ version is * X+23* .* Y* .* Z* .
78
+
79
+ For example, for AGP 7.0.0-alpha08, the lint API versions are 30.0.0-alpha08.
80
+
24
81
Getting Started
25
82
---------------
26
83
@@ -31,36 +88,68 @@ git clone https://github.com/googlesamples/android-custom-lint-rules.git
31
88
cd android-custom-lint-rules
32
89
```
33
90
34
- ##### Build the code
91
+ ##### Run The Sample
92
+
93
+ Run the :app: lint target to have first the custom lint checks in checks/
94
+ compiled, then wrapped into the library, and finally run lint on a
95
+ sample app module which has violations of the check enforced by sample
96
+ check in this project:
97
+ ```
98
+ $ ./gradlew :app:lint
99
+
100
+ > Task :app:lintDebug
101
+
102
+ Scanning app: ...
103
+ Wrote HTML report to file:///demo/android-custom-lint-rules/app/build/reports/lint-results-debug.html
104
+ Wrote SARIF report to file:///demo/android-custom-lint-rules/app/build/reports/lint-results-debug.sarif
35
105
36
- For Android Studio 3.x and above, use the sample in ` android-studio-3 ` .
37
- If you are targeting Android Studio 2.x and older, use the sample in ` android-studio-2 ` .
106
+ /demo/android-custom-lint-rules/app/src/main/java/com/android/example/Test.kt:8: Warning: This code mentions lint: Congratulations [ShortUniqueId]
107
+ val s = "lint"
108
+ ~~~~
109
+
110
+ Explanation for issues of type "ShortUniqueId":
111
+ This check highlights string literals in code which mentions the word lint.
112
+ Blah blah blah.
113
+
114
+ Another paragraph here.
115
+
116
+ Vendor: Android Open Source Project
117
+ Contact: https://github.com/googlesamples/android-custom-lint-rules
118
+ Feedback: https://github.com/googlesamples/android-custom-lint-rules/issues
119
+
120
+ 0 errors, 1 warnings
121
+
122
+ BUILD SUCCESSFUL in 1s
123
+ ```
38
124
39
125
##### Lint Dependencies
40
126
41
- When building your own rules, you will likely want to know which dependencies you should bring into your own project.
42
- The below descriptions of the dependencies included within this project serve to help you make that decision:
127
+ When building your own rules, you will likely want to know which dependencies you should
128
+ bring into your own project. The below descriptions of the dependencies included within
129
+ this project serve to help you make that decision:
43
130
44
131
Source Dependencies
45
132
46
- - ** com.android.tools.lint: lint-api ** : The most important one; it contains things like ` LintClient ` , the ` Detector `
47
- base class, the ` Issue ` class, and everything else that Lint checks rely on in the Lint framework.
48
- - ** com.android.tools.lint: lint-checks ** : Contains the built-in checks that are developed internally. Also contains
49
- utilities that are sometimes useful for other lint checks, such as the ` VersionChecks ` class (which figures out whether
50
- a given UAST element is known to only be called at a given API level, either by surrounding ` if >= SDK-version ` checks or
51
- ` if < SDK-version ` early returns in the method).
133
+ - ** com.android.tools.lint: lint-api ** : The most important one; it contains things
134
+ like ` LintClient ` , the ` Detector ` base class, the ` Issue ` class, and everything else
135
+ that Lint checks rely on in the Lint framework.
136
+ - ** com.android.tools.lint: lint-checks ** : Contains the built-in checks that are developed
137
+ internally. Also contains utilities that are sometimes useful for other lint checks,
138
+ such as the ` VersionChecks ` class (which figures out whether a given UAST element is
139
+ known to only be called at a given API level, either by surrounding ` if >= SDK-version `
140
+ checks or ` if < SDK-version ` early returns in the method).
52
141
53
142
Test Dependencies
54
143
55
- - ** com.android.tools.lint: lint-tests ** : Contains useful utilities for writing unit tests for Lint checks,
56
- including the ` LintDetectorTest ` base class.
57
- - ** com.android.tools: testutils ** : It's unlikely that you need to depend on this directly. The test infrastructure
58
- depends on it indirectly though (the methods we use there were mostly for the older lint test infrastructure,
59
- not the newer one).
60
- - ** com.android.tools.lint: lint ** : Lint checks don't need to depend on this. It's a separate artifact used by tools
61
- that want to integrate lint with the command line, such as the Gradle integration of lint. This is where things like
62
- terminal output, HTML reporting, command line parsing etc is handled.
144
+ - ** com.android.tools.lint: lint-tests ** : Contains useful utilities for writing unit tests
145
+ for Lint checks, including the ` LintDetectorTest ` base class.
146
+ - ** com.android.tools.lint: lint ** : Lint checks don't need to depend on this. It's a
147
+ separate artifact used by tools that want to integrate lint with the command line,
148
+ such as the Gradle integration of lint. This is where things like terminal output, HTML
149
+ reporting, command line parsing etc is handled.
63
150
151
+ The APIs in all but the lint-api artifact are more likely to change incompatibly than
152
+ the lint-api artifact.
64
153
65
154
Support
66
155
-------
0 commit comments