Search code examples
androidandroid-studioandroid-lint

Custom android lint rules not being executed


I have created two custom lint rules and verified that they work as expected with unit tests. However, when I try to run lint on the whole project with ./gradlew app:lint these rules are not executed.
To verify that they are not executed I debugged (following this advice) and the debugger did not stop at breakpoints set in the Detectors.
A println set in the get issues of my IssueRegistry is executed (twice).
I have tried to run in lint --list and ./gradlew lint --list in terminal but none of these work (lint does not exist, and --list option does not exist, respectively).
I have also tried manually enabling the lint checks in lintOptions, in the build.gradle.

How can I get my custom rules to execute?

How can I see a list of the available lint rules, or gather information in any other way? In inspections, searching by id my custom rules do not appear.


My lint code is in a separate module. This is the registry:

class IssueRegistry : IssueRegistry() {
    override val api: Int = CURRENT_API

    override val issues: List<Issue> get() = listOf(
        DispatchersDetector.ISSUE,
        ImportsDetector.ISSUE,
    ).also { println("creating issue registry!!!!!!!!!!") }
}

I also have a file in resources/META_INF/services/my.package.lintrules.IssueRegistry, which I think is not necessary (I've tried adding it and removing it). It contains my.package.lintrules.IssueRegistry.

This is the gradle for the lintrules module (Kotlin DSL, but I've also tried with groovy just in case, with no luck) (I have also tried VERSION_1_7 for Java):

plugins {
    id("java-library")
    id("kotlin")
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

dependencies {
    compileOnly("org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion")
    compileOnly("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
    compileOnly("com.android.tools.lint:lint-api:${Versions.lint}")
    compileOnly("com.android.tools.lint:lint-checks:${Versions.lint}")

    testImplementation("junit:junit:$jUnitVersion")
    testImplementation("com.android.tools.lint:lint:${Versions.lint}")
    testImplementation("com.android.tools.lint:lint-tests:${Versions.lint}")
    testImplementation("com.android.tools:testutils:${Versions.lint}")
}

tasks {
    jar {
        manifest {
            attributes(
                "Lint-Registry-v2" to "my.package.lintrules.IssueRegistry"
            )
        }
    }
}

I added the kotlin stdlib and reflect to remove some warnings about conflicting versions. It still complains about using different versions of the stdlib and reflect.
I use the following versions: android gradle=4.1.1; lint=27.1.1; kotlin=1.4.10; android studio=4.1.1.

In the app build.gradle I have added lintChecks(project(path = ":lintrules")) in dependencies.


Solution

  • The problem was that I had the Issue parameter androidSpecific set to false and it should have been set to null.
    According to the documentation: true if this issue only applies to Android, false if it does not apply to Android at all, and null if not specified or should run on all platforms.