directory

1, Preface 2, brief analysis of the structure of LintOptions 3, the attributes of LintOptions 4, the method of LintOptions 5, write in the last

One, foreword

Today’s lintOptions configuration is designed to help us find code quality issues in our projects.

Ii. Brief analysis of the structure of LintOptions

2.1 Location of LintOptions

android {
    lintOptions {
    	// lintOptions configuration}}Copy the code

2.2 Inheritance of LintOptions

By convention, we know that Gradle configurations map to a Java class.

LintOptions mapping to com. The android. Build. Gradle. Internal. DSL. LintOptions class, its inheritance structure is as follows

graph LR
A[LintOptions] -.-> B[com.android.builder.model.LintOptions]
A[LintOptions] -.-> C[Serializable]

2.3 LintOptions run

./gradlew app:lint
Copy the code

Pay attention to the little point

  • Using the Window environmentgradlewUse the MAC./gradlew
  • useapp:lintYou can append variants, we only have them herereleasedebugSo, it can be usedapp:lintReleaseapp:lintDebugRun checks on variants separately.

3. Properties of LintOptions

3.1 abortOnError

  • Types: Boolean
  • Description: Stop the build if an error is found.
  • Usage:
lintOptions {
	abortOnError true
}
Copy the code
  • To recap:

Lint checks are normally enabled by default when releasing packages, so sometimes you will get a message like this:

> Lint found errors in the project; aborting build. Fix the issues identified by lint, or add the following to your build script to proceed with errors: . android { lintOptions { abortOnErrorfalse}}... The first3 errors (out of 6) were:
  /Users/zinc/Documents/code/gradle/GradleStudy/app/src/main/res/drawable/ic_arrow_drop_down_black_24dp.xml:1: Error: The resource R.drawable.ic_arrow_drop_down_black_24dp appears to be unused [UnusedResources]
  <vector xmlns:android="http://schemas.android.com/apk/res/android"
  ^
  /Users/zinc/Documents/code/gradle/GradleStudy/app/src/main/res/drawable/ic_launcher_background.xml:2: Error: The resource R.drawable.ic_launcher_background appears to be unused [UnusedResources]
  <vector xmlns:android="http://schemas.android.com/apk/res/android"
  ^
  /Users/zinc/Documents/code/gradle/GradleStudy/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: Error: The resource R.mipmap.ic_launcher_round appears to be unused [UnusedResources]
Copy the code

At this point we have two solutions:

  1. Set abortOnError to false so that the error does not abort our compilation, but this is not recommended.
  2. Fix the detected problem and let the compilation run properly. For example, remove the unused resource.

3.2 absolutePaths

  • Types: Boolean
  • Description: Indicates whether the report document outputs the full path. The default value is relative path
  • Usage:
lintOptions {
	absolutePaths false
}
Copy the code

When relative to paths, the paths in red are relative to the gradle file where we set this property.

3.3 check

  • Type: Set< String >
  • Description: Sets the type to be checked only.

The type ids that can be set can be viewed using Lint –list

  • Usage:
lintOptions {
	// Check only unused resources
	check 'UnusedResources'
}
Copy the code

3.4 checkAllWarnings

  • Types: Boolean
  • Description: Whether to check for all warnings. By default, the check type for some warnings is turned off. When it is turned on, all will be checked.
  • Usage:
lintOptions {
	checkAllWarnings true
}
Copy the code

3.5 checkReleaseBuilds

  • Types: Boolean
  • Description: Check whether fatal type errors are detected in the release version. The default release version is enabled. If the FATAL type error is detected, the fatal type error is disabled.
  • Usage:
lintOptions {
	checkReleaseBuilds true
}
Copy the code

3.6 disable

  • Type: Set< String >
  • Description: Disable the type of a check, enter the type ID, can passlint --listView supported ids.
  • Usage:
lintOptions {
	// Close the UnusedResources check
	disable 'UnusedResources'
}
Copy the code

3.7 enable

  • Type: Set< String >
  • Description: Enable the type of a check. Enter the type ID and pass the checklint --listView supported ids.
  • Usage:
lintOptions{
	enable 'UnusedResources'
}
Copy the code

3.8 htmlOutput

  • Type: the File
  • Description: Specifies a file to output HTML files. It overwrites the original content.
  • Usage:
lintOptions {
	// Specifies the output to the htmlReport. HTML file, relative to the gradle address that set this property.
	htmlOutput file('htmlReport.html')}Copy the code

3.9 htmlReport

  • Types: Boolean
  • Description: Whether to output HTML reports. This function is enabled by default
  • Usage:
lintOptions {
	htmlReport true
}
Copy the code

3.10 ignoreWarnings

  • Types: Boolean
  • Description: Whether to ignore warnings and only check for error.
  • Usage:
lintOptions {
	ignoreWarnings true
}
Copy the code

3.11 lintConfig

  • Type: the File
  • Description: The configuration is used as an alternate default configuration file. If a check type ID is set to error and is set to ignore in the lintConfig configuration file, it is still considered error. In short, lintConfig has the lowest priority.
  • Usage:
lintOptions {
	lintConfig file('lint/rule_lint.xml')}Copy the code

The contents of lint/rule_lint.xml are as follows


      
<lint>
    <! -- id you can check supported ids with "lint-\ -list" -->
    <! -- Severity is set to the severity level -->
    <issue id="UnusedResources" severity="error"/>
</lint>
Copy the code

3.12 the -quiet

  • Types: Boolean
  • Description: Whether to turn off some information output, such as the output of the report file path
  • Usage:
lintOptions {
	quiet true
}
Copy the code

3.13 showAll

  • Types: Boolean
  • Description: Whether to output all information without reducing information.
  • Usage:
lintOptions {
	showAll true
}
Copy the code

3.14 textOutput

  • Type: the File
  • Description: Check the path of the report output as text.
  • Usage:
lintOptions {
	textOutput file('report/zincTextReport.txt')}Copy the code

3.15 textReport

  • Types: Boolean
  • Description: Whether to enable the check report output as text.
  • Usage:
lintOptions {
	textReport true
}
Copy the code

3.16 warningsAsErrors

  • Types: Boolean
  • Description: Whether to output all warnings as errors
  • Usage:
lintOptions {
	warningsAsErrors true
}
Copy the code

3.17 xmlOutput

  • Type: the File
  • Description: Check the path of the report output as XML.
  • Usage:
lintOptions {
	xmlOutput file('report/zincXmlReport.xml')}Copy the code

3.18 xmlReport

  • Types: Boolean
  • Description: Whether to enable the check report output as text.
  • Usage:
lintOptions {
	xmlReport true
}
Copy the code

Iv. Methods of LintOptions

4.1 check

  • check(id)
  • check(ids)

You can add only one ID and multiple IDS

Description: The type ID used to add checks, which can be viewed via lint –list.

Usage:

lintOptions {
	check 'id1'
	check 'id1'.'id2'
}
Copy the code

4.2 disable

  • disable(id)
  • disable(ids)

You can add only one ID and multiple IDS

Description: Type ID used to turn off checking. Id can be viewed via lint –list.

Usage:

lintOptions {
	disable 'id1'
	disable 'id1'.'id2'
}
Copy the code

4.3 enable

  • enable(id)
  • enable(ids)

You can add only one ID and multiple IDS

Description: Type ID used to enable checking. Id can be viewed via lint –list.

Usage:

lintOptions {
	enable 'id1'
	enable 'id1'.'id2'
}
Copy the code

4.4 the error

  • error(id)
  • error(ids)

You can add only one ID and multiple IDS

Description: Used to set the type ID of the check to error.

Usage:

lintOptions {
	error 'id1'
	error 'id1'.'id2'
}
Copy the code

4.5 fatal

  • fatal(id)
  • fatal(ids)

You can add only one ID and multiple IDS

Description: Used to set the checked type ID to fatal error.

Usage:

lintOptions {
	fatal 'id1'
	fatal 'id1'.'id2'
}
Copy the code

4.5 ignore

  • ignore(id)
  • ignore(ids)

You can add only one ID and multiple IDS

Description: Used to set the checked type ID to ignore.

Usage:

lintOptions {
	ignore 'id1'
	ignore 'id1'.'id2'
}
Copy the code

4.6 warning

  • warning(id)
  • warning(ids)

You can add only one ID and multiple IDS

Description: Used to set the check type ID to warning error.

Usage:

lintOptions {
	warning 'id1'
	warning 'id1'.'id2'
}
Copy the code

Write at the end

Gradle project address: Github portal

For lintOptions configuration for this post, please enter the portal

If you find this post inspiring or confusing, give it a thumbs up or follow us, and kids will continue to share more great articles.

Or invite me to have a cup of coffee, children will be more confident to write