自定义Lint采坑记录(AS-4.0.1版本)

1,536 阅读2分钟

自定义 Lint

如果你不熟悉自定义 Lint 的规则,参考下面三篇就足够了,其中「美团」的教程 lint 版本比较低,如果你要用新版可以忽略。

  1. Android自定义Lint实践 (Custom Lint Rules & Lint Plugin)
  2. 自定义 lint 规则实践研究
  3. 美团-Android自定义Lint实践

下面有几点上面的资料没涉及到,做个补充

Lint 的依赖方式

通过参考资料知道,lint依赖的方式有两种

  • google:将 lint.jar 放到 ~/.android/lint/下,缺点是所有工程都会用 lint 规则,灵活度太差
  • LinkedIn:用 aar 将 lint.jar 包裹,然后发布 aar,哪个工程想用的话依赖 aar 即可

一般项目都会放弃 google 的做法,都会用第二种,但是参考资料里面大都给了一个自己实现的方案(那会 gradle 还没做支持),通过自定义 gradle configuration,进行一个跨项目的 configuration 传递,将 lint.jar copy 到 aar 工程中,大概代码是这样的

lint-rules 工程


jar {
    manifest {
        attributes 'Lint-Registry': 'com.lint.TestLint'
    }
}

configurations {
    lintOutputJar
}

dependencies {
    lintOutputJar files(jar)
}

lint-aar 工程

configurations {
    lintImport
}

dependencies {
    lintImport project(path: ":lint-rules", configuration: "lintOutputJar")
}

task copyLintJar(type: Copy){
    from (configurations.lintOutputJar) {
        rename {
            String fileName ->
                'lint.jar'
        }
    }
    into 'build/intermediates/lint/'
}

通过 google-lint-samples 的示例代码得知,gradle 已经有默认支持的 configuration 了,叫 lintPublish,所以只需要在 lint-aar 工程中这样配置


/** Package the given lint checks library into this AAR  */
dependencies {
    lintPublish project(':lint-rules')
}

Lint 的坑

开发环境

Android Studio: 4.0.1 gradle version: 5.4.1 android-gradle-build-tools: 3.5.3 lint api: 27.0.1

按照自定义 Lint 教程一顿操作后,发现 IDE 上可以完美提示,但是用 ./gradlew lint 死活都不会把 error 报告到 xml 里,lint 配置了 abortOnError=true 也不会报错,report.xml里只有一条 Issue Obsolete custom lint check

参考文章里一般说的问题都是,IDE 不会提示,但是报告里会有,这次正好相反。于是想到了之前看到过的信息 lint 和 android-gradle-build-tools 的版本是有依赖关系的,有深坑,然后就查这方面的信息,结果查到了这篇 自定义lint 报错:Obsolete custom lint check

最终把 lint api 的版本降低后就一切正常了。

除此之外,新版本 lint 的 api 没有文档,注释有的也没有,只能靠猜靠调试,只能靠个人积累了。

如何调试 Lint

There is the easier way: just add in your command line -Dorg.gradle.debug=true --no-daemon

For example: gradle nameOfTask -Dorg.gradle.debug=true --no-daemon

Then you should start your IDE and run remote debugging with localhost port 5005, that all.

Gradle is waiting to you, because standard option server=y

org.gradle.debug

When set to true, Gradle will run the build with remote debugging enabled, listening on port 5005. Note that this is the equivalent of adding -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 to the JVM command line and will suspend the virtual machine until a debugger is attached.