googlesamples / android-custom-lint-rules

This sample demonstrates how to create a custom lint checks and corresponding lint tests
Apache License 2.0
968 stars 174 forks source link

lintChecks with AGP7 #56

Open androideveloper opened 3 years ago

androideveloper commented 3 years ago

We are currently migrating to AGP 7 and noticed that our custom lint rules are not included in the lint checks. We previously included them using lintChecks project(":libraries:lint-rules") and we only need them for local code check, so no publishing needed. Is there anything specific that needs to be changed to make it work with AGP 7?

tnorbye commented 3 years ago

Do you mean that they're not packaged (e.g. the AAR file does not contain a lint.jar) or that somehow the checks aren't working/being applied?

androideveloper commented 3 years ago

the checks aren't working/being applied

this.

An example of a lint check we have is below. And to test the setup, we previously added a usage of SimpleDateFormat and could see it in the lint report. After updating to AGP 7 we don't see it in the lint report.

@Suppress("UnstableApiUsage")
class LegacyDateNamingPatternDetector : Detector(), Detector.UastScanner {
    override fun getApplicableUastTypes() = listOf(UImportStatement::class.java)

    override fun createUastHandler(context: JavaContext) = LegacyDateInvalidImportHandler(context)
}

class LegacyDateInvalidImportHandler(private val context: JavaContext) : UElementHandler() {
    override fun visitImportStatement(node: UImportStatement) {
        node.importReference?.let { importReference ->
            if (importReference.asSourceString().contains("java.text.SimpleDateFormat")) {
                context.report(IssueLegacyDateImport, node, context.getLocation(importReference), "Forbidden import of Legacy Date API")
            }
        }
    }
}

class IssueRegistry : IssueRegistry() {
    override val issues: List<Issue> = listOf(
        IssueLegacyDateImport,
        IssueContextCompatIncorrectUsage
    )

    override val api: Int = CURRENT_API

    override val vendor: Vendor = Vendor(
        vendorName = "placeholder",
        feedbackUrl = "placeholder",
        contact = "placeholder"
    )
}

val IssueLegacyDateImport = Issue.create(
    id = "LegacyDateImport",
    briefDescription = "Old Java date APIs used",
    explanation = "Use Java 8 dates instead. See DateTimeExt class for more info.",
    category = CORRECTNESS,
    priority = 5,
    severity = Severity.WARNING,
    implementation = Implementation(
        LegacyDateNamingPatternDetector::class.java,
        EnumSet.of(Scope.JAVA_FILE, Scope.TEST_SOURCES)
    )
)
glorinli commented 2 years ago

Same issue with AGP 7.0.3 and lint 30.0.3.

glorinli commented 2 years ago

I figured out that in my case it's caused by the maven publish plugin since I used third party maven plugin and seems it doesn't work well with AGP 7.0.3.

It's ok when I switch to builtin maven-publish plugin.

apkelly commented 2 years ago

I have the same problem, but I'm not using the maven-publish plugin at all, and I'm not publishing my lint checks via a library module, I'm just using them locally.

I am using kotlin for my build scripts, not groovy.....would that make a difference?

Android Studio Bumblebee | 2021.1.1 Beta 4 AGP=7.1.0-beta04 Lint=30.1.0-beta04

I've literally just included the code from this repo and have a Log.println() in my MainActivity which doesn't get highlighted when I run ./gradlew lintDebug or inside Android Studio.