Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions 2_lint_api_basics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ This section covers the core concepts of the Android Lint API. The subsequent se

## 2.1 IssueRegistry

>####Definition
>#### Definition
>An `IssueRegistry` is a registry which provides a list of checks to be performed on an Android project.

All checks* which should be considered during Lint analysis needs to be registered in an `IssueRegistry`. The built-in checks are registered within the `BuiltinIssueRegistry` class in the `com.android.tools.lint.checks` package.
All checks* which should be considered during Lint analysis need to be registered in an `IssueRegistry`. The built-in checks are registered within the `BuiltinIssueRegistry` class in the `com.android.tools.lint.checks` package.

In order to make custom Lint checks available for Lint analysis you need to subclass `IssueRegistry` an override its `getIssues` method which provides a `List` of `Issues`. Here is a straightforward example:
In order to make custom Lint checks available for Lint analysis you need to subclass `IssueRegistry` and override its `getIssues` method which provides a `List` of `Issues`. Here is a straightforward example:

```java
public class CustomIssueRegistry extends IssueRegistry {
@Override
public List<Issue> getIssues() {
return Arrays.asList( //Note:
MyCustomCheck.ISSUE, //A check actually is a detector.
MyCustomCheck.ISSUE, //A check is actually a detector.
MyAdvancedCheck.AN_ISSUE, //One detector can report
MyAdvancedCheck.ANOTHER_ISSUE //multiple types of issues.
);
}
}
```

Last but not least it is necessary to reference your `IssueRegistry` class within your `MANIFEST`, such that it can be found by Lint. For Gradle-based projects this could be achieved by:
Last but not least, it is necessary to reference your `IssueRegistry` class within your `MANIFEST`, such that it can be found by Lint. For Gradle-based projects this could be achieved by:

