autonomousapps / dependency-analysis-gradle-plugin

Gradle plugin for JVM projects written in Java, Kotlin, Groovy, or Scala; and Android projects written in Java or Kotlin. Provides advice for managing dependencies and other applied plugins
Apache License 2.0
1.82k stars 120 forks source link

kapt is reported as unused plugin on an excluded unused annotation processor dependency #778

Open ivanalvarado opened 2 years ago

ivanalvarado commented 2 years ago

Is your feature request related to a problem? Please describe.

The plugin is raising an error that the kapt plugin is unused even when it's applied to an excluded unused dependency in a gradle project.

For example, in this project our dependency analysis plugin is configured to not fail if 'com.google.dagger:hilt-android-compiler' is unused for annotation processors: build-configuration/project-health.gradle

dependencyAnalysis {
    issues {
        onUnusedDependencies {
            severity('ignore')
        }
        onUsedTransitiveDependencies {
            severity('ignore')
        }
        onIncorrectConfiguration {
            severity('ignore')
        }
        onUnusedAnnotationProcessors {
            severity('fail')
            exclude('com.google.dagger:hilt-android-compiler')
        }
        onRedundantPlugins {
            severity('fail')
        }

        ignoreKtx(true)
    }
}

In the mylibrary/build.gradle we have 'com.google.dagger:hilt-android-compiler:2.43.2' configured with kapt:

apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply plugin: "kotlin-kapt"

...

dependencies {
    kapt 'com.google.dagger:hilt-android-compiler:2.43.2'
    ...
    implementation 'com.google.dagger:hilt-android:2.43.2'
}

When we run dependency analysis plugin by doing ./gradlew :mylibrary:projectHealth, the plugin correctly excludes 'com.google.dagger:hilt-android-compiler' from being reported as an unused annotation processor, but raises that the kapt plugin was applied but no annotation processors were used:

> Task :mylibrary:projectHealth FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':mylibrary:projectHealth'.
> Unused plugins that can be removed:
    kotlin-kapt: this project has the kotlin-kapt (org.jetbrains.kotlin.kapt) plugin applied, but there are no used annotation processors.

Describe the solution you'd like

We expect the plugin to detect that 'com.google.dagger:hilt-android-compiler' is excluded and because it's using the kapt plugin, the plugin shouldn't be reported as unused.

Describe alternatives you've considered

We are able to add a workaround for now by excluding kotlin-kapt as a redundant plugin in the dependency analysis configuration. However, we feel this is a bit aggressive:

onRedundantPlugins {
    severity('fail')
    exclude('kotlin-kapt')
}

Additional context

We assume that the plugin is detecting that because 'com.google.dagger:hilt-android-compiler' is unused and configured with kapt, then it effectively detects the kapt plugin as unused as well and thus raises this as an error.

We've create a project that easily reproduces this behavior: https://github.com/ivanalvarado/Dependency-Analysis-Plugin-Repro Simply run ./gradlew :mylibrary:projectHealth and observe the failure.

autonomousapps commented 2 years ago

Thanks for the issue. I'm a bit on the fence as to whether this is a bug or a feature request :) I'll certainly consider whether some improvement could be made in this area.

ivanalvarado commented 2 years ago

Thanks for the issue. I'm a bit on the fence as to whether this is a bug or a feature request :) I'll certainly consider whether some improvement could be made in this area.

Thanks for considering this. In the meantime, we've got a workaround where we apply a plugin configuration in the failing module like the following:

apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply plugin: "kotlin-kapt"

...

dependencies {
    kapt 'com.google.dagger:hilt-android-compiler:2.43.2'
    ...
    implementation 'com.google.dagger:hilt-android:2.43.2'
}

// Dependency Analysis returns the kotlin-kapt could be removed. However the test execution fails(kapt used in test
// implementation. Ignoring manually.
dependencyAnalysis {
    issues {
        onAny {
            severity('fail')
            exclude('kotlin-kapt')
        }
    }
}