{"id":13695814,"url":"https://github.com/googlesamples/android-custom-lint-rules","last_synced_at":"2025-05-14T20:05:36.072Z","repository":{"id":2041492,"uuid":"41508546","full_name":"googlesamples/android-custom-lint-rules","owner":"googlesamples","description":"This sample demonstrates how to create a custom lint checks and corresponding lint tests","archived":false,"fork":false,"pushed_at":"2025-01-16T21:30:24.000Z","size":6241,"stargazers_count":988,"open_issues_count":24,"forks_count":173,"subscribers_count":35,"default_branch":"main","last_synced_at":"2025-04-13T14:06:40.058Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/googlesamples.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-08-27T20:01:41.000Z","updated_at":"2025-04-03T19:14:20.000Z","dependencies_parsed_at":"2024-06-02T03:45:38.742Z","dependency_job_id":"d7a12828-a50c-493a-936b-051a20a5942c","html_url":"https://github.com/googlesamples/android-custom-lint-rules","commit_stats":{"total_commits":53,"total_committers":16,"mean_commits":3.3125,"dds":0.4339622641509434,"last_synced_commit":"1d0569d8bf1acd58f8736fc7e4b7a6f36499d7b1"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googlesamples%2Fandroid-custom-lint-rules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googlesamples%2Fandroid-custom-lint-rules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googlesamples%2Fandroid-custom-lint-rules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googlesamples%2Fandroid-custom-lint-rules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/googlesamples","download_url":"https://codeload.github.com/googlesamples/android-custom-lint-rules/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248724639,"owners_count":21151561,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T18:00:33.765Z","updated_at":"2025-04-13T14:06:46.884Z","avatar_url":"https://github.com/googlesamples.png","language":"Kotlin","funding_links":[],"categories":["Rules"],"sub_categories":[],"readme":"Custom Lint Rules\n=================\n\nThe lint source code contains a lot of documentation on how to write\ncustom checks; this git repository contains a snapshot of this\ndocumentation which you can read here:\n\n* [Full API Guide](https://googlesamples.github.io/android-custom-lint-rules/api-guide.html)\n* [Other docs](https://googlesamples.github.io/android-custom-lint-rules/index.html)\n\nLint\n----\n\nThe [Android `lint` tool](http://developer.android.com/tools/help/lint.html) is a static code\nanalysis tool that checks your project source files for potential bugs and optimization\nimprovements for correctness, security, performance, usability, accessibility, and\ninternationalization. Lint comes with around 400 built-in checks, but it can be extended with \nadditional custom checks. This sample project shows how those sample checks can be built\nand packaged.\n\nNote that while Android Lint has the name \"Android\" in it, it is no longer an Android-specific\nstatic analysis tool; it's a general static analysis tool, and inside Google for example it is\nrun to analyze server-side Java and Kotlin code.\n\n**NOTE: The lint API is not a final API; if you rely on this be prepared\n to adjust your code for the next tools release.**\n\nIntroduction\n------------\n\nThe Android Lint API allows users to create custom lint checks. For example, if you are the author of\nan Android library project, and your library project has certain usage requirements, you can write\nadditional lint rules to check that your library is used correctly, and then you can distribute\nthose extra lint rules for users of the library. Similarly, you may have company-local rules you'd\nlike to enforce.\n\nThis sample demonstrates how to create a custom lint checks and corresponding tests for those rules.\n\n\n# Sample Lint Checks\n\nThis project shows how Android Studio as well as the Android Gradle plugin handles packaging of lint\nrules.\n\n## Lint Check Jar Library\n\nFirst, there's the lint check implementation itself. That's done in the\n\"checks\" project, which just applies the Gradle \"java\" or \"kotlin\" plugins, and\nthat project produces a jar. Note that the dependencies for the lint\ncheck project (other than its testing dependencies) must all be \"compileOnly\":\n\n    dependencies {\n        compileOnly \"com.android.tools.lint:lint-api:$lintVersion\"\n        compileOnly \"com.android.tools.lint:lint-checks:$lintVersion\"\n\t\t...\n\n## Lint Check AAR Library\n\nNext, there's a separate Android library project, called \"library\". This\nlibrary doesn't have any code on its own (though it could). However,\nin its build.gradle, it specifies this:\n\n    dependencies {\n        lintPublish project(':checks')\n    }\n\nThis tells the Gradle plugin to take the output from the \"checks\" project\nand package that as a \"lint.jar\" payload inside this library's AAR file.\nWhen that's done, any other projects that depends on this library will\nautomatically be using the lint checks.\n\n## App Modules\n\nNote that you don't have to go through the extra \"library indirection\"\nif you have a lint check that you only want to apply to one or more\napp modules. You can simply include the `lintChecks` dependency as shown\nabove there as well, and then lint will include these rules when analyzing\nthe project.\n\n## Lint Version\n\nThe lint version of the libraries (specified in this project as the\n`lintVersion` variable in build.gradle) should be the same version\nthat is used by the Gradle plugin.\n\nIf the Gradle plugin version is *X*.*Y*.*Z*, then the Lint library\nversion is *X+23*.*Y*.*Z*.\n\nFor example, for AGP 7.0.0-alpha08, the lint API versions are 30.0.0-alpha08.\n\nGetting Started\n---------------\n\n##### Fetch code\n\n```\ngit clone https://github.com/googlesamples/android-custom-lint-rules.git\ncd android-custom-lint-rules\n```\n\n##### Run The Sample\n\nRun the :app:lint target to have first the custom lint checks in checks/\ncompiled, then wrapped into the library, and finally run lint on a\nsample app module which has violations of the check enforced by sample\ncheck in this project:\n```\n$ ./gradlew :app:lint\n\n\u003e Task :app:lintDebug\n\nScanning app: ...\nWrote HTML report to file:///demo/android-custom-lint-rules/app/build/reports/lint-results-debug.html\nWrote SARIF report to file:///demo/android-custom-lint-rules/app/build/reports/lint-results-debug.sarif\n\n/demo/android-custom-lint-rules/app/src/main/java/com/android/example/Test.kt:8: Warning: This code mentions lint: Congratulations [SampleId]\n    val s = \"lint\"\n             ~~~~\n\n   Explanation for issues of type \"SampleId\":\n   This check highlights string literals in code which mentions the word lint.\n   Blah blah blah.\n\n   Another paragraph here.\n\n   Vendor: Android Open Source Project\n   Contact: https://github.com/googlesamples/android-custom-lint-rules\n   Feedback: https://github.com/googlesamples/android-custom-lint-rules/issues\n\n0 errors, 1 warnings\n\nBUILD SUCCESSFUL in 1s\n```\n\n##### Lint Dependencies\n\nWhen building your own rules, you will likely want to know which dependencies you should \nbring into your own project. The below descriptions of the dependencies included within\nthis project serve to help you make that decision:\n\nSource Dependencies\n\n- **com.android.tools.lint:lint-api**: The most important one; it contains things \n  like `LintClient`, the `Detector` base class, the `Issue` class, and everything else \n  that Lint checks rely on in the Lint framework.\n- **com.android.tools.lint:lint-checks**: Contains the built-in checks that are developed \n  internally. Also contains utilities that are sometimes useful for other lint checks, \n  such as the `VersionChecks` class (which figures out whether a given UAST element is \n  known to only be called at a given API level, either by surrounding `if \u003e= SDK-version`\n  checks or `if \u003c SDK-version` early returns in the method).\n\nTest Dependencies\n\n- **com.android.tools.lint:lint-tests**: Contains useful utilities for writing unit tests \n  for Lint checks, including the `LintDetectorTest` base class.\n- **com.android.tools.lint:lint**: Lint checks don't need to depend on this. It's a \n  separate artifact used by tools that want to integrate lint with the command line, \n  such as the Gradle integration of lint. This is where things like terminal output, HTML \n  reporting, command line parsing etc is handled.\n\nThe APIs in all but the lint-api artifact are more likely to change incompatibly than\nthe lint-api artifact.\n\nSupport\n-------\n\n- The \"lint-dev\" Google group: https://groups.google.com/forum/#!forum/lint-dev\n- Stack Overflow: http://stackoverflow.com/questions/tagged/android\n\nIf you've found an error in this sample, please file an issue:\nhttps://github.com/googlesamples/android-custom-lint-rules/issues\n\nPatches are encouraged, and may be submitted by forking this project and\nsubmitting a pull request through GitHub.\n\nLicense\n-------\nLicensed under the Apache 2.0 license. See the [LICENSE](LICENSE) file for \ndetails.\n\nHow to make contributions?\n--------------------------\nPlease read and follow the steps in the [CONTRIBUTING](CONTRIBUTING.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgooglesamples%2Fandroid-custom-lint-rules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgooglesamples%2Fandroid-custom-lint-rules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgooglesamples%2Fandroid-custom-lint-rules/lists"}