- Notifications
You must be signed in to change notification settings - Fork173
This sample demonstrates how to create a custom lint checks and corresponding lint tests
License
googlesamples/android-custom-lint-rules
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
The lint source code contains a lot of documentation on how to writecustom checks; this git repository contains a snapshot of thisdocumentation which you can read here:
TheAndroidlint tool is a static codeanalysis tool that checks your project source files for potential bugs and optimizationimprovements for correctness, security, performance, usability, accessibility, andinternationalization. Lint comes with around 400 built-in checks, but it can be extended withadditional custom checks. This sample project shows how those sample checks can be builtand packaged.
Note that while Android Lint has the name "Android" in it, it is no longer an Android-specificstatic analysis tool; it's a general static analysis tool, and inside Google for example it isrun to analyze server-side Java and Kotlin code.
NOTE: The lint API is not a final API; if you rely on this be preparedto adjust your code for the next tools release.
The Android Lint API allows users to create custom lint checks. For example, if you are the author ofan Android library project, and your library project has certain usage requirements, you can writeadditional lint rules to check that your library is used correctly, and then you can distributethose extra lint rules for users of the library. Similarly, you may have company-local rules you'dlike to enforce.
This sample demonstrates how to create a custom lint checks and corresponding tests for those rules.
This project shows how Android Studio as well as the Android Gradle plugin handles packaging of lintrules.
First, there's the lint check implementation itself. That's done in the"checks" project, which just applies the Gradle "java" or "kotlin" plugins, andthat project produces a jar. Note that the dependencies for the lintcheck project (other than its testing dependencies) must all be "compileOnly":
dependencies { compileOnly "com.android.tools.lint:lint-api:$lintVersion" compileOnly "com.android.tools.lint:lint-checks:$lintVersion"...Next, there's a separate Android library project, called "library". Thislibrary doesn't have any code on its own (though it could). However,in its build.gradle, it specifies this:
dependencies { lintPublish project(':checks')}This tells the Gradle plugin to take the output from the "checks" projectand package that as a "lint.jar" payload inside this library's AAR file.When that's done, any other projects that depends on this library willautomatically be using the lint checks.
Note that you don't have to go through the extra "library indirection"if you have a lint check that you only want to apply to one or moreapp modules. You can simply include thelintChecks dependency as shownabove there as well, and then lint will include these rules when analyzingthe project.
The lint version of the libraries (specified in this project as thelintVersion variable in build.gradle) should be the same versionthat is used by the Gradle plugin.
If the Gradle plugin version isX.Y.Z, then the Lint libraryversion isX+23.Y.Z.
For example, for AGP 7.0.0-alpha08, the lint API versions are 30.0.0-alpha08.
git clone https://github.com/googlesamples/android-custom-lint-rules.gitcd android-custom-lint-rulesRun the :app:lint target to have first the custom lint checks in checks/compiled, then wrapped into the library, and finally run lint on asample app module which has violations of the check enforced by samplecheck in this project:
$ ./gradlew :app:lint> Task :app:lintDebugScanning app: ...Wrote HTML report to file:///demo/android-custom-lint-rules/app/build/reports/lint-results-debug.htmlWrote SARIF report to file:///demo/android-custom-lint-rules/app/build/reports/lint-results-debug.sarif/demo/android-custom-lint-rules/app/src/main/java/com/android/example/Test.kt:8: Warning: This code mentions lint: Congratulations [SampleId] val s = "lint" ~~~~ Explanation for issues of type "SampleId": This check highlights string literals in code which mentions the word lint. Blah blah blah. Another paragraph here. Vendor: Android Open Source Project Contact: https://github.com/googlesamples/android-custom-lint-rules Feedback: https://github.com/googlesamples/android-custom-lint-rules/issues0 errors, 1 warningsBUILD SUCCESSFUL in 1sWhen building your own rules, you will likely want to know which dependencies you shouldbring into your own project. The below descriptions of the dependencies included withinthis project serve to help you make that decision:
Source Dependencies
- com.android.tools.lint:lint-api: The most important one; it contains thingslike
LintClient, theDetectorbase class, theIssueclass, and everything elsethat Lint checks rely on in the Lint framework. - com.android.tools.lint:lint-checks: Contains the built-in checks that are developedinternally. Also contains utilities that are sometimes useful for other lint checks,such as the
VersionChecksclass (which figures out whether a given UAST element isknown to only be called at a given API level, either by surroundingif >= SDK-versionchecks orif < SDK-versionearly returns in the method).
Test Dependencies
- com.android.tools.lint:lint-tests: Contains useful utilities for writing unit testsfor Lint checks, including the
LintDetectorTestbase class. - com.android.tools.lint:lint: Lint checks don't need to depend on this. It's aseparate artifact used by tools that want to integrate lint with the command line,such as the Gradle integration of lint. This is where things like terminal output, HTMLreporting, command line parsing etc is handled.
The APIs in all but the lint-api artifact are more likely to change incompatibly thanthe lint-api artifact.
- The "lint-dev" Google group:https://groups.google.com/forum/#!forum/lint-dev
- Stack Overflow:http://stackoverflow.com/questions/tagged/android
If you've found an error in this sample, please file an issue:https://github.com/googlesamples/android-custom-lint-rules/issues
Patches are encouraged, and may be submitted by forking this project andsubmitting a pull request through GitHub.
Licensed under the Apache 2.0 license. See theLICENSE file fordetails.
Please read and follow the steps in theCONTRIBUTING
About
This sample demonstrates how to create a custom lint checks and corresponding lint tests
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors12
Uh oh!
There was an error while loading.Please reload this page.