Skip to content

Commit c4819bd

Browse files
authored
Merge pull request #34 from tnorbye/flatten
Cleanup: Flatten directory structure, update to new versions
2 parents dc9cb3f + baae092 commit c4819bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+288
-1423
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ Thumbs.db
3232
#.idea/workspace.xml - remove # and delete .idea if it better suit your needs.
3333
.gradle
3434
build/
35+
36+
lint-results.sarif

README.md

Lines changed: 117 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,82 @@ Custom Lint Rules
22
=================
33

44
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.
914

1015
**NOTE: The lint API is not a final API; if you rely on this be prepared
1116
to adjust your code for the next tools release.**
1217

1318
Introduction
1419
------------
1520

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.
2126

2227
This sample demonstrates how to create a custom lint checks and corresponding tests for those rules.
2328

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+
2481
Getting Started
2582
---------------
2683

@@ -31,36 +88,68 @@ git clone https://github.com/googlesamples/android-custom-lint-rules.git
3188
cd android-custom-lint-rules
3289
```
3390

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
35105
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+
```
38124

39125
##### Lint Dependencies
40126

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:
43130

44131
Source Dependencies
45132

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).
52141

53142
Test Dependencies
54143

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.
63150

151+
The APIs in all but the lint-api artifact are more likely to change incompatibly than
152+
the lint-api artifact.
64153

65154
Support
66155
-------

android-studio-2/README.md

Lines changed: 0 additions & 33 deletions
This file was deleted.

android-studio-2/build.gradle

Lines changed: 0 additions & 44 deletions
This file was deleted.

android-studio-2/gradle/wrapper/gradle-wrapper.properties

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)