Android Lint
This is a generic Android Lint library that you can use to check code specifications, bugs, resource names, etc ✌️.
The biggest feature of this library is that it is universal. Compared with other Lint libraries (where rules are written directly in code), the biggest difference is that rules are generated entirely by configuration and are more flexible. In multi-team cooperation, you only need to copy the configuration file and modify the prompt information to complete migration.
Supports incremental scan for higher speed.
More friendly: supports automatic script execution when Lint finds errors.
For the principle of incremental scanning, see the Lint incremental scanning practice and the Android Lint code checking practice for specific projects.
If you are interested, welcome to develop and communicate with us.
address
Github.com/RocketZLY/A…
The project structure
.
Flag Exercises – – Flag Exercises – – Flag Exercises – – Flag Exercises – – Flag Exercises – – Flag Exercises – – Flag Exercises – – Flag Exercises ├─ Lintplugin // Lint plugin for incremental scanning, script execution, simplified configuration, log output, etc
Rely on
Android Gradle Plugin is required to be above 3.5.0. Currently, 3.5.3 test OK is used in my project.
The root directory gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.rocketzly:lintPlugin:$lastVersion"}}Copy the code
module gradle
apply plugin: "com.rocketzly.lintPlugin"
Copy the code
use
After adding the dependencies, add the custom_lint_config.json rule configuration file in the project root directory (see rule configuration below for configuration)
Encoding real-time prompt
/gradlew :${moduleName}:lintFull)
It is important to note that AS does not work particularly well for Lint real-time hints. There are rare cases where the hints will not be generated during coding, but command line execution is normal, so the report results will prevail.
Command line execution
Currently, two commands are supported:
./gradlew lintFull
Full scan (only for custom issues)./gradlew lintIncrement -Pbaseline="xxx" -Prevision="xxx"
Incremental scan (only for custom issues)
Parameter Description:
- Baseline: mandatory parameter for executing lintIncrement, used to set the baseline code branch or commit node
- Revision: lintIncrement must be used to set the latest branch or commit node
- ScriptPath: optional, automatic scriptPath execution if lint finds an error, currently only python3 and shell scripts are supported
Script input parameter:
- ReportPath: HTML report address
- UserName: indicates the name of the operator
- ModuleName: indicates the moduleName
- ErrorCount: indicates the number of errors
For details about how to obtain parameters, see lintScriptDemo script in the project root directory
By the way, incremental scanning is throughgit diff $baseline $revision --name-only --diff-filter=ACMRTUXB
To find the change file, so anything supported by git can be used as an input parameter to baseline and Revision.
/gradlew :app: lintincrement-pbaseline =” XXX “-prevision =” XXX”
/gradlew :app: lintincrement-pbaseline =”dev” -prevision =”HEAD” -pscriptPath =” lintnotification. py”
View the generated Html file to see the detailed Lint report
The latest version of the code log and report may be slightly different without affecting the results
Lint configuration
module gradle
lintConfig {
baseline = true// Generate the baseline file. The default value is false
}
Copy the code
- Baseline: Whether to generate the baseline file. The default value is false. If the baseline file needs to be generated, set this parameter to true (optional).
Default (currently not configurable, issue can be raised if necessary)
- Only custom issues are scanned
- Warning is not treated as error
- Task execution is stopped only when an error is detected
- HTML report address:
${modulePath}/build/reports/lint-results.html
- XML report address:
${modulePath}/build/reports/lint-results.xml
- Baseline address:
${modulePath}/lint-baseline.xml
Rule configuration
Custom Issue rules are read from the root custom_lint_config.json file.
Currently, five types of rules are supported (we will continue to add and improve the rules in the later stage, welcome interested leaders to develop together)
- Avoid using apis (subdivided into three categories)
- Methods to avoid
- Avoid creating classes
- Avoid classes that inherit or implement
- You need a way to handle exceptions
- Dependency_method Api (trigger_method must be called after the method is called)
- Resource naming conventions
- The Serializerable reference type member variable must also implement the Serializerable interface
The detailed configuration rules and instructions are as follows (note: Since the markdown table cannot represent hierarchical relationships, + is used to represent json hierarchical relationships)
field | type | The default value | Whether must | note |
---|---|---|---|---|
avoid_usage_api | Object | There is no | Not a must | Avoid using apis |
+method | Object[] | There is no | Not a must | Methods to avoid |
++name | String | There is no | Name and name_regex | Method name, matching the full path to Toast# show (), for example, you should fill in the android. The widget. The Toast. The show |
++name_regex | String | There is no | Name and name_regex | Method of regular matching, Log# I (), for example, should fill in the android. Util. The | | d | | I w e (v) |
++message | String | There is no | Must be | Prompt information |
++exclude | String[] | There is no | Not a must | Need to rule out inspection, matching the full path name, such as com. Rocketzly. Androidlint. Test |
++exclude_regex | String | There is no | Not a must | Class re matches that need to be excluded from the check |
++severity | String | “error” | Not a must | Lint Error severity Default error, fatal, error, warning, informational, ignore |
+construction | Object[] | There is no | Not a must | Avoid creating classes |
++name | String | There is no | Name and name_regex | Constructor name, matching method full path, using Thread constructor as an example java.lang.Thread |
++name_regex | String | There is no | Name and name_regex | Method names re match |
++message | String | There is no | Must be | Prompt information |
++exclude | String[] | There is no | Not a must | Need to rule out inspection, matching the full path name, such as com. Rocketzly. Androidlint. Test |
++exclude_regex | String | There is no | Not a must | Class re matches that need to be excluded from the check |
++severity | String | “error” | Not a must | Lint Error severity Default error, fatal, error, warning, informational, ignore |
+inherit | Object[] | There is no | Not a must | Avoid classes that inherit or implement |
++name | String | There is no | Name and name_regex | The name of the class, match the full path name of the class, such as com. Rocketzly. Androidlint. Test |
++name_regex | String | There is no | Name and name_regex | The name of the class of regular matching, such as \. (AppCompat | Main)? Activity$ |
++message | String | There is no | Must be | Prompt information |
++exclude | String[] | There is no | Not a must | Need to rule out inspection, matching the full path name, such as com. Rocketzly. Androidlint. Test |
++exclude_regex | String | There is no | Not a must | Class re matches that need to be excluded from the check |
++severity | String | “error” | Not a must | Lint Error severity Default error, fatal, error, warning, informational, ignore |
handle_exception_method | Object[] | There is no | Not a must | You need a way to handle exceptions |
+name | String | There is no | Name and name_regex | Method name, matching the full path to Toast# show (), for example, you should fill in the android. The widget. The Toast. The show |
+name_regex | String | There is no | Name and name_regex | Method of regular matching, Log# I (), for example, should fill in the android. Util. The | | d | | I w e (v) |
+exception | String | There is no | Must be | Need to deal with the abnormal full path name of the class, such as Java. Lang. IllegalArgumentException |
+message | String | There is no | Must be | Prompt information |
+exclude | String[] | There is no | Not a must | Need to rule out inspection, matching the full path name, such as com. Rocketzly. Androidlint. Test |
+exclude_regex | String | There is no | Not a must | Class re matches that need to be excluded from the check |
+severity | String | “error” | Not a must | Lint Error severity Default error, fatal, error, warning, informational, ignore |
dependency_api | Object[] | There is no | Not a must | There are dependency apis |
+trigger_method | String | There is no | Must be | Trigger method, matching the full path to Toast# show (), for example, you should fill in the android. The widget. The Toast. The show |
+dependency_method | String | There is no | Must be | Dependency_method is a method that matches the method path (trigger_method) |
+message | String | There is no | Must be | Prompt information |
+severity | String | “error” | Not a must | Lint Error severity Default error, fatal, error, warning, informational, ignore |
resource_name | Object | There is no | Not a must | Resource naming conventions |
+drawable | Object | There is no | Not a must | Drawable naming convention |
++name_regex | String | There is no | Must be | Resource name regularization specification |
++message | String | There is no | Must be | Prompt information |
++severity | String | “error” | Not a must | Lint Error severity Default error, fatal, error, warning, informational, ignore |
+layout | Object | There is no | Not a must | Layout naming conventions |
++name_regex | String | There is no | Must be | Resource name regularization specification |
++message | String | There is no | Must be | Prompt information |
++severity | String | “error” | Not a must | Lint Error severity Default error, fatal, error, warning, informational, ignore |
serializable_config | Object | There is no | Not a must | The Serializable configuration |
+name_regex | String | There is no | Must be | Check which classes are in the package, such as only checking your own class ^ com.rocketzly.androidLint |
+message | String | There is no | Must be | Prompt information |
+severity | String | “error” | Not a must | Lint Error severity Default error, fatal, error, warning, informational, ignore |
The matching rule is exclude and then match. Exclude > exclude_regex > name > name_regex indicates the priority.
Message affects the information displayed in real-time prompts AS and reports.
Severity is the error level corresponding to AS real-time prompt and the error level in the report.
For demo, see custom_lint_config.json in the root directory of the project.
about
If you are interested in Lint, please add me to my wechat account: RocketZly.
In addition, I have also created a Lint communication group, welcome to join me in the group.