这里不多介绍Lint开发环境的搭建,可以直接参考官方demo:github.com/googlesampl… 。 具体效果可以参考ToastDetector:提醒Toast增加show()方法。
关键方法:
Detector中有两个和project相关的方法:
-
beforeCheckRootProject(context: Context)
,遍历根module之前的回调,即项目中最顶层app module -
beforeCheckEachProject(context: Context)
,遍历每个子module之前的回调。
该两个方法中都有Context对应的上下文,在构造函数中惊奇的发现Project这个属性。
open class Context(
main: Project?,
project: Project?,
val file: File,
private var contents: CharSequence? = null
)
是不是感觉和gradle中的project很像?通过debug发现,该project确实包含了build.gradle中的depedency信息,那么基于Lint方案的思路就肯定可以落地了。窃窃自喜:「只要你想要,没有我给不了」。
关键代码:
class DependencyProjectDetector : Detector(), Detector.UastScanner {
// 各个module之间的依赖树
private val treeMap = HashMap<String, ElementNode>()
// 根module结点
private var rootNode = ElementNode()
companion object {
const val TAG = “DependencyProjectDetector”
private val IMPLEMENTATION = Implementation(
DependencyProjectDetector::class.java,
Scope.JAVA_FILE_SCOPE
)
val ISSUE: Issue = Issue
.create(
id = “DependencyProjectDetector”,
briefDescription = “app dependency relationship”,
explanation = “”"
app dependency relationship
“”".trimIndent(),
category = Category.CORRECTNESS,
priority = 9,
severity = Severity.FATAL,
androidSpecific = true,
imp