Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

This sample demonstrates how to create a custom lint checks and corresponding lint tests

License

NotificationsYou must be signed in to change notification settings

googlesamples/android-custom-lint-rules

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:

Lint

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.

Introduction

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.

Sample Lint Checks

This project shows how Android Studio as well as the Android Gradle plugin handles packaging of lintrules.

Lint Check Jar Library

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

Lint Check AAR Library

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.

App Modules

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.

Lint Version

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.

Getting Started

Fetch code
git clone https://github.com/googlesamples/android-custom-lint-rules.gitcd android-custom-lint-rules
Run The Sample

Run 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 1s
Lint Dependencies

When 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 thingslikeLintClient, theDetector base class, theIssue class, 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 theVersionChecks class (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-version early returns in the method).

Test Dependencies

  • com.android.tools.lint:lint-tests: Contains useful utilities for writing unit testsfor Lint checks, including theLintDetectorTest base 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.

Support

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.

License

Licensed under the Apache 2.0 license. See theLICENSE file fordetails.

How to make contributions?

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors12

Languages


[8]ページ先頭

©2009-2025 Movatter.jp