```groovy
jar {
Expand All @@ -40,20 +40,20 @@ You will find a complete example with a lot more code in section 3.

## 2.2 Detector

>####Definition
>#### Definition
>A detector is responsible for scanning through code and finding issue instances and reporting them.

The `Detector` class is the central element for the definition of Lint rules. More precisely a `Detector` is an implementation of a Lint rule. It contains the logic to find certain problems in your development artifacts and map it to particular `Issues`. You will learn more about `Issues` in section 2.4.

In order to write a custom Lint rule you will need to subclass `Detector`. For certain use cases the Lint API also provides specific `Detectors` for your convenience, such as `LayoutDetector` or `ResourceXmlDetector` - but they are in turn also sub-classes of `Detector`.

Besides extending the `Detector` class you also need to implement a `Scanner` interface which defines how your `Detector` will scan the development artifacts! **By default the `Detector` class declares all methods of all available `Scanners` but you will notice that only the once of the implemented interface(s) will get called!**
Besides extending the `Detector` class you also need to implement a `Scanner` interface which defines how your `Detector` will scan the development artifacts! **By default the `Detector` class declares all methods of all available `Scanners` but you will notice that only the ones of the implemented interface(s) will get called!**

The different types of `Scanner` will be presented in the next section.
The different types of `Scanners` will be presented in the next section.

## 2.3 Scanner

>####Definition
>#### Definition
>A scanner is a specialized interface for detectors.

Currently seven different types of `Scanner` exist:
Expand Down Expand Up @@ -81,7 +81,7 @@ As you can see, the `JavaScanner` interface provides dedicated methods for scann

## 2.4 Issue

>####Definition
>#### Definition
>An issue is a type of problem you want to find and show to the user. An issue has an associated description, fuller explanation, category, priority, etc.

So an `Issue` is the link between all previously described concepts. An `Issue` is registered within an `IssueRegistry`, such that Lint is aware of it, and it is detected and reported by a `Detector`, so that it becomes visible to the user. A `Detector` can report more than one type of `Issue`.
Expand Down
19 changes: 7 additions & 12 deletions 4_detectors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ Detectors represent the actual implementation of a custom Lint rule. Detectors h

## Simple detectors

>####Definition
>#### Definition
> Simple detectors scan isolated artifacts of one type
(e.g. just code or just resources). Scan and evaluation is performed in one phase.
(e.g. just code or just resources). Scan and evaluation are performed in one phase.

In order to elaborate the aforementioned definition of simple detectors a bit let's have a look at Android's `HardcodedValuesDetecor` [1], which is a good example for a simple detector. It's responsible for detecting hardcoded values, which means if you have defined values in your layouts or menus directly instead of referencing them from a dedicated `strings.xml` file.
In order to elaborate the aforementioned definition of simple detectors a bit let's have a look at Android's [`HardcodedValuesDetecor`](https://android.googlesource.com/platform/tools/base/+/android-m-preview/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks), which is a good example of a simple detector. It's responsible for detecting hardcoded values, which means you have defined values in your layouts or menus directly instead of referencing them from a dedicated `strings.xml` file.

In order to detect such flaws only layout or menu files are scanned. In each file all value-containing attributes, such as `text` or `hint`, are visited. Their values are evaluated and in case they are no references, for instance they don't start with `@`, an `Issue` (see section 2.4) is reported. This evaluation is performed during scan phase directly:
In order to detect such flaws only layout or menu files are scanned. In each file all value-containing attributes, such as `text` or `hint`, are visited. Their values are evaluated and in case they are no references, for instance they don't start with `@`, an `Issue` (see section 2.4) is reported. This evaluation is performed during the scan phase directly:

```java
public class HardcodedValuesDetector extends LayoutDetector {
Expand Down Expand Up @@ -40,23 +40,18 @@ public class HardcodedValuesDetector extends LayoutDetector {
}
```

Other examples for simple detector are `SdCardDetector` [2] or `MissingIdDetector` [3].
Other examples for simple detector are [`SdCardDetector`](https://android.googlesource.com/platform/tools/base/+/android-m-preview/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/SdCardDetector.java) or [`MissingIdDetector`](https://android.googlesource.com/platform/tools/base/+/android-m-preview/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/MissingIdDetector.java).

In general, typically use cases for simple detectors are naming conventions or invalid value detection.
In general, typical use cases for simple detectors are naming conventions or invalid value detection.

## Advanced detectors

>####Definition
>#### Definition
> Advanced detectors scan related artifacts of different types.
Scan and evaluation is performed in two phases.

*This section is still under construction.*

##References
1. https://android.googlesource.com/platform/tools/base/+/android-m-preview/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks
2. https://android.googlesource.com/platform/tools/base/+/android-m-preview/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/SdCardDetector.java
3. https://android.googlesource.com/platform/tools/base/+/android-m-preview/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/MissingIdDetector.java

## License
&copy;2015 André Diermann

Expand Down
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,33 @@ This reference guide aims to fill this gap and provide essential knowledge on ho
## Structure
The guide is structured into six consecutive sections.

#### 1. Introduction
#### 1. [Introduction](1_introduction)
This section introduces into the domain of Android Lint. It briefly describes the purpose, usage and configuration of this tool.

#### 2. Lint API basics
#### 2. [Lint API basics](2_lint_api_basics)
This section covers the basics of the Android Lint API. It explains the core concepts of the API, such as `Issues`, `Detectors`, `Scanners` and `IssueRegistry`.

#### 3. Getting started
#### 3. [Getting started](3_getting_started)
After the elaboration of the Lint API theory in [section 1](1_introduction/) and [section 2](2_lint_api_basics/) this chapter provides practical explanations and code to get you started writing your own custom Android Lint rules.

#### 4. Detectors
#### 4. [Detectors](4_detectors)
This section demonstrates how to cope with `Detectors`. It showcases simple as well as advanced detecting techniques.

#### 5. Verification
#### 5. [Verification](5_verification)
This section presents how to test and debug custom Lint rules.

#### 6. Application
#### 6. [Application](6_application)
This section elaborates options to apply your custom Android Lint rules to your Android application project.


## Additional Resources
If you are looking for more information on Lint the following resources can be helpful.

1. [Android Docs on Lint](https://developer.android.com/studio/write/lint.html): Covers how to configure lint
2. [Android's Conceptual Overview of Lint](http://tools.android.com/tips/lint/writing-a-lint-check): Covers the concepts of writing android lint rules.
3. [A Presentation on Lint with Android](https://academy.realm.io/posts/360andev-matthew-compton-linty-fresh-java-android/): Presents is the same information as this guide, in video format.
4. [Writing Lint Rules with Annotations](https://android.jlelse.eu/writing-custom-lint-rules-and-integrating-them-with-android-studio-inspections-or-carefulnow-c54d72f00d30): Walks you through writing a lint rule that enforces an annotation.

## Contributing
Contribution to this guide is appreciated. Please refer to the [contribution guidelines](CONTRIBUTING.md) if you want to contribute something.

Expand Down