1、从命令行运行Lint(需要配置环境变量)
lint --help
2、通过gradle运行Lint
gradlew lint
3、配置lint
(1)配置lint.xml
<?xml version="1.0" encoding="UTF-8"?> <lint> <!-- Disable the given check in this project --> <issue id="IconMissingDensityFolder" severity="ignore" /> <!-- Ignore the ObsoleteLayoutParam issue in the specified files --> <issue id="ObsoleteLayoutParam"> <ignore path="res/layout/activation.xml" /> <ignore path="res/layout-xlarge/activation.xml" /> </issue> <!-- Ignore the UselessLeaf issue in the specified file --> <issue id="UselessLeaf"> <ignore path="res/layout/main.xml" /> </issue> <!-- Change the severity of hardcoded strings to "error" --> <issue id="HardcodedText" severity="error" /> </lint>
(2)配置对java的检查
@SuppressLint("NewApi") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
(3)配置对xml的检查
您可以使用 tools:ignore
属性禁止 Lint 检查 XML 文件的特定部分。在 lint.xml
文件中添加以下命名空间值,以便 Lint 工具能识别此属性:
namespace xmlns:tools="http://schemas.android.com/tools"
tools:ignore="NewApi,StringFormatInvalid"
tools:ignore="all"
(4)通过gradle配置lint选项
android { ... lintOptions { // Turns off checks for the issue IDs you specify. disable 'TypographyFractions','TypographyQuotes' // Turns on checks for the issue IDs you specify. These checks are in // addition to the default lint checks. enable 'RtlHardcoded','RtlCompat', 'RtlEnabled' // To enable checks for only a subset of issue IDs and ignore all others, // list the issue IDs with the 'check' property instead. This property overrides // any issue IDs you enable or disable using the properties above. check 'NewApi', 'InlinedApi' // If set to true, turns off analysis progress reporting by lint. quiet true // if set to true (default), stops the build if errors are found. abortOnError false // if true, only report errors. ignoreWarnings true } } ...
4、手动运行检查
您可以选择 Inspect Code > Analyze,手动运行已配置的 Lint 和其他 IDE 检查。检查结果显示在 Inspection Results 窗口中。