• Stars
    star
    949
  • Rank 48,153 (Top 1.0 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created about 9 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Custom Lint Rules

The lint source code contains a lot of documentation on how to write custom checks; this git repository contains a snapshot of this documentation which you can read here:

Lint

The Android lint tool is a static code analysis tool that checks your project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization. Lint comes with around 400 built-in checks, but it can be extended with additional custom checks. This sample project shows how those sample checks can be built and packaged.

Note that while Android Lint has the name "Android" in it, it is no longer an Android-specific static analysis tool; it's a general static analysis tool, and inside Google for example it is run to analyze server-side Java and Kotlin code.

NOTE: The lint API is not a final API; if you rely on this be prepared to 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 of an Android library project, and your library project has certain usage requirements, you can write additional lint rules to check that your library is used correctly, and then you can distribute those extra lint rules for users of the library. Similarly, you may have company-local rules you'd like 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 lint rules.

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, and that project produces a jar. Note that the dependencies for the lint check 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". This library 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" project and 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 will automatically 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 more app modules. You can simply include the lintChecks dependency as shown above there as well, and then lint will include these rules when analyzing the project.

Lint Version

The lint version of the libraries (specified in this project as the lintVersion variable in build.gradle) should be the same version that is used by the Gradle plugin.

If the Gradle plugin version is X.Y.Z, then the Lint library version is X+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.git
cd 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 a sample app module which has violations of the check enforced by sample check in this project:

$ ./gradlew :app:lint

> Task :app:lintDebug

Scanning app: ...
Wrote HTML report to file:///demo/android-custom-lint-rules/app/build/reports/lint-results-debug.html
Wrote 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/issues

0 errors, 1 warnings

BUILD SUCCESSFUL in 1s
Lint Dependencies

When building your own rules, you will likely want to know which dependencies you should bring into your own project. The below descriptions of the dependencies included within this project serve to help you make that decision:

Source Dependencies

  • com.android.tools.lint:lint-api: The most important one; it contains things like LintClient, the Detector base class, the Issue class, and everything else that Lint checks rely on in the Lint framework.
  • com.android.tools.lint:lint-checks: Contains the built-in checks that are developed internally. Also contains utilities that are sometimes useful for other lint checks, such as the VersionChecks class (which figures out whether a given UAST element is known to only be called at a given API level, either by surrounding if >= SDK-version checks or if < SDK-version early returns in the method).

Test Dependencies

  • com.android.tools.lint:lint-tests: Contains useful utilities for writing unit tests for Lint checks, including the LintDetectorTest base class.
  • com.android.tools.lint:lint: Lint checks don't need to depend on this. It's a separate 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, HTML reporting, command line parsing etc is handled.

The APIs in all but the lint-api artifact are more likely to change incompatibly than the 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 and submitting a pull request through GitHub.

License

Licensed under the Apache 2.0 license. See the LICENSE file for details.

How to make contributions?

Please read and follow the steps in the CONTRIBUTING

More Repositories

1

easypermissions

Simplify Android M system permissions
Java
9,846
star
2

mlkit

A collection of sample apps to demonstrate how to use Google's ML Kit APIs on Android and iOS
Java
3,534
star
3

google-services

A collection of quickstart samples demonstrating the Google APIs for Android and iOS
Java
3,045
star
4

android-vision

Deprecated: The Mobile Vision API is now a part of ML Kit: Check out this repo:
Java
2,928
star
5

android-testing-templates

Java
1,960
star
6

unity-jar-resolver

Unity plugin which resolves Android & iOS dependencies and performs version management
C#
1,275
star
7

mediapipe

Jupyter Notebook
1,253
star
8

assistant-sdk-python

Samples and bindings for the Google Assistant API
Python
914
star
9

android-vulkan-tutorials

A set of samples to illustrate Vulkan API on Android
C++
849
star
10

arcore-depth-lab

ARCore Depth Lab is a set of Depth API samples that provides assets using depth for advanced geometry-aware features in AR interaction and rendering. (UIST 2020)
C#
777
star
11

android-testdpc

Test DPC is a sample device policy controller for use with Android Enterprise. It gives developers the ability to see how their app will behave in a managed context such as device owner or within a managed profile. Users can set up a work profile, enable work apps, set applications restrictions, manage security polices, and much more. The app also serves as a implementation reference for other DPCs
Java
751
star
12

io2015-codelabs

codelabs for Google I/O 2015
Java
517
star
13

google-photos

Samples for the Google Photos Library API 📸
JavaScript
495
star
14

vulkan-basic-samples

C++
494
star
15

android-play-publisher-api

Java
491
star
16

androidtv-sample-inputs

Sample Channel App (TV Input Service) on Android TV using TIF
Java
487
star
17

oauth-apps-for-windows

OAuth for Apps: Samples for Windows
C#
474
star
18

android-media-controller

Kotlin
448
star
19

android-dynamic-code-loading

Android dynamic code loading sample for Dynamic Feature Modules.
Kotlin
423
star
20

google-signin-unity

Google Sign-In API plugin for Unity game engine. Works with Android and iOS.
C++
407
star
21

android-AppUsageStatistics

Java
366
star
22

web-fundamentals

Google Web Fundamentals
HTML
313
star
23

android-play-safetynet

Samples for the Google SafetyNet Attestation API
Java
287
star
24

io2014-codelabs

Google I/O 2014 Codelabs
Java
177
star
25

android-play-games-in-motion

Java
152
star
26

glass-enterprise-samples

Glass Enterprise Edition 2 Samples
Java
123
star
27

cloud-polymer-go

Sample App Engine application with Go, Cloud Endpoints, and Polymer
HTML
118
star
28

appauth-js-electron-sample

This is an Electron Application, which uses the AppAuth-JS library.
TypeScript
115
star
29

arcore-lightboard

C#
97
star
30

assistant-sdk-cpp

Example of Google Assistant gRPC in C++
C++
96
star
31

android-PermissionRequest

Java
95
star
32

ios-vision

Objective-C
93
star
33

assistant-sdk-nodejs

JavaScript
93
star
34

sceneform-samples

Sceneform samples for 3D rendering for ARCore in Java.
Java
87
star
35

arcore-ml-sample

Java
71
star
36

ios-nearby

Objective-C
68
star
37

arcore-illusive-images

C#
62
star
38

mugo

Sample on how to transpile a small subset of go to Arduino sketches
Go
47
star
39

identity-appflip-android

Lightweight Android app that simulates your native app role during App Flip
Java
34
star
40

identity-toolkit-node

JavaScript
29
star
41

functions-as-a-service

A demo showing Google Cloud Functions + Google Maps Platform
TypeScript
28
star
42

identity-toolkit-go

Identity toolkit sample code for Go
Go
25
star
43

identity-toolkit-java

Java
23
star
44

identity-appflip-tester-android

Lightweight Android app that simulates the Google app role during App Flip
Java
18
star
45

onetwoseven

Programmers debugging web server.
Go
16
star
46

identity-toolkit-php

PHP
15
star
47

android-TensorFlowCloudMachineLearningEngineStylizer

Java
14
star
48

Firebase-Plays-GCP-2016

JavaScript
14
star
49

identity-toolkit-ios

Objective-C
14
star
50

brillo-dragonboard-jacksmart

12
star
51

maps-deckgl-scatterplot-example

JavaScript
11
star
52

io19-sonic-boom

Live coding demo of "Sonic Boom!" talk in Google I/O 2019
C++
11
star
53

meet-live-sharing

Java
10
star
54

identity-appflip-ios

Lightweight iOS app that simulates your native app role during App Flip
Swift
10
star
55

dcp-parser-go

Go
9
star
56

identity-appflip-tester-ios

Lightweight iOS app that simulates the Google app role during App Flip
Swift
9
star
57

amapi

Kotlin
8
star
58

testloopmanager

Java
8
star
59

task-interop

Kotlin
6
star
60

sceneform-poly-browser

Java
6
star
61

subgraph_sdk_sample

Kotlin
6
star
62

identity-toolkit-ruby

HTML
4
star
63

engage-sdk-samples

Set of sample apps that demonstrate how to integrate the SDK in your app to publish different types of content. These apps are a great way to learn how to use the SDK, to get started with the integration in your own app, as well as some best practices.
Java
4
star
64

gboard-dev-samples

Java
4
star
65

identity-toolkit-django

2
star
66

snippets

Hosting for miscellaneous code snippets
2
star
67

pluscode-swift-demo

An example Plus Code service written in Swift
Swift
2
star
68

searchinapps-sample

Kotlin
2
star
69

.allstar

1
star
70

.github

1
star
71

zero-touch-enrollment-colabs

Jupyter Notebook
1
star