Android lint检查fix自动导入包

Android lint检查fix自动导入包

fix自动修复代码时自动将类导入。

示例

例如Log使用封装的工具类修复后:
在这里插入图片描述
代码里自动 import com.lkl.lint.commonlib.utils.LogUtils;
在这里插入图片描述

实现

核心代码:

fix().replace()
	.name(fixDisplayName)
	.range(location)
	.with("${fixClassName}.${method.name}")
	.shortenNames()
	.autoFix()
	.build()
  • fixClassName是配置文件里配置的要使用的工具类的全类名
    在这里插入图片描述
  • shortenNames()

The IDE should simplify fully qualified names in the element after this fix has been run (off by default)

从注释可以看到这个是起作用的关键所在。

参考文献

https://googlesamples.github.io/android-custom-lint-rules/api-guide/quickfixes.md.html


版权声明:本文为lkl22原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。